在 Navicat for MySQL 中,将表从一个数据库复制至另一个数据库时,报错:[ERR] 1292 – Incorrect datetime value: ‘0000-00-00 00:00:00’ for column ‘created_at_gmt’ at row 1(在 MySQL 8.0 中)

1、在 Navicat for MySQL 中,将表从一个数据库复制至另一个数据库时,报错:[ERR] 1292 – Incorrect datetime value: ‘0000-00-00 00:00:00’ for column ‘created_at_gmt’ at row 1。如图1

图1

[DTF] Drop table: `attribute_names`
[DTF] Create table: `attribute_names`
[DTF] Begin transaction on target server
[DTF] Start transfer data for table: `attribute_names`
[ERR] 1292 - Incorrect datetime value: '0000-00-00 00:00:00' for column 'created_at_gmt' at row 1
[DTF] End transaction on target server
[DTF] Finished unsuccessfully

2、执行:SELECT VERSION(),发现两个数据库软件的版本不一致。分别为:5.7.19-log 与 8.0.23-0ubuntu0.20.04.1。如图2

图2

3、由于目标数据库是基于 Homestead 部署,编辑 Homestead.yaml,在 「features」 设置 中,添加:mysql8: false。如图3

图3

features:
    - mysql: true
    - mariadb: false
    - postgresql: false
    - ohmyzsh: false
    - webdriver: false
    - mysql8: false

4、先销毁虚拟机:vagrant destroy –force,再 启动 vagrant:vagrant up。仍然是 MySQL 8,原因在于 内置软件 已经为 MySQL 8 了。如图4

图4

5、执行:SHOW VARIABLES LIKE ‘sql_mode’;,查看 SQL 模式。5.7 下为:STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION。8.0 下为:ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION。如图5

图5

6、重新设置 8.0 下的 SQL 模式,删除掉:NO_ZERO_IN_DATE,NO_ZERO_DATE,保留下其他的。然后关闭连接,再打开连接,查看 SQL 模式,已经生效。如图6

图6

set GLOBAL sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

7、再次 Ctrl + C,然后在目标数据库中 Ctrl + V,不再报错。如图7、图8

图7

图8

永夜