【已解决】mysql 5.7.x 设置sqlmode 解决 Invalid default value for…

原创文章,转载请注明: 转载自勤奋的小青蛙
本文链接地址: 【已解决】mysql 5.7.x 设置sqlmode 解决 Invalid default value for…

mysql

mysql如果从 5.5 升级到 5.7.x,可能在导入数据的时候,会发现如下错误:

1067 – Invalid default value for

产生这个报错的原因主要是Mysql 5.7默认sql模式为严格模式,就是sql语法必须非常规范,不允许出现空值等不规范的操作。

其中sqlmode中有一项:NO_ZERO_DATE,在非严格模式下,可以插入形如“0000-00-00 00:00:00”的非法日期,MySQL数据库仅抛出一个警告。而启用该选项后,MySQL数据库不允许插入零日期,插入零日期会抛出错误而非警告。

解决方法很容易,临时解决方法:

mysql -u root -p //登陆mysql
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 26
Server version: 5.7.21 Homebrew
 
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
 
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> set global sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
-> ;
Query OK, 0 rows affected, 1 warning (0.01 sec)
 
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
 
mysql> exit;

永久解决方法:

Linux:

可以直接修改my.cnf文件

例如:vim /etc/my.cnf

[mysqld]下面添加如下列:

sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

Windows:

修改方法同样,寻找的配置文件名和目录不同
如当前目录为mysql安装目录C:\Program Files\MySQL\MySQL Server 5.7

复制一份my-default.ini文件,命名为my.ini
修改最下面配置的为这个
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
保存后,重启mysql即可

原创文章,转载请注明: 转载自勤奋的小青蛙
本文链接地址: 【已解决】mysql 5.7.x 设置sqlmode 解决 Invalid default value for…

文章的脚注信息由WordPress的wp-posturl插件自动生成



|2|left
打赏

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: