About mysql database migration, the pit encountered by datetime

Keywords: MySQL Database Navicat SQL

Migration database is often encountered in work.
If you use phpmyadmin to import, you can solve the problem if the database is too small. If it is too large, it will be troublesome. Either it will time out or the file is too large to import.
So I chose to use Navicat For Mysql to copy the database

In this process, I often encounter a problem:

ERROR 1292 (22007): Incorrect datetime value: '1970-01-01 08:00:00' for column 'time' at row 1

After querying the data, it is found that in MySQL, the legal interval of timestamp type is 1970-01-01 00:00:01 - 2038-01-19 03:14:07 UTC, while in storage, the inserted data will be converted to UTC time first, then stored, read, and then converted to your local time. Since my time zone is Zone 8, it becomes 1970-01-01 00:00:00 UTC after conversion, which is illegal. (excerpts from MySQL time type and mode)

MySql strict mode

mysql> mysql> show variables like "%sql_mode%";
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value                                                                                                                                     |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| sql_mode      | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

There are two modes: no zero in date and no zero date. The time format of datetime must be legal

mysql> set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTOO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show variables like "%sql_mode%";                                                             
+---------------+--------------------------------------------------------------------------------------------------------------+
| Variable_name | Value                                                                                                        |
+---------------+--------------------------------------------------------------------------------------------------------------+
| sql_mode      | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+---------------+--------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

Reset the mode, and remove the no? Zero? In? Date and no? Zero? Date

At this time, you should pay special attention to that before copying the database with Navicat for my SQL, you must disconnect the current server and reconnect (you cannot restart Mysql, because after restarting, sql mode will be reset by my.ini and will be restored to the original mode).
Because the operation permissions and restrictions of Mysql connection have been determined and stored after the connection is successful, you need to disconnect and get the operation permissions and restrictions again before the copy is successful, otherwise an error will still be reported

Posted by dannyb785 on Sat, 23 Nov 2019 09:28:42 -0800