mysql数据库是可以开启安全模式,不过默认情况下,安全模式不开启的,下面就来说说什么是mysql的安全模式
不知道小伙伴们是否有过维护的数据库表业务数据被人或者因为程序bug导致全表更新,全表删除的痛苦经历,恢复业务数据真的是一个精细活,尤其与交易和钱相关的数据,必须恢复成和原来一模一样,那能不能在数据库层面架起最后一道安全堡垒,拒绝全表更新,全表删除的非法操作呢,答案是有的,在mysql中sql_safe_updates可以完美解决这个问题,下面就来给大家演示一下实际效果
sql_safe_updates默认是不开启的
mysql> show variables like 'sql_safe_updates'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | sql_safe_updates | OFF | +------------------+-------+ 1 row in set (0.01 sec)
复制
现在就开启这个参数,如果要永久生效,需要将参数添加到数据库配置文件(my.cnf)中
mysql> set global sql_safe_updates=1; Query OK, 0 rows affected (0.00 sec)
复制
需要重新连接一下数据库,才会生效
mysql> show variables like 'sql_safe_updates'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | sql_safe_updates | ON | +------------------+-------+ 1 row in set (0.00 sec)
复制
下面就开始用表进行测试
mysql> create table t_test10 (id char(10),name char(20), primary key(id)); Query OK, 0 rows affected (0.29 sec) 插入语句 insert into t_test10 values('1','test1'); insert into t_test10 values('2','test2'); insert into t_test10 values('3','test3'); insert into t_test10 values('4','test4'); insert into t_test10 values('5','test5'); insert into t_test10 values('6','test6'); insert into t_test10 values('7','test7'); insert into t_test10 values('8','test8'); mysql> select * from t_test10; +----+-------+ | id | name | +----+-------+ | 1 | test1 | | 2 | test2 | | 3 | test3 | | 4 | test4 | | 5 | test5 | | 6 | test6 | | 7 | test7 | | 8 | test8 | +----+-------+ 8 rows in set (0.00 sec)
复制
测试一下全表删除
mysql> delete from t_test10; 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 t_test10 where 1=1; 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> update t_test10 set name='test'; 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> update t_test10 set name='test' where 1=1; 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> update t_test10 set name='test' where name='test1'; 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 t_test10 where name='test2'; 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数据库安全策略拦截了呢,这是因为name列没有索引
mysql> show create table t_test10\G; *************************** 1. row *************************** Table: t_test10 Create Table: CREATE TABLE `t_test10` ( `id` char(10) NOT NULL, `name` char(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) ERROR: No query specified 在name列上加索引 mysql> alter table t_test10 add index idx_t_test10_name (name); Query OK, 0 rows affected (0.44 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table t_test10\G; *************************** 1. row *************************** Table: t_test10 Create Table: CREATE TABLE `t_test10` ( `id` char(10) NOT NULL, `name` char(20) DEFAULT NULL, PRIMARY KEY (`id`), KEY `idx_t_test10_name` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec)
复制
重新测试一下正常删除和更新语句
mysql> update t_test10 set name='test' where name='test1'; Query OK, 1 row affected (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> delete from t_test10 where name='test2'; Query OK, 1 row affected (0.01 sec)
复制
全部成功了,开启sql_safe_updates安全模式之后,要想正常删除或者更新,需要满足2个条件
- delete,update必须带条件,或者加limit进行过滤
- 所带条件必须走索引
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
墨天轮个人数说知识点合集
JiekeXu
441次阅读
2025-04-01 15:56:03
MySQL数据库当前和历史事务分析
听见风的声音
426次阅读
2025-04-01 08:47:17
MySQL 生产实践-Update 二级索引导致的性能问题排查
chengang
388次阅读
2025-03-28 16:28:31
MySQL 30 周年庆!MySQL 8.4 认证免费考!这次是认真的。。。
严少安
363次阅读
2025-04-25 15:30:58
【MySQL 30周年庆】MySQL 8.0 OCP考试限时免费!教你免费领考券
墨天轮小教习
360次阅读
2025-04-25 18:53:11
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
356次阅读
2025-04-17 17:02:24
MySQL 9.3 正式 GA,我却大失所望,新特性亮点与隐忧并存?
JiekeXu
354次阅读
2025-04-15 23:49:58
3月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
331次阅读
2025-04-15 14:48:05
云和恩墨杨明翰:安全生产系列之MySQL高危操作
墨天轮编辑部
307次阅读
2025-03-27 16:45:26
openHalo问世,全球首款基于PostgreSQL兼容MySQL协议的国产开源数据库
严少安
294次阅读
2025-04-07 12:14:29