今天,一位原公司的同事,打電話來問我說誤操作了資料庫數據,如何恢復。他原本的想法是登入資料庫update一個記錄,結果忘了加where,於是悲劇發生了。今天,我們不講如何恢復誤操作的數據,我們講講如何從源頭避免這樣的問題,這才是避免類似問題的根本措施。好了。我們直接進入主題:
1、MySQL幫助說明
[root@liuyazhuang151 ~]# 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登入測試
[root@liuyazhuang151 ~]# mysql -uroot -proot -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誤操作
[root@liuyazhuang151 ~]# alias mysql='mysql -U' [root@liuyazhuang151 ~]# 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 [root@liuyazhuang151 ~]# echo "alias mysql='mysql -U'" >>/etc/profile [root@liuyazhuang151 ~]# . /etc/profile [root@liuyazhuang151 ~]# tail -1 /etc/profile alias mysql='mysql -U'
4、結論:
在MySQL命令加上選項-U後,當發出沒有WHERE或LIMIT關鍵字的UPDATE或DELETE命令加上選項-U後,當發出沒有WHERE或LIMIT關鍵字的UPDATE或DELETE命令時, mysql程式拒絕執行
以上就是MySQL之-如何防止人為誤操作MySQL資料庫的內容,更多相關內容請關注PHP中文網(www.php.cn)!