PHP:MySQL 服务器已经消失

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11583352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:48:59  来源:igfitidea点击:

PHP: MySQL server has gone away

phpmysql

提问by Lee

I know this question has been asked numerous times but I've tried everything.

我知道这个问题已经被问过很多次了,但我已经尝试了一切。

I have a PHP script that iterates through thousands of images, performs resizing if necessary and then inserts into the database.

我有一个 PHP 脚本,它遍历数千张图像,必要时执行调整大小,然后插入到数据库中。

I'm getting this error message:

我收到此错误消息:

Warning: mysql_query() [function.mysql-query]: MySQL server has gone away in C:\Utilities\server\htdocs\newGen\index.php on line 105

Warning: mysql_query() [function.mysql-query]: Error reading result set's header in C:\Utilities\server\htdocs\newGen\index.php on line 105

警告:mysql_query() [function.mysql-query]:MySQL 服务器已在 C:\Utilities\server\htdocs\newGen\index.php 中第 105 行消失

警告:mysql_query() [function.mysql-query]:在第 105 行读取 C:\Utilities\server\htdocs\newGen\index.php 中的结果集标题时出错

I'm using Xampplite and so some parts of the php.ini are missing that others have used to solve the problem because they're not using the lite version.

我正在使用 Xampplite,因此 php.ini 的某些部分缺少其他人用来解决问题的部分,因为他们没有使用 lite 版本。

I'm certain the problem is not timeouts. While the script will run for several minutes it skips over images that have already been processed and so I quickly get the error message within about 10 seconds. Maybe this has something to do with max table size? I can't find anything in my php.ini that controls this.

我确定问题不是超时。虽然脚本会运行几分钟,但它会跳过已经处理过的图像,所以我在大约 10 秒内很快就收到了错误消息。也许这与最大表大小有关?我在 php.ini 中找不到任何控制它的东西。

All help is greatly appreciated, I've been going at this for hours now.

非常感谢所有帮助,我已经花了几个小时了。

EDIT:

编辑:

Here is the code at the 105 mark, it's the query:

这是 105 标记处的代码,它是查询:

        // Add tNail and image to db
        mysql_query("INSERT INTO gallery values('','$hash','$thumbBlob','$imageBlob','$type','','0','0.0')");
    //  $q->execute();
        print "Successfully processed $fileName<br>";

As you can see by the comments I tried PDO with this and got the same error, foolishly I thought going back to mysql_ commands would help.

正如您在评论中看到的那样,我尝试了 PDO 并得到了同样的错误,愚蠢的是我认为回到 mysql_ 命令会有所帮助。

If it's of any help the script complains at the same image every time, yet each image is only ever a few tens of kilobytes and a couple hundred at the most.

如果它有任何帮助,脚本每次都会抱怨同一个图像,但每个图像只有几十千字节,最多几百字节。

回答by LSerni

Check the value of "max_allowed_packet" in your my.cnfMySQL config file.

检查MySQL 配置文件中“ max_allowed_packet”的值my.cnf

回答by Niet the Dark Absol

This happens because the PHP script took too long to process whatever it was processing, and the connection to the server timed out. You have a few solutions:

发生这种情况是因为 PHP 脚本花费了太长时间来处理它正在处理的任何内容,并且与服务器的连接超时。你有几个解决方案:

  • Check to make sure the connection is still up, and reconnect if needed (mysql_connecthas built-in functionality to do that for you)
  • Use mysql_pconnect(but remember to close the connection at the end because it won't close for you)
  • Improve the running time of your script to avoid the timeout.
  • 检查以确保连接仍然存在,并在需要时重新连接(mysql_connect具有为您执行此操作的内置功能)
  • 使用mysql_pconnect(但请记住最后关闭连接,因为它不会为您关闭)
  • 改善脚本的运行时间以避免超时。

回答by Puggan Se

you can use mysql_ping()to see if the connection still connected, if not you can try to reconnnect, or make an error message telling you how far the script had gone before the connection was lost.

您可以使用它mysql_ping()来查看连接是否仍然连接,如果没有,您可以尝试重新连接,或者发出错误消息,告诉您在连接丢失之前脚本已经走了多远。