亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

MySQL sql_mode修改不生效的原因及解決

瀏覽:3日期:2023-10-03 14:35:37
前言

近期多次聊到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系統變量,只對新建連接和新建對象(主要包括函數和存儲過程)有效,對已有連接和已有對象不生效。

以上就是MySQL sql_mode修改不生效的原因及解決的詳細內容,更多關于MySQL sql_mode修改不生效的資料請關注好吧啦網其它相關文章!

標簽: MySQL 數據庫
相關文章:
主站蜘蛛池模板: 欧美午夜在线播放 | 黄色片网站在线免费观看 | 日批视频网址免费观看 | 欧美日韩一区二区中文字幕视频 | 老汉tv永久视频福利在线观看 | 国产亚洲福利精品一区二区 | 在线亚洲精品视频 | 狼人青草久久网尹人 | 亚洲六月婷婷 | 国产黄色视屏 | 午夜亚洲视频 | 色屁屁www影院入口免费观看 | 成年女美黄网站大全免费播放 | 亚洲国产精品aa在线看 | 久草资源在线播放 | 久久一区二区免费播放 | 丁香婷婷激情网 | 特级女人十八毛片a级 | 欧美一级录像 | 亚洲 欧美 国产 中文 | 1769国产精品一区2区 | 亚洲高清在线视频 | 黄色免费网站在线 | 午夜影院在线观看视频 | 在线成人免费观看国产精品 | 99久久精品国产国产毛片 | 在线播放国产一区 | 成人激情视频在线观看 | 国产v国产v片大片线观看网站 | 亚洲黄色网址大全 | 欧美精品免费线视频观看视频 | 亚洲综合欧美在线 | 国产精在线 | 在线97视频| 黄色一及 | 国产一二三四区在线观看 | 中文字幕第13亚洲另类 | 免费亚洲一区 | 49pao强力免费打造在线高清 | 瑟妃19禁福利视频在线看mp4 | 亚洲欧美制服丝袜一区二区三区 |