几行命令教你如何解决MySQL8.0 sql_mode=only_full_group_by问题

问题描述

SpringBoot部署web项目,查询出现报错
数据库版本MySQL 8.0.21

报错提示

java.sql.SQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'dev.*******' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
        at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:370) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]

解决办法

在MySQL配置文件中指定sql_mode
需要注意的是每台机器的配置文件目录不一样,你需要自己找到你的配置文件
我的配置文件在/etc/my.cnf.d/mysql-server.cnf
几行命令教你如何解决MySQL8.0 sql_mode=only_full_group_by问题_第1张图片
你需要找到[mysqld],在下面添加一行

sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

几行命令教你如何解决MySQL8.0 sql_mode=only_full_group_by问题_第2张图片
然后需要重启mysql服务
我使用systemctl restart mysqld来重启MySQL服务

问题解决

你可能感兴趣的:(数据库,mysql,java,spring)