166

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
  • This error will appear if you try to connect to a postgres database using a mysql tool. Apr 29, 2022 at 12:21

42 Answers 42

127

Someone here 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.

Could that be it?

Also, someone here 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'

4
  • 10
    what do you mean localhost? I am also facing the same problem and using localhost only also. Application is on the same machine as the database. What did you mean localhost?
    – oneofakind
    Apr 6, 2015 at 1:56
  • 7
    @oneofakind He means to connect to 'localhost" instead of '127.0.0.1' Oct 30, 2015 at 11:04
  • 2
    I had the same problem on OSX with mamp pro. I fixed it by disabling/re-enabling the "allow network access to mysql" checkbox on the mamp pro mysql tab.
    – Ousmane
    Dec 27, 2017 at 15:33
  • 1
    Another reason for this error would be using a MySQL client to connect to a non MySQL db (like PostgreSQL).
    – roychri
    Dec 21, 2022 at 19:15
49

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.

4
  • 2
    Make sure you use bind-address = 127.0.0.1 or localhost when using ssh tunnel. Had this problem with bitnami, where bind-address was machine ip.
    – z2z
    Jan 3, 2019 at 11:25
  • 1
    The same problem and solution for Debian 10 + MySQL standalone dedicated server.
    – Vilq
    Jan 27, 2020 at 17:04
  • It should be mentioned that it might be generally preferrable to bind the mysql service to 127.0.0.1 where applicable so direct access from other hosts is not possible. This prevents remote brute force attacks and does not expose possible security issues to the network. If your application is located on the same machine (which is a very common hosting setup), use 127.0.0.1 resp. localhost as mysql host and it will work the same way as binding the service to your external interface and using that as mysql host. If your application is located elsewhere use an internal network if possible.
    – David
    Apr 20, 2020 at 19:16
  • Trust in this guy, I have a team of people here that were doubtful, but this does definitely solve the problem definitely.
    – John Hunt
    Feb 10, 2022 at 17:16
49
  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
    
6
  • 1
    For me I forget verify bind-address on my.cnf. Thanks.
    – abkrim
    Sep 20, 2016 at 8:17
  • 1
    This totally helped. Thank you!
    – osehgol
    Dec 21, 2017 at 16:07
  • 1
    Worked perfectly, but i was using workbench on windows so didn't do step 3. but it worked. Thanks!
    – RohitAneja
    May 7, 2018 at 7:17
  • Although last command did not work for me, creating new user with your way solved my problem. I can access mysql on raspberry pi from my laptop. Thanks! Mar 21, 2020 at 20:30
  • 1
    remember to FLUSH PRIVILEGES after created a new user. Apr 9, 2020 at 7:03
17

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.

3
  • my problem with connector/c++. now, if i could only find what setting needs to be in allow when deny has ALL: ALL...
    – user1382306
    Jul 8, 2013 at 23:30
  • this really saved my live, I was having issues connecting to mysql from workbench, I needed to add "ALL: 127.0.0.1" to /etc/hosts.allow file and it started to work May 30, 2014 at 6:58
  • In my case, /etc/hosts had a bad (old) entry for the host IP. When issued a new IP, it came under the old one and was seemingly ignored. Deleting the old IP healed the error. Nov 12, 2015 at 22:44
9

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.

1
  • 2
    Good thing for this answer. Turns out, i did this to myself.
    – cautionbug
    Dec 13, 2014 at 3:12
7

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.

1
  • I don't think this is correct, if there's nothing listening on that port then you'll get the response ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (61), not the Lost connection to MySQL server message. Oct 12, 2019 at 2:12
7

One more reason...

I ran into an Ubuntu server where everything was customized and could not connect because of that same error.

This setting was inside /etc/ssh/sshd_config

PermitTunnel no

After turning into

PermitTunnel yes

I was able to connect remotely to my MySQL DB

6
  • I don't see this line in my file. PermitPassword is there.
    – Nithur
    Sep 16, 2022 at 8:27
  • Default is PermitTunnel yes and it's not necessary to add the line... but in my case it was no so I just changed the no to yes... I could've also delete the whole line...
    – lewis4u
    Sep 16, 2022 at 8:35
  • ok, I am now facing system error 104 while connecting to a remote MySQL server and couldn't find any useful solutions.
    – Nithur
    Sep 16, 2022 at 8:46
  • that has to do something with port...
    – lewis4u
    Sep 16, 2022 at 8:48
  • can you please explain it a bit??
    – Nithur
    Sep 16, 2022 at 8:59
