阿良の小屋

解决 wordpress 的 comment_date 默认值无效的问题

2026-02-17

安装好了 wordpress,今天 MySQL 数据库 ALTER TABLE wp_comments 时出现了报错:

Error Code: 1067. Invalid default value for ‘comment_date’

the lead developer of WordPress wrote:

“WordPress just pretty simply does not support strict mode.”

So the solution is to disable strict mode on MySQL. It’s a pity.
In the WordPress code, you can find includes/wp-db.php which lists:

/**
* A list of incompatible SQL modes.
*
* @since 3.9.0
* @var array
*/
protected $incompatible_modes = array(
'NO_ZERO_DATE',
'ONLY_FULL_GROUP_BY',
'STRICT_TRANS_TABLES',
'STRICT_ALL_TABLES',
'TRADITIONAL',
);

Set the sql_mode option in your my.cnf file to omit those sql modes. You can keep other sql modes, like the ones in the default set of sql modes for MySQL 5.7:

[mysqld]
sql-mode = "ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

注意:配置文件里 sql-mode 中间是连字符,并非下划线。后面的值用双引号包裹。

You can also change the global sql mode set without needing to restart the MySQL Server(重启会失效):

mysql> SET GLOBAL sql_mode = 'ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

附:查询 sql_mode 的方法:

-- 查看当前连接的数据库模式
SELECT @@SESSION.sql_mode;

-- 查看全局连接的数据库模式
SELECT @@GLOBAL.sql_mode;
Tags: MySQL

扫描二维码,分享此文章