MySQL sql_mode修改不生效的原因及解决
作者:王文安 时间:2024-01-25 03:32:48
目录
前言
场景模拟
总结
前言
近期多次聊到sql_mode的话题,也是多次遇到相关问题,今天就趁热打铁,再给大家带来一个sql_mode的案例分享。
场景模拟
基于业务敏感性的考虑,下面涉及的表、存储过程等均非真实数据,但并不影响排查过程。
(1)客户侧开发童鞋创建了一个存储过程,该存储过程没有严格遵守group by标准语法
session 1:
mysql> delimiter //
mysql> create procedure test_for_group_by()
-> begin
-> select k,pad,count(*) from test.test group by k;
-> end //
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
(2)客户侧开发童鞋调用该存储过程,报错ERROR 1140;因为当时存储过程比较复杂,改造起来比较麻烦,所以客户侧选择修改sql_mode
session 1:
mysql> call test_for_group_by();
ERROR 1140 (42000): In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'test.test.k'; this is incompatible with sql_mode=only_full_group_by
(3)客户侧修改完sql_mode,再次执行,发现仍然报错ERROR 1140
session 2:
mysql> set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected (0.00 sec)
session 1:
mysql> call test_for_group_by();
ERROR 1140 (42000): In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'test.test.k'; this is incompatible with sql_mode=only_full_group_by
(4)此时想到,修改系统变量,只对新建连接有效,对已有连接不起作用;于是,让客户侧重新建立连接,确认系统变量已生效,再次调用存储过程,但仍然报错ERROR 1140,重复尝试几次都是这个结果
session 3:
mysql> show variables like 'sql_mode';
+---------------+------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value |
+---------------+------------------------------------------------------------------------------------------------------------------------+
| sql_mode | STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+---------------+------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
mysql> call test_for_group_by();
ERROR 1140 (42000): In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'test.test.k'; this is incompatible with sql_mode=only_full_group_by
(5)进一步排查,让客户侧在该会话,执行非标准的group by语句,发现可以正常执行
session 3:
mysql> select user,host,count(*) From mysql.user group by user;
+---------------+-----------+----------+
| user | host | count(*) |
+---------------+-----------+----------+
| mysql.session | localhost | 1 |
| mysql.sys | localhost | 1 |
| root | localhost | 1 |
| rpl_user | % | 1 |
| test | % | 1 |
+---------------+-----------+----------+
5 rows in set (0.00 sec)
(6)继续排查发现,该存储过程的sql_mode,还是包括ONLY_FULL_GROUP_BY,因此执行报错
session 2:
mysql> select routine_catalog,routine_schema,routine_name,routine_type,created,last_altered,sql_mode from routines where routine_name='test_for_group_by';
+-----------------+----------------+-------------------+--------------+---------------------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| routine_catalog | routine_schema | routine_name | routine_type | created | last_altered | sql_mode |
+-----------------+----------------+-------------------+--------------+---------------------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| def | test | test_for_group_by | PROCEDURE | 2020-12-24 12:12:10 | 2020-12-24 12:12:10 | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-----------------+----------------+-------------------+--------------+---------------------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
(7)这里我们也可以知道,系统变量修改只对新建对象有效,对已有对象不生效;解决办法很简单,重建该存储过程即可
session 3:
mysql> drop procedure test_for_group_by;
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter //
mysql> create procedure test_for_group_by()
-> begin
-> select k,pad,count(*) from test.test group by k;
-> end //
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
mysql> call test_for_group_by();
+--------+-------------------------------------------------------------+----------+
| k | pad | count(*) |
+--------+-------------------------------------------------------------+----------+
| 393975 | 35227182905-15234265621-59793845249-15413569710-23749555118 | 1 |
| 495688 | 09512147864-77936258834-40901700703-13541171421-15205431759 | 1 |
| 497896 | 13152283289-69561545685-52868757241-04245213425-69280254356 | 1 |
| 498573 | 43131080328-59298106536-35954612339-97546855884-75769514803 | 1 |
| 500775 | 27590239742-20204899609-34345212327-79811525340-24267764271 | 1 |
| 501885 | 63188288836-92351140030-06390587585-66802097351-49282961843 | 1 |
| 503330 | 01495266405-82925129145-92643983850-90243995398-18709399387 | 1 |
| 503666 | 40929980986-33813039690-13155419391-97985458477-39771362212 | 1 |
| 504353 | 00505722282-72931248925-57037623248-81117963809-88658076981 | 1 |
| 514246 | 21979564480-87492594656-60524686334-78820761788-57684966682 | 1 |
+--------+-------------------------------------------------------------+----------+
10 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
总结
通过这个案例,我们可以知道,修改sql_mode系统变量,只对新建连接和新建对象(主要包括函数和存储过程)有效,对已有连接和已有对象不生效。
来源:https://cloud.tencent.com/developer/inventory/2101/article/1765721
标签:MySQL,sql,mode
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
大家都对vertical-align的各说各话
2008-06-19 12:11:00
![](https://img.aspxhome.com/file/UploadPic/20086/19/2008619121517533s.gif)
Python二叉搜索树与双向链表转换实现方法
2022-08-23 12:46:34
python如何删除文件中重复的字段
2021-09-19 15:43:12
用Python实现一个简单的能够发送带附件的邮件程序的教程
2023-04-08 11:45:48
关于数据库的alter table操作和索引概念
2009-12-15 21:57:00
python网络编程学习笔记(10):webpy框架
2022-07-10 05:43:14
![](https://img.aspxhome.com/file/2023/4/121414_0s.jpg)
Mysql8.0使用窗口函数解决排序问题
2024-01-18 15:24:00
![](https://img.aspxhome.com/file/2023/3/68683_0s.jpg)
python smtplib模块自动收发邮件功能(一)
2021-04-16 07:11:18
![](https://img.aspxhome.com/file/2023/3/107853_0s.png)
SQL Server 2000 作数据库服务器的优点
2009-01-23 13:47:00
Mysql联合查询UNION和Order by同时使用报错问题的解决办法
2024-01-12 18:44:35
Python中的 Set 与 dict
2022-04-09 20:20:33
SQLSERVER记录登录用户的登录时间(自写脚本)
2024-01-27 15:07:24
![](https://img.aspxhome.com/file/2023/7/129867_0s.jpg)
WEB2.0网页制作标准教程(5)head区的其他设置
2007-11-13 13:28:00
防止Access 2000密码被破译的方法
2008-11-20 16:48:00
python爬虫用mongodb的理由
2023-09-27 23:06:40
SQLServer存储过程实现单条件分页
2024-01-14 21:50:01
mysql中的general_log(查询日志)开启和关闭
2024-01-19 01:49:10
![](https://img.aspxhome.com/file/2023/5/91445_0s.png)
python 字典的概念叙述和使用方法
2021-11-08 00:42:06
![](https://img.aspxhome.com/file/2023/2/114482_0s.png)
用实例详解Python中的Django框架中prefetch_related()函数对数据库查询的优化
2024-01-19 08:47:12
![](https://img.aspxhome.com/file/2023/3/112913_0s.jpg)
基于Python中单例模式的几种实现方式及优化详解
2022-10-24 14:20:45