mysql如何能有效防止删库跑路

吾爱主题 阅读:107 2024-04-02 08:06:18 评论:0

大家肯定听说过,有些开发者由于个人失误,在delete或者update语句的时候没有添加where语句,导致整个表数据错乱。

mysql安全模式:mysql发现delete、update语句没有添加where或者limit条件时会报错。整个sql将无法执行,有效防止了误删表的情况。

安全模式设置

在mysql中通过如下命令查看状态:

?
1 show variables like 'sql_safe_updates' ;

默认是off状态,将状态设置为on即可:

  • set sql_safe_updates=1; //打开
  • set sql_safe_updates=0; //关闭

设置为on之后

  • update语句:where条件中列(column)没有索引可用且无limit限制时会拒绝更新。where条件为常量且无limit限制时会拒绝更新。
  • delete语句: ①where条件为常量,②或where条件为空,③或where条件中 列(column)没有索引可用且无limit限制时拒绝删除。

测试

打开安全模式进行测试

1.无where的update和delete

delete from t_user

?
1 2 3 delete from t_user > 1175 - you are using safe update mode and you tried to update a table without a where that uses a key column > 时间: 0.001s

update t_user set name='123'

?
1 2 3 update t_user set name = '123' > 1175 - you are using safe update mode and you tried to update a table without a where that uses a key column > 时间: 0.001s

2、非索引键的delete

delete from t_user where name='123'

?
1 2 3 delete from  t_user where name = '123' > 1175 - you are using safe update mode and you tried to update a table without a where that uses a key column > 时间: 0.007s

如果delete的where条件不是索引键,则必须要添加limit才可以。

delete from t_user where name='123' limit 1

?
1 2 3 delete from  t_user where name = '123' limit 1 > affected rows : 0 > 时间: 0.002s

3.索引键的delete

delete from t_user where group_id='123'

?
1 2 3 delete from  t_user where group_id= '123' > affected rows : 0 > 时间: 0s

总结

如果设置了sql_safe_updates=1,那么update语句必须满足如下条件之一才能执行成功

  • 使用where子句,并且where子句中列必须为prefix索引列
  • 使用limit
  • 同时使用where子句和limit(此时where子句中列可以不是索引列)

delete语句必须满足如下条件之一才能执行成功

  • 使用where子句,并且where子句中列必须为prefix索引列
  • 同时使用where子句和limit(此时where子句中列可以不是索引列)一才能执行成功。

到此这篇关于mysql如何能有效防止删库跑路的文章就介绍到这了,更多相关mysql 防止删库内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_30285985/article/details/120525460

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容