今天在本地环境使用Navicat进行MySQL数据备份恢复的时候,出现了错误,导入没有顺利完成,就出现了MySQL server has gone away的错误。
逐一判断:
1、服务器运行状态正常(排除)
2、mysql是否连接超时,备份恢复的时候,大概进行了8秒,就出现了这个错误
mysql>show global variables like ‘%timeout’;
发现列表中都没有出现这个时间范围的限制(排除)
3、是否语句限制超过范围了
mysql>show global variables like ‘max_allowed_packet’;
发现,只有1M=1024*1024
导入的查询数据量是明显超过这个值的,于是临时设置(只对当前的server进程有效)
mysql>set global max_allowed_packet=1024*1024*32;
重新进行导入备份数据,正常了。
如果需要每次server启动的时候,都指定max_allowed_packet的值
可以直接修改
my.cnf
#如果有这个key,则直接修改值;如果没有这个key,则添加
max_allowed_packet=32M
关于max_allowed_packet的官方说明文档如下:
A communication packet is a single SQL statement sent to the MySQL server, a single row that is sent to the client, or a binary log event sent from a master replication server to a slave.
The largest possible packet that can be transmitted to or from a MySQL 5.1 server or client is 1GB.
When a MySQL client or the mysqld server receives a packet bigger than max_allowed_packet bytes, it issues an ER_NET_PACKET_TOO_LARGE error and closes the connection. With some clients, you may also get a Lost connection to MySQL server during query error if the communication packet is too large.
Both the client and the server have their own max_allowed_packet variable, so if you want to handle big packets, you must increase this variable both in the client and in the server.
If you are using the mysql client program, its default max_allowed_packet variable is 16MB. To set a larger value, start mysql like this:
shell> mysql –max_allowed_packet=32M
That sets the packet size to 32MB.
The server’s default max_allowed_packet value is 1MB. You can increase this if the server needs to handle big queries (for example, if you are working with big BLOB columns). For example, to set the variable to 16MB, start the server like this:
shell> mysqld –max_allowed_packet=16M
You can also use an option file to set max_allowed_packet. For example, to set the size for the server to 16MB, add the following lines in an option file:
[mysqld]
max_allowed_packet=16M
It is safe to increase the value of this variable because the extra memory is allocated only when needed. For example, mysqld allocates more memory only when you issue a long query or when mysqld must return a large result row. The small default value of the variable is a precaution to catch incorrect packets between the client and server and also to ensure that you do not run out of memory by using large packets accidentally.
You can also get strange problems with large packets if you are using large BLOB values but have not given mysqld access to enough memory to handle the query. If you suspect this is the case, try adding ulimit -d 256000 to the beginning of the mysqld_safe script and restarting mysqld.
http://dev.mysql.com/doc/refman/5.1/en/packet-too-large.html