MySQL 5.7 datetime and timestamp set default 0 error

After mysql5.7 version datetime/timestamp default value 0 or 0000-00-00 00:00:00 error
occurs abnormal: Invalid default value for'create_time '
Reason:
sql_mode version after mysql5.7 default use:

mysql> select @@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 

Among them, NO_ZERO_IN_DATE and NO_ZERO_DATE two options prohibit dates and times such as 0000. So in the mysql configuration file, reset sql_mode and remove these two items.
Solution:

# 临时修改
mysql> set global sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' ;
mysql> set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' ;
# 永久修改
# 修改my.cnf文件,在[mysqld]中添加
sql-mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

Author: iscona
link: https: //www.jianshu.com/p/f701ceffc7e1
Source: Jane books
are copyrighted by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/ory001/article/details/108627125