Lost connection to MySQL server at 'reading in

2019-01-01 02:30发布

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());

标签: php mysql
23条回答
素衣白纱
2楼-- · 2019-01-01 02:40

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楼-- · 2019-01-01 02:44

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

mysql> FLUSH PRIVILEGES;
查看更多
像晚风撩人
4楼-- · 2019-01-01 02:46

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.

查看更多
一个人的天荒地老
5楼-- · 2019-01-01 02:46

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

查看更多
旧时光的记忆
6楼-- · 2019-01-01 02:47

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.

查看更多
明月照影归
7楼-- · 2019-01-01 02:48

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
查看更多
登录 后发表回答