Can’t connect to MySQL server on localhost error often occurs when there is a connection or a database configuration issue. These issues make MySQL unable to connect to the localhost.
There are various reasons for this error. This tutorial will explain it and give possible solutions.
Why MySQL Can’t Connect To Server On Localhost?
After installing the server on the Windows system, you can log in to your mySQL server by the Command line. Yet, it is easy to get the error below:
mysql --user=[your username] --password=[your password] ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (10061)
This indicates that the network connection is refused and you need to check it. The network failure may come from the MySQL server side or from the connecting application one.
Another reason for this error is that the server is not running. MySQL shuts down manually and errors make it unable to run on the server machine.
How To Fix Can’t Connect To MySQL Server On ‘Localhost’ Error
Check The Operation Of MySQL
If your MySQL is still running, you can use the following commands to check it.
$> mysqladmin version $> mysqladmin variables $> mysqladmin -h `hostname` version variables $> mysqladmin -h `hostname` --port=3306 version $> mysqladmin -h host_ip version $> mysqladmin --protocol=SOCKET --socket=/tmp/mysql.sock version
If the database is running properly, ping its server’s IP. This way, you can validate whether the server is shut down or alive or runs into any network issue.
Ensure the server is switched on with a correctly connected network cable.
For local machine
$> ping 127.0.0.1
For remote server
$> ping <ip of the remote server> $> ping 22.214.171.124
Check The Service’s Status
If MySQL is running properly, the problem may be in its service’s status. Stop and restart it again by using the command:
$> 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
Connect To MySQL Server
Now that you have checked the operation and network connection, let’s try to connect to MySQL server with the telnet command:
$> telnet 126.96.36.199 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>
Check Out my.cnf File
Find the local of this file by running the command:
$> mysql --help | grep my.cnf or just run $> mysql --help
Open it and search for the bind-address. If there is no change existing, add the line:
to refer the localhost bind-address = 127.0.0.1 to refer for any address bind-address = 0.0.0.0
Now all you need is to restart the server and you successfully fix the error:
$> sudo server mysql status $> sudo server mysql stop $> sudo server mysql start
The post has helped you check MySQL service status and fix can’t connect to MySQL server on localhost error.
Leave a comment