5

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

5

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.

5

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.

0
4

I faced the same problem. I checked and tried to set AllowTcpForwarding Yes but it was missing in my sshd_config so no help.I didn't change sshd_config or my.cnf. Make sure the ssh hostname is NOT the same with the mysql hostname(use localhost).

In workbench, choose + to add new connection and set the following:

  • connection method: standard TCP/IP over SSH
  • SSH Hostname: 192.168.0.50:22 (replace remote SSH server IP and port(optional))
  • SSH Username: sshuser
  • You can set password or add at the prompt
  • MYSQL Hostname: localhost or 127.0.0.1
  • MYSQL Server port:3306
  • You can set password or add at the prompt

Test connection. It should be successful then hit OK.Viola!

1
  • Thank you so much, the mysql host name was my hang up. It makes perfect sense now though.
    – Nick
    Aug 24, 2022 at 21:47
4

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;
  1. change the MySQL hostname value in the connection details in MySQL workbench to match the bind-address
1
  • Thank you very much, we had also MySQL replication and the bind-address was a local one: 10.0.0.x. I was struggling for days for this solution.
    – iFadi
    Feb 5, 2020 at 9:42
3

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.

1
  • I suspect that DNS is to blame in my case, as well.
    – Zenexer
    Apr 9, 2015 at 19:22
3

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 [email protected] -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.

3

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.

1
  • For my biggest surprise, this one worked under Debian 10. I gave a chance for it and WORKS! Edited mysql config sudo nano /etc/mysql/mariadb.conf.d/50-server.cnfand instead of bind-address = 111.112.113.114 I used bind-address = 127.0.0.1. The SSH tunnel settings are set as described on hostpresto.com/community/tutorials/… Good job! Thanks!
    – klor
    Jul 17, 2019 at 18:07
3

I am trying to connect my db docker container on Ubuntu 18.04, same problem.

First check your device by run nmcli dev to check if device docker0 is connected.

If it is not connected, try to restart docker service:

sudo service docker restart

3

I tried make a telnet over remote server on port 3306. The error message is clear

Host 'x.x.x.x' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'Connection closed by foreign host.

As root at server mysqladmin flush-hosts worked at all!

3

I had the same error when using localhost. I restarted the MySQL service and it worked fine.

1
  • 1
    Apparently in my case, it took a lot of time for the image to initialize the database, and that's why I couldn't connect for quite a while. Aug 22, 2021 at 12:10
2

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

2

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

mysql> FLUSH PRIVILEGES;
2

Firewalld 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

1

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

1
  • 6
    All addresses/interfaces, but only a single port.
    – Zenexer
    Apr 9, 2015 at 19:23
1

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

service mysqld start

1

In my case it was the university wifi blocking port 3306. I was able to connect by using a mobile hotspot.

Change to a mobile hotspot or another network, and if it works there, then you know that original network is blocking port 3306. If you get the same error on more than 1 network, then you know it's specific to your machine.

1
  • I had port 3306 in Docker container but in Dockerfile it was 33060. I edited the port in Docker container to 33060

  • Must have been added to the Dockerfile

    ENV MYSQL_ROOT_HOST 172.17.0.1

1

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 mysql restart

  3. GRANT ALL PRIVILEGES ON yourDB.* TO 'username'@'YOUR_APPLICATION_IP' IDENTIFIED BY 'YOUR_PASSWORD' WITH GRANT OPTION;

1

I had the same issue installing MySQL docker image then trying to connect from WSL2 MySQL client.

As it was stated in the accepted answer that it should be a firewall issue, in my case this error was caused due to not allowing docker for windows to communicate to private network.

I changed the settings on "Firewall & network protection", "allow an app through firewall", "change settings" (need administrator rights) and allowed "Docker desktop backend" to connect to private network.

1

I had the same error on my Mac with a local MySQL installation. The problem was that the number files that MySQL was opening was too high for MacOS.

To see if you have the same problem you can run this command and look for File Descriptor errors:

tail -200 /usr/local/var/mysql/$(whoami).err | grep "Warning"

I added this line to my.cnf file and the problem was fixed:

table_open_cache = 200
1

Had the same problem, what worked for me was:

  1. Go to Windows Firewall where you allow applications.
  2. mysql probably won't be in the list, so you need to add it, its path is typically C:/Program Files (x86)/MySQL/bin/mysql
  3. Mark both private and public networks, apply.
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.