mysql如何能有效防止刪庫跑路
大家肯定聽說過,有些開發(fā)者由于個人失誤,在delete或者update語句的時候沒有添加where語句,導致整個表數(shù)據(jù)錯亂。
mysql安全模式:mysql發(fā)現(xiàn)delete、update語句沒有添加where或者limit條件時會報錯。整個sql將無法執(zhí)行,有效防止了誤刪表的情況。
安全模式設置
在mysql中通過如下命令查看狀態(tài):
show variables like 'sql_safe_updates';

默認是OFF狀態(tài),將狀態(tài)設置為ON即可:
set sql_safe_updates=1;//打開set sql_safe_updates=0;//關閉
設置為ON之后
- update語句:where條件中列(column)沒有索引可用且無limit限制時會拒絕更新。where條件為常量且無limit限制時會拒絕更新。
- delete語句: ①where條件為常量,②或where條件為空,③或where條件中 列(column)沒有索引可用且無limit限制時拒絕刪除。
測試
打開安全模式進行測試
1.無where的update和delete
delete from t_user
delete from t_user > 1175 - You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column > 時間: 0.001s
update t_user set name='123'
update t_user set name='123' > 1175 - You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column > 時間: 0.001s
2、非索引鍵的delete
delete from t_user where name='123'
delete from t_user where name='123' > 1175 - You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column > 時間: 0.007s
如果delete的where條件不是索引鍵,則必須要添加limit才可以。
delete from t_user where name='123' limit 1
delete from t_user where name='123' limit 1 > Affected rows: 0 > 時間: 0.002s
3.索引鍵的delete
delete from t_user where group_id='123'
delete from t_user where group_id='123' > Affected rows: 0 > 時間: 0s
總結
如果設置了sql_safe_updates=1,那么update語句必須滿足如下條件之一才能執(zhí)行成功
- 使用where子句,并且where子句中列必須為prefix索引列
- 使用limit
- 同時使用where子句和limit(此時where子句中列可以不是索引列)
delete語句必須滿足如下條件之一才能執(zhí)行成功
- 使用where子句,并且where子句中列必須為prefix索引列
- 同時使用where子句和limit(此時where子句中列可以不是索引列)一才能執(zhí)行成功。
到此這篇關于mysql如何能有效防止刪庫跑路的文章就介紹到這了,更多相關mysql 防止刪庫內容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持本站!
版權聲明:本站文章來源標注為YINGSOO的內容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。
關注官方微信