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數據庫不允許插入零日期,插入零日期會拋出錯誤而非警告。
解決方法很容易,臨時解決方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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]下面添加如下列:
1
|
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即可