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

默認(rèn)是OFF狀態(tài),將狀態(tài)設(shè)置為ON即可:
set sql_safe_updates=1;//打開set sql_safe_updates=0;//關(guān)閉
設(shè)置為ON之后
- update語句:where條件中列(column)沒有索引可用且無limit限制時(shí)會(huì)拒絕更新。where條件為常量且無limit限制時(shí)會(huì)拒絕更新。
- delete語句: ①where條件為常量,②或where條件為空,③或where條件中 列(column)沒有索引可用且無limit限制時(shí)拒絕刪除。
測試
打開安全模式進(jìn)行測試
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 > 時(shí)間: 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 > 時(shí)間: 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 > 時(shí)間: 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 > 時(shí)間: 0.002s
3.索引鍵的delete
delete from t_user where group_id='123'
delete from t_user where group_id='123' > Affected rows: 0 > 時(shí)間: 0s
總結(jié)
如果設(shè)置了sql_safe_updates=1,那么update語句必須滿足如下條件之一才能執(zhí)行成功
- 使用where子句,并且where子句中列必須為prefix索引列
- 使用limit
- 同時(shí)使用where子句和limit(此時(shí)where子句中列可以不是索引列)
delete語句必須滿足如下條件之一才能執(zhí)行成功
- 使用where子句,并且where子句中列必須為prefix索引列
- 同時(shí)使用where子句和limit(此時(shí)where子句中列可以不是索引列)一才能執(zhí)行成功。
到此這篇關(guān)于mysql如何能有效防止刪庫跑路的文章就介紹到這了,更多相關(guān)mysql 防止刪庫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用mysqldump導(dǎo)入數(shù)據(jù)和mysqldump增量備份(mysqldump使用方法)
mysqldump常用于MySQL數(shù)據(jù)庫邏輯備份,下面看實(shí)例吧2013-12-12
mysql查詢昨天 一周前 一月前 一年前的數(shù)據(jù)
這篇文章主要介紹了mysql查詢昨天 一周前 一月前 一年前的數(shù)據(jù)的方法,需要的朋友可以參考下2014-05-05
CentOS7.3下mysql 8.0.13安裝配置方法圖文教程
這篇文章主要為大家詳細(xì)介紹了CentOS7.3下mysql 8.0.13安裝配置方法圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
MYSQL數(shù)據(jù)表基本操作之創(chuàng)建+查看+修改+刪除操作方法
本文將介紹如何在MySQL數(shù)據(jù)庫中進(jìn)行數(shù)據(jù)表的創(chuàng)建、查看、修改和刪除操作,并討論一些常見的注意事項(xiàng)及防止誤操作的策略,通過這些基礎(chǔ)操作,您將能夠更高效地進(jìn)行數(shù)據(jù)庫設(shè)計(jì)和管理工作,感興趣的朋友一起看看吧2025-04-04
MySQL中必須了解的13個(gè)關(guān)鍵字總結(jié)
這篇文章主要為大家詳細(xì)介紹了MySQL中必須了解學(xué)會(huì)的13個(gè)關(guān)鍵字,文中的示例代碼簡潔易懂,對(duì)我們掌握MySQL有一定的幫助,需要的可以了解下2023-09-09

