MySQL数据库防止人为误操作的实例讲解
有不少开发人员在操作MySQL数据库的时候都遇到过误操作的情况,例如更新数据库的时候update语句忘记加上where条件,就会造成极为悲剧的结果。本文就针对防止MySQL数据库误操作的方法做出如下详解:
1、mysql帮助说明
#mysql--help|grepdummy -U,--i-am-a-dummySynonymforoption--safe-updates,-U. i-am-a-dummyFALSE
在mysql命令加上选项-U后,当发出没有WHERE或LIMIT关键字的UPDATE或DELETE时,mysql程序就会拒绝执行
2、指定-U登录测试
#mysql-uroot-poldboy123-S/data/3306/mysql.sock-U WelcometotheMySQLmonitor.Commandsendwith;or\g. YourMySQLconnectionidis14 Serverversion:5.5.32-logMySQLCommunityServer(GPL) Copyright(c)2000,2013,Oracleand/oritsaffiliates.Allrightsreserved. OracleisaregisteredtrademarkofOracleCorporationand/orits affiliates.Othernamesmaybetrademarksoftheirrespective owners. Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement. mysql>deletefromoldboy.student; ERROR1175(HY000):YouareusingsafeupdatemodeandyoutriedtoupdateatablewithoutaWHEREthatusesaKEYcolumn mysql>quit Bye
提示:此处不加条件无法删除,目的达到。
3、做成别名防止DBA误操作
#aliasmysql='mysql-U' #mysql-uroot-poldboy123-S/data/3306/mysql.sock WelcometotheMySQLmonitor.Commandsendwith;or\g. YourMySQLconnectionidis15 Serverversion:5.5.32-logMySQLCommunityServer(GPL) Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement. mysql>deletefromoldboy.student; ERROR1175(HY000):YouareusingsafeupdatemodeandyoutriedtoupdateatablewithoutaWHEREthatusesaKEYcolumn mysql>deletefromoldboy.studentwhereSno=5; QueryOK,1rowaffected(0.02sec) mysql>quit Bye #echo"aliasmysql='mysql-U'">>/etc/profile #./etc/profile #tail-1/etc/profile aliasmysql='mysql-U'
结论:在mysql命令加上选项-U后,当发出没有WHERE或LIMIT关键字的UPDATE或DELETE时,mysql程序拒绝执行