mysql5.7版本因为sql_mode设置导致的问题
因为重新安装了环境,想把之前的数据库导入到新库里面,
结果就报了如下错误:
[err] 1055 - expression #1 of order by clause is not in group by clause and contains nonaggregated column ‘information_schema.profiling.seq’ which is not functionally dependent on columns in group by clause; this is incompatible with sql_mode=only_full_group_by”
尝试解决
方法一
登录mysql,设置sql_mode
use mysql; set sql_mode=‘no_engine_substitution,strict_trans_tables';
然而并没有什么卵用
方法二
原来上边的设置方法只是会话级别的设置,真正让他生效得修改配置文件:
vi /etc/my.cnf
在[mysqld]配置项下面加上:
sql_mode=no_unsigned_subtraction,no_engine_substitution
重启服务,
再进入mysql
show variables like ‘sql_mode';
设置成功啦!!
关于sql_mode
官方文档:https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html
重要的几项配置简单介绍,更多详细介绍移步官方文档
- no_engine_substitution 使用 alter table或create table 指定 engine 时, 需要的存储引擎被禁用或未编译,该如何处理。
- 启用no_engine_substitution时,那么直接抛出错误;不设置此值时,create用默认的存储引擎替代,atler不进行更改,并抛出一个 warning .
- strict_trans_tables 设置它,表示启用严格模式。
注意: strict_trans_tables 不是几种策略的组合,单独指 insert、update出现少值或无效值该如何处理:
- 例如:
如果我们建表的时候不严格,比如字段设置是not null,然而并没有给字段设置默认值,在插入语句的时候,这个字段我们忽略,没有赋值,恰好这个时候mysql的sql_mode是严格模式,即启用了strict_trans_tables,就会报字段没有默认值的错误。
或者说,我们将空字符串赋值给int类型的字段,在严格模式下也会报同样的错误。如果sql_mode没有配置strict_trans_tables则不会报错
only_full_group_by 对于group by聚合操作,如果在select中的列、having或者order by子句的列,没有在group by中出现,那么这个sql是不合法的。因为不在 group by 的列查出来展示会有矛盾。
- 例如:
select id from conf_merchant group by merchant_id;
这条sql在only_full_group_by模式下就会报错。
1055 - expression #1 of select list is not in group by clause and contains nonaggregated column ‘iapw.conf_merchant.market_id’ which is not functionally dependent on columns in group by clause; this is incompatible with sql_mode=only_full_group_by
如果sql_mode 没有配置only_full_group_by,就不会报错。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论