电脑疯子技术论坛|电脑极客社区

微信扫一扫 分享朋友圈

已有 1832 人浏览分享

MySQL数据库防止人为误操作的实例讲解

[复制链接]
1832 0

有不少开发人员在操作MySQL数据库的时候都遇到过误操作的情况,例如更新数据库的时候update语句忘记加上where条件,就会造成极为悲剧的结果。本文就针对防止MySQL数据库误操作的方法做出如下详解:

1、mysql帮助说明

  1. # mysql --help|grep dummy  
  2. -U, --i-am-a-dummy Synonym for option --safe-updates, -U.
  3. i-am-a-dummy  FALSE
复制代码

在mysql命令加上选项-U后,当发出没有WHERE或LIMIT关键字的UPDATE或DELETE时,mysql程序就会拒绝执行

2、指定-U登录测试



  1. # mysql -uroot -poldboy123 -S /data/3306/mysql.sock -U
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 14
  4. Server version: 5.5.32-log MySQL Community Server (GPL)
  5. Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
  6. Oracle is a registered trademark of Oracle Corporation and/or its
  7. affiliates. Other names may be trademarks of their respective
  8. owners.
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  10. mysql> delete from oldboy.student;
  11. ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
  12. mysql> quit
  13. Bye
复制代码


提示:此处不加条件无法删除,目的达到。

3、做成别名防止DBA误操作


  1. # alias mysql='mysql -U'
  2. # mysql -uroot -poldboy123 -S /data/3306/mysql.sock
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 15
  5. Server version: 5.5.32-log MySQL Community Server (GPL)
  6. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  7. mysql> delete from oldboy.student;
  8. ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
  9. mysql> delete from oldboy.student where Sno=5;
  10. Query OK, 1 row affected (0.02 sec)
  11. mysql> quit
  12. Bye
  13. # echo "alias mysql='mysql -U'" >>/etc/profile
  14. # . /etc/profile
  15. # tail -1 /etc/profile
  16. alias mysql='mysql -U'
复制代码


结论:在mysql命令加上选项-U后,当发出没有WHERE或LIMIT关键字的UPDATE或DELETE时,mysql程序拒绝执行


您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

关注

0

粉丝

9021

主题
精彩推荐
热门资讯
网友晒图
图文推荐

Powered by Pcgho! X3.4

© 2008-2022 Pcgho Inc.