您好,欢迎访问一九零五行业门户网

防止人为误操作MySQL数据库技巧一例_MySQL

防止人为误操作mysql数据库技巧一例
(本题来自老男孩培训内部学生问题,属于数据库安全技巧)
在若干年前,老男孩亲自遇到一个“命案”,老大登录数据库update一个记录,结果忘了加where,于是悲剧发生了,这使得我对mysql的增量恢复熟练度远超过其他的知识点,同时也很在意对内的数据库安全,并且每次讲课讲到此处,都会给学生讲这个领导的故事。
1、mysql帮助说明
[oldboy_c64 ~]# mysql --help|grep dummy        -u, --i-am-a-dummy synonym for option --safe-updates, -u. i-am-a-dummy      false 
在mysql命令加上选项-u后,当发出没有where或limit关键字的update或delete时,mysql程序就会拒绝执行
2、指定-u登录测试
[oldboy_c64 ~]# mysql -uroot -poldboy123 -s /data/3306/mysql.sock -u welcome to the mysql monitor. commands end with ; or \g. your mysql connection id is 14 server version: 5.5.32-log mysql community server (gpl) copyright (c) 2000, 2013, oracle and/or its affiliates. all rights reserved. oracle is a registered trademark of oracle corporation and/or its affiliates. other names may be trademarks of their respective owners. type 'help;' or '\h' for help. type '\c' to clear the current input statement. mysql> delete from oldboy.student; error 1175 (hy000): you are using safe update mode and you tried to update a table without a where that uses a key column mysql> quit bye 
提示:不加条件无法删除,目的达到。
3、做成别名防止老大和dba误操作
[oldboy_c64 ~]# alias mysql='mysql -u' [oldboy_c64 ~]# mysql -uroot -poldboy123 -s /data/3306/mysql.sock welcome to the mysql monitor. commands end with ; or \g. your mysql connection id is 15 server version: 5.5.32-log mysql community server (gpl) type 'help;' or '\h' for help. type '\c' to clear the current input statement. mysql> delete from oldboy.student; error 1175 (hy000): you are using safe update mode and you tried to update a table without a where that uses a key column mysql> delete from oldboy.student where sno=5; query ok, 1 row affected (0.02 sec) mysql> quit bye [oldboy_c64 ~]# echo alias mysql='mysql -u' >>/etc/profile [oldboy_c64 ~]# . /etc/profile [oldboy_c64 ~]# tail -1 /etc/profile alias mysql='mysql -u' 
结论:
在mysql命令加上选项-u后,当发出没有where或limit关键字的update或delete时,mysql程序拒绝执行
其它类似信息

推荐信息