MySQL之——如何防止人為誤操作MySQL數(shù)據(jù)庫

今天,一位原公司的同事,打電話來問我說誤操作了數(shù)據(jù)庫數(shù)據(jù),如何恢復。他原本的想法是登錄數(shù)據(jù)庫update一個記錄,結(jié)果忘了加where,于是悲劇發(fā)生了。今天,我們不講如何恢復誤操作的數(shù)據(jù),我們講講如何從源頭上避免這樣的問題,這才是避免類似問題的根本措施。好了。我們直接進入主題:

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后,當發(fā)出沒有WHERE或LIMIT關(guān)鍵字的UPDATE或DELETE時,mysql程序就會拒絕執(zhí)行

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、結(jié)論:

在MySQL命令加上選項-U后,當發(fā)出沒有WHERE或LIMIT關(guān)鍵字的UPDATE或DELETE時,mysql程序拒絕執(zhí)行

以上就是MySQL之——如何防止人為誤操作MySQL數(shù)據(jù)庫的內(nèi)容,更多相關(guān)內(nèi)容請關(guān)注PHP中文網(wǎng)(www.php.cn)!

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點贊12 分享