从 PHP 连接到我的本地主机 MySQL 实例时出现间歇性错误:mysqli_connect(): HY000/2013

发布于 2024-12-12 10:19:26 字数 2657 浏览 0 评论 0原文

我有一个 LAMP 网站 (PHP/MySQL),并且严重依赖 MySQL 数据库。

最近,开始间歇性地出现以下错误:

[30-Oct-2011 16:11:49] PHP Warning:  mysqli_connect() [<a href='function.mysqli-connect'>function.mysqli-connect</a>]: (HY000/2013): Lost connection to MySQL server at 'sending authentication information', system error: 32 in /home/path/to/my/code.php on line 1029

这是第 1029 行,该行偶尔会出现上述错误:

$db = mysqli_connect($cfg['db_location'], $cfg['db_reader_username'], $cfg['db_reader_password']);

where $cfg['db_location'] is "localhost" ,且用户名和密码正确。

似乎有什么东西导致我的 PHP 脚本间歇性地失去与 MySQL 服务器的连接,这让我很困惑,因为我的所有 MySQL 连接都是与本地主机建立的。也就是说,PHP 脚本和 MySQL 服务器都在同一个机器上运行,我通过 localhost 连接到 MySQL。

此外,这令人费解,因为我的 PHP 脚本能够成功连接到 MySQL 服务器,大约 90% 的页面。

据我所知,我的服务器有足够的可用 RAM 和 CPU:

top - 16:38:23 up 173 days, 19:19,  2 users,  load average: 0.57, 0.78, 0.84
Tasks: 142 total,   4 running, 137 sleeping,   0 stopped,   1 zombie
Cpu(s): 18.7%us, 27.5%sy,  0.7%ni, 52.6%id,  0.2%wa,  0.1%hi,  0.2%si,  0.0%st
Mem:   2976260k total,  1861368k used,  1114892k free,   532628k buffers
Swap:  2048248k total,      116k used,  2048132k free,  1035804k cached

以及足够的可用磁盘空间。当我运行 df -h 时,我的所有文件系统的使用率最多为 25%。

所以我不知道什么会导致我在 PHP 错误日志中看到的偶发错误。

有什么想法可能会导致我的 PHP 脚本间歇性地失去与本地主机上的 MySQL 服务器的连接吗?

我升级到 MySQL 5.1,现在这就是我运行 mysql -V时得到的结果code>:

mysql  Ver 14.14 Distrib 5.1.56, for pc-linux-gnu (i686) using readline 5.1

尽管如此,仍然会发生相同的错误:

[30-Oct-2011 20:26:18] PHP Warning:  mysqli_connect() [<a href='function.mysqli-connect'>function.mysqli-connect</a>]: (HY000/2013): Lost connection to MySQL server at 'sending authentication information', system error: 32 in /home/path/to/my/code.php on line 1029

与以前一样,错误仍然是间歇性的。

升级到 MySQL 5.1 后,我一直在仔细查看 PHP 错误日志。我注意到其他一些与第一个类似的错误。例如,这里有一个这样的错误:

[30-Oct-2011 22:42:29] PHP Warning:  mysqli_connect() [<a href='function.mysqli-connect'>function.mysqli-connect</a>]: (HY000/2013): Lost connection to MySQL server at 'reading authorization packet', system error: 104 in /home/path/to/my/code.php on line 1029

这是我在日志中看到的另一个错误:

PHP Warning:  mysqli::mysqli() [<a href='mysqli.mysqli'>mysqli.mysqli</a>]: (HY000/2002): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/path/to/different/code.php on line 7

在所有情况下,问题从根本上来说似乎是连接到 MySQL 失败。有什么想法可能导致此问题或我如何调试它?

I have a LAMP website (PHP/MySQL), and I rely heavily on my MySQL database.

Recently, the following error started occurring intermittently:

[30-Oct-2011 16:11:49] PHP Warning:  mysqli_connect() [<a href='function.mysqli-connect'>function.mysqli-connect</a>]: (HY000/2013): Lost connection to MySQL server at 'sending authentication information', system error: 32 in /home/path/to/my/code.php on line 1029

This is line 1029, the specific line that occasionally gives the above error:

$db = mysqli_connect($cfg['db_location'], $cfg['db_reader_username'], $cfg['db_reader_password']);

where $cfg['db_location'] is "localhost", and the username and password are correct.

It appears as though something is causing my PHP script to intermittently lose connection to the MySQL server, which is puzzling to me because all of my MySQL connections are made to localhost. That is, the PHP script and the MySQL server are both running on the same box, and I connect to MySQL via localhost.

Additionally, this is puzzling because my PHP script is able to successfully connect to the MySQL server for perhaps 90% of pages.

As best I can tell, my server has plenty of available RAM and CPU:

top - 16:38:23 up 173 days, 19:19,  2 users,  load average: 0.57, 0.78, 0.84
Tasks: 142 total,   4 running, 137 sleeping,   0 stopped,   1 zombie
Cpu(s): 18.7%us, 27.5%sy,  0.7%ni, 52.6%id,  0.2%wa,  0.1%hi,  0.2%si,  0.0%st
Mem:   2976260k total,  1861368k used,  1114892k free,   532628k buffers
Swap:  2048248k total,      116k used,  2048132k free,  1035804k cached

And plenty of available disk space. When I run df -h, all of my filesystems are at most 25% in use.

So I don't know what would be causing the sporadic errors that I'm seeing in my PHP error logs.

Any ideas what might be causing my PHP script to intermittently lose connection to my MySQL server on localhost?

I upgraded to MySQL 5.1, and this is now what get when I run mysql -V:

mysql  Ver 14.14 Distrib 5.1.56, for pc-linux-gnu (i686) using readline 5.1

The same errors are still happening, though:

[30-Oct-2011 20:26:18] PHP Warning:  mysqli_connect() [<a href='function.mysqli-connect'>function.mysqli-connect</a>]: (HY000/2013): Lost connection to MySQL server at 'sending authentication information', system error: 32 in /home/path/to/my/code.php on line 1029

As before, the errors continue to be intermittent.

After I upgraded to MySQL 5.1, I've been looking carefully at my PHP errors logs. I've noticed a few other errors similar to the first one. For example, here's one such error:

[30-Oct-2011 22:42:29] PHP Warning:  mysqli_connect() [<a href='function.mysqli-connect'>function.mysqli-connect</a>]: (HY000/2013): Lost connection to MySQL server at 'reading authorization packet', system error: 104 in /home/path/to/my/code.php on line 1029

And here's another error I've seen in my logs:

PHP Warning:  mysqli::mysqli() [<a href='mysqli.mysqli'>mysqli.mysqli</a>]: (HY000/2002): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/path/to/different/code.php on line 7

In all cases, the problem fundamentally seems to be a failure to connect to MySQL. Any ideas what might be causing this or how I could debug it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪你到最终 2024-12-19 10:19:26

也许这个错误报告会有所帮助 - 似乎修复可能是为全局 connect_timeout。在 MySQL 5.1 的更高版本中,它被设置为更高的值(10 秒),而早期版本则设置为 5 秒。

http://bugs.mysql.com/bug.php?id=28359

Perhaps this bug report will help - It seems that a fix may be to set a higher timeout for the global connect_timeout. It was set to a higher value in later versions of MySQL 5.1 (10 seconds) as opposed to 5 seconds for earlier versions.

http://bugs.mysql.com/bug.php?id=28359

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文