MySQL插入查询给出了错误MYSQL已经消失

时间:2014-03-19 05:11:40

标签: php mysql

我在插入查询期间收到错误MYSQL has gone away,即使我不知道是否可以在单个插入查询中插入大约7000条新记录。

insert into table name (col1, col2, col3, ....) 
values 
    ('col1', 'col2', 'col3', ....), 
    ('col1', 'col2', 'col3', ....), 
    ('col1', 'col2', 'col3', ....) .....

我还在mysqld中设置了以下配置:max_allowed_packet = 512M

// php.ini
mysql.connect_timeout = 300
default_socket_timeout = 300

有人可以指导我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

试试这个

if (!mysql_ping ($conn)) {
   //here is the major trick, you have to close the connection (even though its not currently working) for it to recreate properly.
   mysql_close($conn);
   $conn = mysql_connect('localhost','user','pass');
   mysql_select_db('db',$conn);
}

参考:http://www.php.net/manual/en/function.mysql-ping.php

答案 1 :(得分:0)

试试这个http://dev.mysql.com/doc/refman/5.0/en/gone-away.html它有可能的原因。可能你正在运行一个长操作而你300毫秒也不足以满足这一要求。或

你可以设置

 $pdo->exec("SET @@session.wait_timeout = {$waitTimeout}");