MySQL8数据库运行SQL时候,提示如下错误:
Expression #2 of SELECT list is not in GROUP BY clause *** which is not functionally dependent on columns in GROUP BY clause;this is incompatible with sql_mode=only_full_group_by
大概意思是select的字段,都要在group by里面得有;不过之前旧的版本是可以的;所以通过修改一些配置,也可以做让这样的SQL正确运行
在MySQL命令行,可以通过如下命令查看当前的sql_mode:
select @@GLOBAL.sql_mode;
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6521 Server version: 8.0.27 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> select @@GLOBAL.sql_mode; +-----------------------------------------------------------------------------------------------------------------------+ | @@GLOBAL.sql_mode | +-----------------------------------------------------------------------------------------------------------------------+ | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION | +-----------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
看到sql_mode里面有ONLY_FULL_GROUP_BY,我们只要想办法去掉就可以了
解决办法1
运行如下命令:
set @@GLOBAL.sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
set @@GLOBAL.sql_mode=里面的内容就是刚才查出来的,然后去掉了『ONLY_FULL_GROUP_BY』,注意这个是临时的解决办法,如果MySQL数据库重启后,还是会恢复之前的。
解决办法2
修改MySQL8配置文件(配置文件一般在/etc/my.cnf),在 [mysqld]下面增加:
[mysqld] # mysqld 组下增加如下,具体数值用select @@GLOBAL.sql_mode;得到的结果去掉ONLY_FULL_GROUP_BY+逗号 sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
关于修改sql_mode后无法启动MySQL的问题解决
总结了修改sql_mode无法启动MySQLServer遇到的一些错误和一些解决方法
- 首先修改配置文件/etc/my.cnf里面的sql_mode,注意是放在[mysqld]下面,如果你的配置已经有[mysqld]了,请不要新建[mysqld]
- sql_mode='' 注意有单引号
- sql_mode里面的内容,很多时候,是从往上抄过来的,可能针对MySQL的版本,具体的内容是不一样的,所以要先使用select @@GLOBAL.sql_mode;命令得到你的MySQL当前的sql_mode,然后去掉ONLY_FULL_GROUP_BY(当前也要去掉后面的逗号,多个mode是用逗号隔开的)
文章评论