Lỗi cannot connect to database server mysql localhost 3306

Learn how to resolve the Cannot connect to mysql server on localhost 3306 error. Our MySQL Support team is here to help you with your questions and concerns.

Cannot connect to MySQL server on localhost 3306 | Resolved

If you have been running into the “cannot connect to mysql server on localhost 3306”, you have come to the right place Some of the common reasons behind this error include:

Lỗi cannot connect to database server mysql localhost 3306

  • Due to a connection problem, MySQL cannot connect to localhost.
  • Due to a problem with the database configuration, MySQL cannot connect to localhost.
  • It’s possible that the MySQL server hasn’t started or isn’t running.
  • The MySQL server’s network connection cannot be established.
  • The port for the MySQL server is different.

How to fix the ‘cannot connect to MySQL server on localhost 3306’ error ‘?

1. By using Ping command

Run the ping command with localhost if the database is executing on the local machine. Ping the database server’s IP to see if the database is running on the remote server. So, we can verify whether the database server is running, shut down, or experiencing a network problem. Verify that the server is turned on if the ping receives no response. Make sure the network cable is properly connected.

For local machines,

$> ping 127.0.0.1

For remote server,

$> ping 
$> ping 172.168.1.3

2. Confirm MySQL server is running

Sometimes the MySQL server may be switched off or stopped due to some critical error. If that’s the case, then we need to restart the MySQL server. We can use the below command to confirm it.

$> 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

This will show the network status of the MySQL.

$> netstat -ln | grep 3306
tcp4    0   0  127.0.0.1.3306      127.0.0.1.53815     ESTABLISHED
tcp4    0   0  127.0.0.1.53815     127.0.0.1.3306      ESTABLISHED
tcp4    0   0  127.0.0.1.3306      127.0.0.1.53814     ESTABLISHED
tcp4    0   0  127.0.0.1.53814     127.0.0.1.3306      ESTABLISHED

This command will show whether or not the MySQL port is listening. The default port number is 3306. If both the commands don’t return the expected result, then check if 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  ... ... ... ... ... ... ... ... --early-plugin-load=keyring_file=keyring_file.so

3. Check the status of MySQL service

We can use the following commands to check the service status.

$> 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

We have to check the status of MySQL service is running without any issues. If the server appears to be running, stop and restart it. When MySQL server is hung due to memory issues then this handy trick will resolve the issue.

4. By connecting to MySQL server

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. We can also use the telnet command.

$> telnet 172.168.1.3 3306
$> mysql -u root -p -h 
if the port is different from the default port 3306
$> mysql -u root -p -h  -P 

5. Check out my.cnf file

The my.cnf file contains all configurations of MySQL. We can use the below command to locate the my.cnf file.

mysql --help | grep my.cnf or just run mysql --help

Now open my.cnf file and search for the word bind-address. If there is no address, then add the below line.

$> ping 
$> ping 172.168.1.3

0

6. Restart MySQL server

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

7. With install and initialize option

Start the MySQL server with the install and initialize option. Then, check if the MySQL server is running or not. If we are using Windows, we can run the MySQL server as admin.

$> ping 
$> ping 172.168.1.3

2

8. Using MySQL Installer – Community

When using the Windows OS and a newly installed MySQL database, use “MySQL Installer – Community” from the menu to reconfigure. We have to select “MySQL server” in the product tab and click the “reconfigure” in the “Quick Action” tab to reconfigure the MySQL database.

[Looking for a solution to another query? We are just a click away.]

Conclusion

To sum up, our Support team introduced several different ways to resolve the ‘cannot connect to MySQL server on localhost 3306’ error.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.