MySQL Mode 二三事

MySQL Mode 二三事


这两天一直忙着迁移服务器,数据库,赶上这样或者那样的小故障。 在这里也随笔记一下。html

1. 故事背景

因为部分阿里云RDS数据库过时,再加上相关费用问题,全部公司决定只有线上环境使用阿里RDS数据库, 其余环境都自搭建数据库。mysql

线上阿里云数据库 Mysql 5.6 自建服务器数据库 Mysql 5.7sql


2. 问题

迁移完测试的数据库后, 小小测试一下, 大概没有什么问题。 下午的时候产品找过了说某些个功能不能用了, 一查日志。数据库

Expression #1 of SELECT list is not in GROUP BY clause 
and contains nonaggregated column '1066export.ebay_order_items.TransactionID' 
which is not functionally dependent on columns in GROUP BY clause; 
this is incompatible with sql_mode=only_full_group_by

一看sql(此处来源于网络), 你们也就知道是什么问题。服务器

select libelle,credit_initial,disponible_v,sum(montant) as montant 
FROM fiche,annee,type where type.id_type=annee.id_type and annee.id_annee=fiche.id_annee 
and annee = year(current_timestamp) GROUP BY libelle order by libelle asc

而后一查官网手册, 有以下一段话, 大体的意思是自 MySQL 5.7.5 以后, 默认 ONLY_FULL_GROUP_BY 模式是开启的, ONLY_FULL_GROUP_BY 必须搭配汇集函数才能使用。网络

ONLY_FULL_GROUP_BY : Reject queries for which the select list, 
HAVING condition, or ORDER BY list refer to nonaggregated columns 、that are neither named in the GROUP BY clause nor are functionally dependent on 
(uniquely determined by) GROUP BY columns.

As of MySQL 5.7.5, the default SQL mode includes ONLY_FULL_GROUP_BY.
(Before 5.7.5, MySQL does not detect functional dependency 
and ONLY_FULL_GROUP_BY is not enabled by default

3. 解决方案

3.1 修改SQL

因为刚接手这代码不久,且已经在线上运行了一年多, 修改sql有必定的风险。 因此打算只修改测试服务器数据库的 Mode函数

3.2 修改MySQL Mode

3.2.1 先查询获得当钱使用的 mode

SELECT @@sql_mode;

获得测试

ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

3.2.2 修改配置文件 /etc/my.cnf (可能有些服务器不是这个路径)

加入以下配置this

[mysqld]
sql_mode = "STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

3.2.3 重启mysql

service mysqld restart

4. 检查

修改完后, 测试阿里云

相关文章
相关标签/搜索