||
在用php批量插入mysql数据时,如:
$pdo->beginTransaction(); // also helps speed up your inserts.
$stmt = $pdo->prepare ($sql);
try {
$stmt->execute($dataToInsert);
} catch (PDOException $e){
echo $e->getMessage();
}
$pdo->commit();
会遇到如下异常:
MYSQL异常:General error: 2006 MySQL server has gone away ph
我们查一下这里:
https://dev.mysql.com/doc/refman/5.0/en/gone-away.html
发现有这么一段话:
You can also get these errors if you send a query to the server that is incorrect or too large. If mysqld receives a packet that is too large or out of order, it assumes that something has gone wrong with the client and closes the connection. If you need big queries (for example, if you are working with big BLOB columns), you can increase the query limit by setting the server's max_allowed_packet variable, which has a default value of 1MB. You may also need to increase the maximum packet size on the client end. More information on setting the packet size is given in Section B.5.2.10, “Packet Too Large”.
很显然,timeout肯定不是错误根源了,肯定是这个max_allowed_packet的值问题,我们再看一下:
https://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_max_allowed_packet
发现,最大可以设置为1Gb。
修改:C:ProgramDataMySQLMySQL Server 5.6my.ini
max_allowed_packet=4M
改为:
max_allowed_packet=100M
或者你认为可以接受的最大的包大小,我们最大不到10MB,所以设置为100MB足够了。
加我私人微信,交流技术。
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-5-12 15:32
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社