php 与 MySQL 服务器的连接丢失,系统错误:111

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8495684/
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-26 04:52:00  来源:igfitidea点击:

Lost connection to MySQL server, system error: 111

phpmysql

提问by Joe Torraca

When I try to connect to my databases, I get this error:

当我尝试连接到我的数据库时,出现此错误:

Lost connection to MySQL server at 'reading initial communication packet', system error:111

Here is my PHP code (it's just a test):

这是我的 PHP 代码(这只是一个测试):

<?php
@session_start(); 
include(".conf.php");
print_r($conf);
$con = mysql_connect($conf['db_hostname'], $conf['db_username'], $conf['db_password']) or die (mysql_error());
mysql_select_db("aTableName", $con);
$prof = '000';
//$prof = $_GET['profile'];
$prof = addslashes(htmlentities($prof));
$result = "SELECT * FROM aTableName WHERE id = '$prof'";
$q = mysql_query($result, $con);
$qarr = mysql_fetch_array($q);
print_r($qarr); 
?>

Anyhelp would be appreciated. I can't figure it out and it's really bugging me. Thank you!

任何帮助将不胜感激。我想不通,这真的困扰着我。谢谢!

EDIT: I am hosting on GoDaddy, hosting and databases.

编辑:我在 GoDaddy、托管和数据库上托管。

回答by David Schwartz

Your host is wrong, your port is wrong, a firewall blocked the connection, or the SQL server is not running. 111 is "connection refused". You can troubleshoot by trying to telnetto the SQL server's host and port. If that works, your code gets the host or port wrong.

您的主机错误,您的端口错误,防火墙阻止了连接,或者 SQL 服务器没有运行。111 是“连接被拒绝”。您可以通过尝试访问telnetSQL 服务器的主机和端口来进行故障排除。如果可行,则您的代码会错误地获取主机或端口。

If that doesn't work, check on the SQL server itself. If it works there, the problem is the network. If it doesn't work there, the SQL server is not running.

如果这不起作用,请检查 SQL 服务器本身。如果它在那里工作,则问题出在网络上。如果它在那里不起作用,则 SQL 服务器没有运行。

回答by Alfabravo

Check my.cnf file. It probably allows localhost to connect but your conf.php surely tries to connect to your IP or 127.0.0.1. Try with localhost and, if it works and you want it to behave in a different way, edit your my.cnf file in the bind-address directive (changing it to your IP or to 127.0.0.1, perhaps, or commenting the line so no binding is performed).

检查 my.cnf 文件。它可能允许 localhost 连接,但您的 conf.php 肯定会尝试连接到您的 IP 或 127.0.0.1。尝试使用 localhost,如果它有效并且您希望它以不同的方式运行,请在 bind-address 指令中编辑您的 my.cnf 文件(将其更改为您的 IP 或 127.0.0.1,也许,或注释该行不进行绑定)。

Anyway, when you connect to mysql with "localhost", it uses a socket instead of a TCP conn, hence it probably fails with that,

无论如何,当您使用“localhost”连接到 mysql 时,它使用套接字而不是 TCP 连接,因此它可能会失败,

回答by Kevin

I had the same error. I was trying to access the mysql DB on the server 192.168.0.4 from a client machine 192.168.0.47. When I went into /etc/mysql/my.cnf an changed the bind-address = 192.168.0.4 (and commented out 127.0.0.1), and rebooted the machine, the error message went away.

我有同样的错误。我试图从客户端机器 192.168.0.47 访问服务器 192.168.0.4 上的 mysql DB。当我进入 /etc/mysql/my.cnf 并更改 bind-address = 192.168.0.4(并注释掉 127.0.0.1)并重新启动机器时,错误消息消失了。

回答by Elminson De Oleo Baez

In my case, i have datadir=/media/sdc1/mysql and the disk was not mounted. i Remount and restart mysql

就我而言,我有 datadir=/media/sdc1/mysql 并且磁盘没有安装。我重新安装并重新启动mysql

回答by neske

In my case disk is full. You have to remove any not necessary files.

在我的情况下,磁盘已满。您必须删除任何不需要的文件。

If you removed then you have to restart mysqld.

如果删除了,则必须重新启动 mysqld。

For example: sudo service mysqld restart

例如: sudo service mysqld restart