5.7之sql_model

问题发生背景

今天在部署项目的时候发现,测试后台接口,直接报 500,仔细一看原来是操作数据库的时候报错了,在本地测试的时候是没遇到类似的问题,数据库的版本是一样的,后面查找资料,说是 MySQL 5.7 默认的 sql_model 配置是有 only_full_group_by,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 分组查询报错
[Err] 1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'leping.township_info.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

# 查看当前MySQL sql_mode 配置
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.18 |
+-----------+
1 row in set (0.00 sec)

mysql> select @@sql_mode;
+--------------------------------------------+
| @@sql_mode |
+--------------------------------------------+
| STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+--------------------------------------------+
1 row in set (0.00 sec)

only_full_group_by :使用这个就是使用和oracle一样的group 规则, select的列都要在group中,或者本身是聚合列(SUM,AVG,MAX,MIN) 才行

解决的办法就是将这个 sql_model 的 only_full_group_by 参数去除
1
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
  • window server 服务器,直接找到 my.ini 配置文件,如果配置中没有对应的配置,先添加 sql_model ,有则直接移除 only_full_group_by
  • mac vim /etc/my.cof
  • centos 7 vim /etc/my.cof