主頁 > 知識庫 > MySQL sql_mode修改不生效的原因及解決

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

熱門標(biāo)簽:大豐地圖標(biāo)注app 400電話辦理服務(wù)價格最實(shí)惠 html地圖標(biāo)注并導(dǎo)航 400電話變更申請 催天下外呼系統(tǒng) 南太平洋地圖標(biāo)注 呂梁外呼系統(tǒng) 北京金倫外呼系統(tǒng) 武漢電銷機(jī)器人電話

前言

近期多次聊到sql_mode的話題,也是多次遇到相關(guān)問題,今天就趁熱打鐵,再給大家?guī)硪粋€sql_mode的案例分享。

場景模擬

基于業(yè)務(wù)敏感性的考慮,下面涉及的表、存儲過程等均非真實(shí)數(shù)據(jù),但并不影響排查過程。

(1)客戶側(cè)開發(fā)童鞋創(chuàng)建了一個存儲過程,該存儲過程沒有嚴(yán)格遵守group by標(biāo)準(zhǔn)語法

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)客戶側(cè)開發(fā)童鞋調(diào)用該存儲過程,報(bào)錯ERROR 1140;因?yàn)楫?dāng)時存儲過程比較復(fù)雜,改造起來比較麻煩,所以客戶側(cè)選擇修改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)客戶側(cè)修改完sql_mode,再次執(zhí)行,發(fā)現(xiàn)仍然報(bào)錯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)此時想到,修改系統(tǒng)變量,只對新建連接有效,對已有連接不起作用;于是,讓客戶側(cè)重新建立連接,確認(rèn)系統(tǒng)變量已生效,再次調(diào)用存儲過程,但仍然報(bào)錯ERROR 1140,重復(fù)嘗試幾次都是這個結(jié)果

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)進(jìn)一步排查,讓客戶側(cè)在該會話,執(zhí)行非標(biāo)準(zhǔn)的group by語句,發(fā)現(xiàn)可以正常執(zhí)行

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)繼續(xù)排查發(fā)現(xiàn),該存儲過程的sql_mode,還是包括ONLY_FULL_GROUP_BY,因此執(zhí)行報(bào)錯

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)這里我們也可以知道,系統(tǒng)變量修改只對新建對象有效,對已有對象不生效;解決辦法很簡單,重建該存儲過程即可

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)

總結(jié)

通過這個案例,我們可以知道,修改sql_mode系統(tǒng)變量,只對新建連接和新建對象(主要包括函數(shù)和存儲過程)有效,對已有連接和已有對象不生效。

以上就是MySQL sql_mode修改不生效的原因及解決的詳細(xì)內(nèi)容,更多關(guān)于MySQL sql_mode修改不生效的資料請關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • 詳解MySQL的sql_mode查詢與設(shè)置
  • MySQL中SQL Mode的查看與設(shè)置詳解
  • mysql中的sql_mode模式實(shí)例詳解
  • Django2 連接MySQL及model測試實(shí)例分析
  • 關(guān)于MySQL的sql_mode合理設(shè)置詳解
  • MySQL關(guān)于sql_mode解析與設(shè)置講解
  • MySQL5.7中的sql_mode默認(rèn)值帶來的坑及解決方法
  • MySql版本問題sql_mode=only_full_group_by的完美解決方案
  • 解決MySQL 5.7.9版本sql_mode=only_full_group_by問題
  • Mysql之SQL Mode用法詳解
  • mysql sql_mode="" 的作用說明
  • MySQL sql_mode的使用詳解

標(biāo)簽:龍巖 無錫 徐州 迪慶 南充 自貢 西寧 麗水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL sql_mode修改不生效的原因及解決》,本文關(guān)鍵詞  MySQL,sql,mode,修改,不,生效,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySQL sql_mode修改不生效的原因及解決》相關(guān)的同類信息!
  • 本頁收集關(guān)于MySQL sql_mode修改不生效的原因及解決的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章