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

匿名 (未验证) 提交于 2019-12-03 02:01:02

问题:

I am getting error:

"Lost connection to MySQL server at 'reading initial communication packet, system error: 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:

mysql_connect("202.131.xxx.106:xxxx", "xxxx", "xxxxx") or die(mysql_error()); 

回答1:

Someone here: http://forums.mysql.com/read.php?52,166244,258515#msg-258515 suggests 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.

Could that be it?

Also, somehere here: http://forums.mysql.com/read.php?52,151255,213970#msg-213970 suggests 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".

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'



回答2:

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)

This will solve the problem definitely.

Thanks



回答3:

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.

Note that I use hosts.allow and hosts.deny to control access.



回答4:

1) Allow remote connect to MySQL. Edit file:

>sudo nano /etc/mysql/my.cnf 

Comment line:

#bind-address       = 127.0.0.1 

Restart MySQL:

>sudo service mysql restart 

2) Create user for remote connection.

>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:

>iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT 


回答5:

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.



回答6:

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

this allows mysql to bind to any network interface



回答7:

This error occurred to me while trying to connect to the Google Cloud SQL using MySQL Workbench 6.3.

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.

I authorized it and went back to work.



回答8:

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.



回答9:

I just set up mysql on a windows box. I got the OP's error when trying to connect with the Navicat MySql client on the same box. I had to specify 127.0.0.1 as the host, and that got it.

localhost, or the servers actual ip address both did not work.



回答10:

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.



回答11:

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:

  1. create a user that can be used to connect from the bind address in the my.cnf file E.g.

    CREATE USER 'username'@'bind-address' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON schemaname.* TO 'username'@'bind-address'; FLUSH PRIVILEGES;

  2. change the MySQL hostname value in the connection details in MySQL workbench to match the bind-address



回答12:

in my case, I had ALL: ALL in hosts.deny. Changing this to ALL: PARANOID solved my problem when connecting over ssh



回答13:

The problem was quite stupid for me.

I used to get the same issue on AWS EC2 Ubuntu machine (MariaDB is installed locally for the time being), so I tried to make SSH tunneling, and had the same issue. So I tried to ssh tunnel over terminal:

ssh -L13306:127.0.0.1:3306 root@ip.address -i my/private/key.pem 

And it told me this:

Please login as the user "ubuntu" rather than the user "root".

I changed ssh user from root to ubuntu, just like my ssh config, and it connected just fine.

So check your SSH connecting user.

I oversaw this, so this too half an hour of my time, so I hope this will be useful for you.



回答14:

Ran into this same issue, Bind Address back and forth to no avail. Solution for me was flushing privileges.

mysql> FLUSH PRIVILEGES; 


回答15:

For me setting bind-address = 0.0.0.0 in mysql/my.cnf worked. It basically listens to all addresses (but still one port) then.

And don't forget restart your server: systemctl restart mysql



回答16:

When connecting to Mysql remotely, I got the error. I had this warning in /var/log/mysqld.log:

[Warning] IP address 'X.X.X.X' could not be resolved: Temporary failure in name resolution 

I just added this line to /etc/hosts file:

X.X.X.X some_name 

Problem solved! Not using skip-name-resolve caused some errors in my local app when connecting to MySQL.



回答17:

I had identical problem. To fix it I just changed host from localhost:3306 to just localhost. So error may acour when You sepcify unproper port for connection. It's better to leave it default.



回答18:

Database directory read-write permission also a problem i found. Just make sure your application is able to rw files on db location. Try chmod 777 for testing.



回答19:

I have done below 3 steps then working for me.

1) bind-address = "YOUR MACHINE IP" in my.cnf file at /etc/my.cnf 2) Restart service by command : service httpd restart 3) GRANT ALL PRIVILEGES ON yourDB.* TO 'username'@'YOUR_APPLICATION_IP' IDENTIFIED BY 'YPUR_PASSWORD' WITH GRANT OPTION; 


回答20:

The apache firewall blocks the ip address. so to give access, use these commands:

firewall-cmd --permanent --zone=trusted --add-source=YOUR_IP/32

firewall-cmd --permanent --zone=trusted --add-port=3306/tcp

firewall-cmd --reload



回答21:

I just had the same problem, but in my case I solved it with

service mysqld start



回答22:

For me the config file was found "/etc/mysql/mysql.conf.d/mysqld.cnf" commenting out bind address did the trick.

As we can see here: Instead of skip-networking the default is now to listen only on localhost which is more compatible and is not less secure.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!