In mysql, Can’t connect to MySQL server on localhost error occurs when mysql is unable to connect to localhost due to the connection issue with mysql or a database configuration issue with mysql. You may see an error Can’t connect to MySQL server on ‘’ when you connect to ip and unable to connect to localhost 3306. You can’t connect to mysql server on localhost or ip (3306) because either the mysql server may not be started or it may not be running, or the network connection to the mysql server is not established, or the mysql server port is different.

Some times, the application can’t connect to local mysql server or mysql could not connect server may not be running or mysql unable to connect to localhost:3306. The application can’t connect to mysql server on ‘localhost:3306’.

If you are trying to access mysql server from a remote server, the error message “Can’t connect to MySQL server on ‘ localhost ‘” may be displayed in the application. This error occurs if the application is unable to connect to mysql server due to a variety of reasons. In this post, we will see the error message “Can’t connect to MySQL server on ‘ localhost ‘” and how to fix this error.

The error message indicates that there is a problem when connecting to mysql from the calling application. If you identify a connectivity issue, the error message will be fixed.

Can't connect to MySQL server on 'localhost'
Can't connect to local MySQL server through socket '/tmp/mysql.sock'

Root Cause

The error is shown in the application because the application was unable to connect to the mysql server. There could be a number of reasons. This issue is caused by a network failure.

The network failure could be on mysql server side or on the connecting application side. The other reason is that the mysql server is not running on the server. Mysql may be shut down manually, or mysql may not be running on the server machine due to some error.

Solution 1

If the database is running on the local machine, run the ping command with localhost. If the database is running on the remote server, ping the ip of the database server. In this way, we can validate the database server is alive or shutdown or network issue. If the ping does not respond, check that the server is switched on. Check that the network cable is connected correctly.

For local machine

$> ping

For remote server

$> ping <ip of the remote server>

$> ping

Solution 2

Check that mysql is running or not in the database server. The mysql server may be switched off or stopped due to some critical error. try to restart the mysql server. Check the following command to check whether the mysql server is running or not.

$> netstat -ln | grep mysql

fcc1dd9a70716e73 stream      0      0 fcc1dd9a734eb92b                0                0                0 /tmp/mysqlx.sock
fcc1dd9a707174b3 stream      0      0 fcc1dd9a732f1f7b                0                0                0 /tmp/mysql.sock

The command above shows the network status of the mysql command. The command below will show whether or not the mysql port is listening. The default port number is 3306.

$> netstat -ln | grep 3306

tcp4       0      0        ESTABLISHED
tcp4       0      0         ESTABLISHED
tcp4       0      0        ESTABLISHED
tcp4       0      0         ESTABLISHED

If the above two commands do not return the expected result, check whether the mysql process is running or not using the following command.

$> ps -ax | grep mysql
  113 ??         4:47.15 /usr/local/mysql/bin/mysqld --user=_mysql --basedir=/usr/local/mysql  ... ... ... ... ... ... ... ...

Solution 3

If mysql is running, check the status of mysql service. If the server appears to be running. Stop the server and restart the service again. If mysql server is hung due to issues such as memory issues, the network issue will be resolved. Use the command below to check the service

$> sudo server mysql status

$> sudo server mysql stop

$> sudo server mysql start
$> sudo /etc/init.d/mysqld status

$> sudo /etc/init.d/mysqld stop

$> sudo /etc/init.d/mysqld start

Solution 4

The mysql server is running without error. Network issues have been resolved and no network issues have been identified. Now connect to mysql server using mysql command. You can also check using the telnet command

$> telnet 3306
$> mysql -u root -p -h <host name/ip>

if the port is different from the default port 3306

$> mysql -u root -p -h <host name/ip> -P <port>

Solution 5

If the above command is working successfully and you still see error in mysql. Check out my.cnf file. My.cnf file contains all configurations of mysql. Run the command below to find the location of my.cnf file,

$> mysql --help | grep my.cnf

or just run 

$> mysql --help

Open my.cnf file and search for the word bind-address. If there is an existing change as below. If it doesn’t exist, add the line below.

to refer the localhost 
bind-address =

to refer for any address
bind-address =

Solution 6

If all of the above steps are checked and configured, restart the mysql server. All changes will be updated and start with a new configuration. If there is an error in the configuration, mysql will throw an error and will not start the server. Run the command below to check and restart mysql server

$> sudo server mysql status

$> sudo server mysql stop

$> sudo server mysql start

Solution 7

Start the mysql server with install and initialize option and check the mysql server is running or not. if you are using windows, run the mysql server as admin.

C:\Program Files\MySQL\MySQL Server\bin>mysqld --install

C:\Program Files\MySQL\MySQL Server\bin>mysqld --initialize

Solution 8

If you are using windows operating system and mysql database is installed newly, then use “MySQL Installer – Community” from the menu to reconfigure. select “mysql server” in the product tab and click the “reconfigure” in the “Quick Action” tab. This will reconfigure the mysql database.

Leave a Reply