php 在“读取初始通信数据包”时失去与 MySQL 服务器的连接,系统错误:0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5755819/
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
Lost connection to MySQL server at 'reading initial communication packet', system error: 0
提问by Rikesh
I am getting error:
我收到错误:
"Lost connection to MySQL server at 'reading initial communication packet, system error: 0"
“在读取初始通信数据包时失去与 MySQL 服务器的连接,系统错误:0”
while I am going to connect my db.
当我要连接我的数据库时。
If I am using localhost everything is working fine. But when I am using my live IP address like below, it's getting error:
如果我使用 localhost 一切正常。但是,当我使用如下所示的实时 IP 地址时,出现错误:
mysql_connect("202.131.xxx.106:xxxx", "xxxx", "xxxxx") or die(mysql_error());
采纳答案by Thomas Daugaard
Someone heresuggests that it might be a firewall problem:
这里有人建议这可能是防火墙问题:
I have just had this problem and found it was my firewall. I use PCTools Firewall Plus and it wasn't allowing full access to MySQL. Once I changed that it was fine. Hope that helps.
我刚刚遇到这个问题,发现它是我的防火墙。我使用 PCTools Firewall Plus 并且它不允许完全访问 MySQL。一旦我改变它就好了。希望有帮助。
Could that be it?
会是这样吗?
Also, someone heresuggests that it might be because the MySQL server is bound to the loop-back IP (127.0.0.1 / localhost) which effectively cuts you off from connecting from "outside".
此外,这里有人建议这可能是因为 MySQL 服务器绑定到环回 IP (127.0.0.1 / localhost),这有效地切断了您与“外部”的连接。
If this is the case, you need to upload the script to the webserver (which is probably also running the MySQL server) and keep your server host as 'localhost'
如果是这种情况,您需要将脚本上传到网络服务器(它可能也在运行 MySQL 服务器)并将您的服务器主机保持为“本地主机”
回答by intekhab rizvi
Open mysql configuration file named my.cnf and try to find "bind-address", here replace the setting (127.0.0.1 OR localhost) with your live server ip (the ip you are using in mysql_connect function)
打开名为 my.cnf 的 mysql 配置文件并尝试找到“bind-address”,这里将设置(127.0.0.1 OR localhost)替换为您的实时服务器 ip(您在 mysql_connect 函数中使用的 ip)
This will solve the problem definitely.
这将绝对解决问题。
Thanks
谢谢
回答by sNICkerssss
1) Allow remote connect to MySQL. Edit file:
1) 允许远程连接到 MySQL。编辑文件:
>sudo nano /etc/mysql/my.cnf
Comment line:
评论行:
#bind-address = 127.0.0.1
Restart MySQL:
重启 MySQL:
>sudo service mysql restart
2) Create user for remote connection.
2) 创建远程连接用户。
>mysql -uroot -p
CREATE USER 'developer'@'localhost' IDENTIFIED BY 'dev_password';
CREATE USER 'developer'@'%' IDENTIFIED BY 'dev_password';
GRANT ALL ON *.* TO 'developer'@'localhost';
GRANT ALL ON *.* TO 'developer'@'%';
3) In my case I need to connect remotely from Windows to VirtualBox machine with Ubuntu. So I need to allow port 3306 in iptables:
3) 就我而言,我需要使用 Ubuntu 从 Windows 远程连接到 VirtualBox 机器。所以我需要在 iptables 中允许端口 3306:
>iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT
回答by Mark
Had this problem when setting up a new slave server. Found it was the slave server IP address was missing from the master server /etc/hosts.allow
file. Added the IP address and it let me connect to the master server.
设置新的从服务器时遇到了这个问题。发现是主服务器/etc/hosts.allow
文件中缺少从服务器IP地址。添加了 IP 地址,它让我连接到主服务器。
Note that I use hosts.allow
and hosts.deny
to control access.
请注意,我使用hosts.allow
和hosts.deny
来控制访问。
回答by user3347295
I had this problem and it ended up being the prior sys admin changed the port MySQL was running on. MySQL Workbench was trying to connect to the default 3306 but the server was running on 20300.
我遇到了这个问题,结果是之前的系统管理员更改了 MySQL 运行的端口。MySQL Workbench 试图连接到默认的 3306,但服务器在 20300 上运行。
回答by Renato Mendes
The problem on my case was MySQL being bind only to the lo on linux. in order to solve the problem i have edited the my.cnf (found at /etc/mysql/my.cnf) removing the line bind-address=127.0.0.1
我的问题是 MySQL 只绑定到 linux 上的 lo。为了解决这个问题,我编辑了 my.cnf(在 /etc/mysql/my.cnf 中找到)删除了行 bind-address=127.0.0.1
this allows mysql to bind to any network interface
这允许 mysql 绑定到任何网络接口
回答by Paulo Occaso
This error occurred to me while trying to connect to the Google Cloud SQL using MySQL Workbench 6.3.
我在尝试使用 MySQL Workbench 6.3 连接到 Google Cloud SQL 时发生了这个错误。
After a little research I found that my IP address has been changed by the internet provider and he was not allowed in the Cloud SQL.
经过一番研究,我发现我的 IP 地址已被互联网提供商更改,并且他不允许在 Cloud SQL 中使用。
I authorized it and went back to work.
我批准了它并回去工作。
回答by Tishan
I ran into this exact same error when connecting from MySQL workbench. Here's how I fixed it. My /etc/my.cnf configuration file had the bind-address value set to the server's IP address. This had to be done to setup replication. Anyway, I solved it by doing two things:
从 MySQL 工作台连接时,我遇到了完全相同的错误。这是我修复它的方法。我的 /etc/my.cnf 配置文件将 bind-address 值设置为服务器的 IP 地址。必须这样做才能设置复制。无论如何,我通过做两件事来解决它:
- create a user that can be used to connect from the bind address in the my.cnf file
- 在 my.cnf 文件中创建一个可用于从绑定地址连接的用户
e.g.
例如
CREATE USER 'username'@'bind-address' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON schemaname.* TO 'username'@'bind-address';
FLUSH PRIVILEGES;
- change the MySQL hostname value in the connection details in MySQL workbench to match the bind-address
- 更改 MySQL 工作台中连接详细信息中的 MySQL 主机名值以匹配绑定地址
回答by ClearCrescendo
The error means that it didn't receive a response from the port it expected to find the server on. The causes range from contacting the wrong machine (For one of a number of reasons) to the server not being on the expected port.
该错误意味着它没有收到来自它期望找到服务器的端口的响应。原因范围从联系错误的机器(出于多种原因之一)到服务器不在预期的端口上。
Check which port your server is bound to in /etc/mysql/my.cnf. Does that correspond to what is in your connect statement. If they match then try connecting with mysql from the server itself and from the command line of the machine where you are running the client. If it works form one place and not another then you may have a firewall / router configuration issue.
在 /etc/mysql/my.cnf 中检查您的服务器绑定到哪个端口。这是否与您的连接语句中的内容相对应。如果它们匹配,则尝试从服务器本身和运行客户端的机器的命令行连接 mysql。如果它在一个地方而不是另一个地方工作,那么您可能有防火墙/路由器配置问题。
回答by Bill Grady
The problem for me was that DNS queries were blocked by the FW within the subnet. The solution was to disable DNS lookups within MySQL.
我的问题是 DNS 查询被子网内的 FW 阻止。解决方案是在 MySQL 中禁用 DNS 查找。