解决ERROR 1231 (42000): Variable 'time_zone' can't

MySQL根据配置文件会限制Server接受的数据包大小。有时候大的插入和更新会受 max_allowed_packet 参数限制,导致写入或者更新失败。(比方说导入数据库,数据表)

mysql 数据库在迁移或还原数据过程中出现的如下报错:

ERROR 1231 (42000): Variable 'time_zone' can't be set to the value of 'NULL'
ERROR 1231 (42000): Variable 'sql_mode' can't be set to the value of 'NULL'
ERROR 1231 (42000): Variable 'foreign_key_checks' can't be set to the value of 'NULL'
ERROR 1231 (42000): Variable 'unique_checks' can't be set to the value of 'NULL'
ERROR 1231 (42000): Variable 'character_set_client' can't be set to the value of 'NULL'
Query OK, 0 rows affected (0.00 sec)
ERROR 1231 (42000): Variable 'collation_connection' can't be set to the value of 'NULL'
ERROR 1231 (42000): Variable 'sql_notes' can't be set to the value of 'NULL'

解决办法:

修改mysql配置文件:max_allowed_packet=1024M

查看目前配置:

代码如下:
show VARIABLES like '%max_allowed_packet%';


显示的结果为:

代码如下:
+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_allowed_packet | 1048576 |
+--------------------+---------+

以上说明目前的配置是:1M

修改方法

1、修改配置文件

可以编辑my.cnf来修改(windows下my.ini),在[mysqld]段或者mysql的server配置段进行修改。

代码如下:
max_allowed_packet = 20M


如果找不到my.cnf可以通过

代码如下:
mysql --help | grep my.cnf


去寻找my.cnf文件。
linux下该文件在/etc/下。

重启mysql:

1、使用 service 启动:service mysqld restart
2、使用 mysqld 脚本启动:/etc/inint.d/mysqld restart

查看下max_allowed_packet是否编辑成功

注意:该值设置过小将导致单个记录超过限制后写入数据库失败,且后续记录写入也将失败。

原文地址:https://www.cnblogs.com/niuben/p/12937216.html