MySQL中参数sql_safe_updates在生产环境的使用详解

所属分类: 数据库 / Mysql 阅读数: 1192
收藏 0 赞 0 分享

前言

在应用 BUG或者 DBA误操作的情况下,会发生对全表进行更新:update delete 的情况。MySQL提供 sql_safe_updates 来限制次操作。

set sql_safe_updates = 1;

设置之后,会限制update delete 中不带 where 条件的SQL 执行,较严格。会对已有线上环境带来不利影响。对新系统、应用做严格审核,可以确保不会发生全表更新的问题。

CREATE TABLE working.test01 (id INT NOT NULL AUTO_INCREMENT,NAME VARCHAR(20),age INT,gmt_created DATETIME,PRIMARY KEY(id));

 insert into test01(name,age,gmt_created) values('xiaowang',2,now());
 insert into test01(name,age,gmt_created) values('huahua',5,now()); 
 insert into test01(name,age,gmt_created) values('gougou',9,now()); 
 insert into test01(name,age,gmt_created) values('heihei',12,now()); 
 insert into test01(name,age,gmt_created) values('baibai',134,now()); 

# 过滤字段上没有索引
update
update test01 set name = 'xiaoxiao' where age = 2 ;
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
# 全表更新
update test01 set name = 'xiaoxiao';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
# 加入limit的更新
update test01 set name = 'xia' limit 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

# 新增索引
create index idx_age on test01(age);

update test01 set name = 'xiaoxiao' where age = 2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

update test01 set name = 'hhh' where age = 9 limit 10;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

alter table test01 drop index idx_age;
create index idx_age_name on test01(age,name);


update test01 set age= 100 where name = 'hhh';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

update test01 set age= 100 where name = 'hhh' limit 10;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

由此,update 时,在没有 where 条件或者where 后不是索引字段时,必须使用 limit ;在有 where 条件时,为索引字段

最近在工作中又发现了一个问题,mysql sql_safe_updates 不支持子查询的更新。

考虑到开发人员有时候不小心误更新数据,要求线上库的 MySQL 实例都设置 sql_safe_updates=1 来避免没有索引的 update、delete。

结果有一天开发发现下面的一个SQL 没法正确执行:

update t1 set col2=1 where key1 in (select col2 from t2 where key2='ABcD');

错误如下:

ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

也就是说没法对没有走到索引的where条件进行更新。搜索了下发现,的确不行。及时 key1 和key2 分别是 t1、t2 的索引[我换成主键都不行] 。说明是不支持子查询的update。

google 了一下发现人家也问过这个问题。。

http://stackoverflow.com/questions/24314830/query-not-getting-executed-if-supplied-a-nested-sub-query

最后解决方法:

1)修改 session 级别的参数: set sql_safe_updates=0; 执行 update 操作。退出终端。

2)程序处理:先 select col2 from t2 where key2='ABcD' 获取数据,然后循环处理结果,并用 update t1 set col2=1 where key1=? 来批量更新过。建议还是用程序处理,临时修改变量不是长久之计。

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。  

更多精彩内容其他人还在看

mysql多表连接查询实例讲解

本篇文章中给大家通过实例代码讲述了mysql多表连接查询的方法,有需要的朋友们可以参考学习下。
收藏 0 赞 0 分享

MySQL设置global变量和session变量的两种方法详解

这篇文章主要介绍了MySQL设置global变量和session变量的两种方法,每种方法给大家介绍的非常详细 ,需要的朋友可以参考下
收藏 0 赞 0 分享

8种手动和自动备份MySQL数据库的方法

作为流行的开源数据库管理系统,MySQL的使用者众多,为了维护数据安全性,数据备份是必不可少的。本文就为大家介绍几种适用于企业的数据备份方法,需要的朋友可以参考下
收藏 0 赞 0 分享

使用JDBC连接Mysql数据库会出现的问题总结

这篇文章主要给大家介绍了关于使用JDBC连接Mysql数据库会出现的问题的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Ubuntu中MySQL的参数文件my.cnf示例详析

这篇文章主要给大家介绍了关于Ubuntu中MySQL的参数文件my.cnf的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用mysql具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

解决启动MongoDB错误:error while loading shared libraries: libstdc++.so.6:cannot open shared object file:

本文提供了解启动MongoDB时提示:error while loading shared libraries: libstdc++.so.6: cannot open shared object file: 错误的解决方案
收藏 0 赞 0 分享

PHP定时备份MySQL与mysqldump语法参数详解

本文为大家介绍了PHP利用mysqldump命令定时备份MySQL与mysqldump语法参数大全以及定时备份的PHP实例代码
收藏 0 赞 0 分享

定时备份 Mysql并上传到七牛的方法

常见的 MySQL 数据备份方式有,直接打包复制对应的数据库或表文件(物理备份)、mysqldump 全量逻辑备份、xtrabackup 增量逻辑备份等。这篇文章主要介绍了定时备份 MySQL 并上传到七牛 ,需要的朋友可以参考下
收藏 0 赞 0 分享

MySQL锁(表锁,行锁,共享锁,排它锁,间隙锁)使用详解

本文全面讲解了MySQL中锁包括表锁,行锁,共享锁,排它锁,间隙锁的详细使用方法
收藏 0 赞 0 分享

MySQL中的排序函数field()实例详解

这篇文章主要给大家介绍了关于MySQL中排序函数field()的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多