[mysql] ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)

I use the following command:

mysql -u root -h 127.0.0.1 -p

and the error message is :

ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)

Who can help me to fix it?

This question is related to mysql mysql-error-2003

The answer is


You need to change the bind-address parameter to 127.0.0.1 in the mysql config file (my.ini or my.cnf) or use the one that is defined there.

If that doesn't work you should check that the mysql service is actually running.


Please make sure your MySql server is running on localhost.

On Linux

To check if MySql server is running:

sudo service mysql status

To run MySql server:

sudo service mysql start

On Windows

To check if MySql server is running:

C:\Windows\system32>net start

If MySql is not in list, you have to start/run MySql.

To run MySql server:

C:\Windows\system32>net start mysql

Hope this helps.


Try dont shut down iptables and open port 3306.

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

or sudo ufw allow 3306 if you use ufw.

check: netstat -lnp | grep mysql you should get sth like that:

cp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      2048/mysqld         
tcp6       0      0 :::33060                :::*                    LISTEN      2048/mysqld         
unix  2      [ ACC ]     STREAM     LISTENING     514961   2048/mysqld          /var/run/mysqld/mysqld.sock
unix  2      [ ACC ]     STREAM     LISTENING     514987   2048/mysqld          /var/run/mysqld/mysqlx.sock

if you have null then delete # before port = 3306 in cnf file.


For Docker users - When trying to connect local sql using mysql -u root -h 127.0.0.1 -p and your database is running on Docker container, make sure the mysql service is up and running (verify using docker ps and also check that you are in the right port as well) , if the container is down you'll get connection error.

Best practice is to set the ip's in /etc/hosts on your machine:

127.0.0.1 db.local

and running it by mysql -u root -h db.local -p


In my case (remote connnection) helped turning off firewall on server.

service iptables stop

Incase you are running on a non-default port, you may try using --port=<port num> provided --skip-networking is not enabled.


This problem may occur because your MySQL server is not installed and running. To do that start command prompt as admin and enter command:

"C:\Program Files (x86)\MySQL\MySQL Server 5.1\bin\mysqld" --install

If you get "service successfully installed" message then you need to start the MySQL service. To do that: go to Services window (Task Manager -> Services -> Open Services) Search for MySQL and Start it from the top navigation bar. Then if try to open mysql.exe it will work.


If you have tried all of the above and it still did not work. Check firewall.

I was trying to connect from a windows machine to a MySql Server 5.7.32 installed on a VirtualBox machine with CentOs7. I have tried everything and it was not working, even though i could ping the machine i couldn't connect to the MySql Server.

On CentOs7 the commands to check the firewall are:

Step 1: Check the status of your firewall service:

systemctl status firewallid.service

Step 2: Disable the firewall service

systemctl stop firewallid.service

I just have this problem.... running in Win7 and wamp server ... after reading this

Found that Antivirus Firewall has caused the problem.


If you are using ubuntu, you have to use the following steps to avoid this error(if there is no replication enabled):

  1. run the command vim /etc/mysql/my.cnf
  2. comment bind-address = 127.0.0.1 using the # symbol
  3. restart your mysql server once.

Update

In Step 1, if you cannot find bind-address in the my.cnf file, look for it in /etc/mysql/mysql.conf.d/mysqld.cnf file.

Update in case of MySQL replication enabled

Try to connect MySQL server on IP for which MySQL server is bind in 'my.cnfinstead oflocalhost or 127.0.0.1`.


I changed the installation directory on re-install, and it worked.


I was also facing the same issue. The following helped me fix the problem go to control panel-> Administrative tools-->services inside this you will most certainly see the MySQL servce: right click and say start(force start).


This happens when you forget to start the database before connecting to it:

mysql.server start

then

mysql -u root -p -h 127.0.0.1

look at the my.cnf file, if there contain [client] section, and the port is other than real listen port (default 3306), you must connect the server with explicit parameter -P 3306, e.g.

mysql -u root -h 127.0.0.1 -p -P 3306


i just restart my mysql server and the problem solved in windows net stop MySQL then net start MySQl ubuntu linux sudo service start mysql


Try localhost instead of 127.0.0.1 to connect or in your connection-config. Worked for me on a Debian Squeeze Server