温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mysql有效防止删库跑路的方法教程

发布时间:2021-09-29 17:22:33 来源:亿速云 阅读:98 作者:iii 栏目:开发技术

这篇文章主要讲解了“mysql有效防止删库跑路的方法教程”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“mysql有效防止删库跑路的方法教程”吧!

目录
  • 安全模式设置

  • 测试

    • 1.无where的update和delete

    • 2、非索引键的delete

    • 3.索引键的delete

  • 总结

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

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

    安全模式设置

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

     show variables like 'sql_safe_updates';

    mysql有效防止删库跑路的方法教程

    默认是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

    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'

    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'

    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

    delete from  t_user where name='123' limit 1
    > Affected rows: 0
    > 时间: 0.002s

    3.索引键的delete

    delete from t_user where group_id='123'

    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有效防止删库跑路的方法教程这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI