Failed to Connect to MySQL

When mysql is unable to connect to localhost due to a connection issue with mysql or a database setup issue with mysql, the Can’t connect to MySQL server on localhost error occurs. When connecting to ip and unable to connect to localhost 3306, you may receive the error Can’t connect to MySQL server on ‘127.0.0.1’. Because the mysql server is not started or functioning, the network connection to the mysql server is not established, or the mysql server port is different, you can’t connect to it on localhost or ip 127.0.0.1 (3306).

The program may be unable to connect to the local MySQL server, or the MySQL server may not be functioning, or mysql unable to connect to localhost:3306. On ‘localhost:3306’, the application is unable to connect to the MySQL server.

The error message “Can’t connect to MySQL server on ‘localhost ‘” may appear in the application if you try to connect to MySQL server from a remote server. This error appears when the program is unable to connect to the MySQL server for various reasons. We’ll look at the error message “Can’t connect to MySQL server on ‘localhost ‘” and how to repair it in this post.

The error message indicates that a connection to mysql from the calling program has failed. The error message will be fixed if a connectivity issue is discovered.

Can’t connect to MySQL server on ‘localhost’ 

Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’

 

Root Cause

Because the application was unable to connect to the MySQL server, the error is displayed in the application. There could be several explanations for this. A network failure is to blame for this problem.

The network failure could be caused by either the mysql server or the connecting application. The other cause is that the server’s mysql server is not running. Mysql might have been manually turned down, or it might not be operating on the server system due to an error.

 

Solution 1

Run the ping command using localhost if the database is running on the local system. Ping the database server’s IP address if the database is running on a remote server. This allows us to check if the database server is up and running or if it is down due to a network problem. Check sure the server is turned on if the ping does not answer. Verify that the network cable is properly attached.

For local machine

$> ping 127.0.0.1

For remote server

$> ping <ip of the remote server>

$> ping 172.168.1.3

 

Solution 2

Verify whether or not mysql is operating on the database server. Due to a major error, the mysql server may be turned off or stopped. Restart the mysql server if necessary. To see if the mysql server is up and working, perform the following command.

$> 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 mysql command’s network status is displayed by the command above. The command below will display whether the mysql port is open for connections. 3306 is the default port number.

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

If the preceding two commands do not get the desired result, use the following command to see if the mysql process is running.

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

 

Solution 3

Check the status of the mysql service if mysql is functioning. If it appears that the server is up and operating. Stop the server and then start it up again. The network issue will be fixed if the mysql server is hung due to difficulties such as memory issues. To test the service, use the command below.

$> 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 up and operating without any issues. Network problems have been fixed, and no new problems have been discovered. Now use the mysql command to connect to the MySQL server. You can also use the telnet command to check.

$> telnet 172.168.1.3 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 completes correctly yet the mysql error persists. Take a look at the my.cnf file. The my.cnf file contains all of mysql’s configurations. To find the location of the my.cnf file, use the command below.

$> mysql –help | grep my.cnf

or just run

$> mysql –help

Look for the word bind-address in the my.cnf file. If there is already a change, it will be as follows. Add the line below if it doesn’t already exist.

to refer the localhost
bind-address = 127.0.0.1

to refer for any address
bind-address = 0.0.0.0

 

Solution 6

Restart the mysql server once all of the preceding procedures have been checked and configured. All modifications will be updated, and a new configuration will be created. If there is a configuration problem, mysql will throw an error and the server will not start. To verify and restart mysql server, run the command below.

$> sudo server mysql status

$> sudo server mysql stop

$> sudo server mysql start

 

Solution 7

Start the MySQL server with the install and initialize option and verify that it is up and running. If you’re using Windows, make sure the mysql server is running as an administrator.

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

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

 

Solution 8

If you’re using Windows and your MySQL database was just installed, go to the menu and select “MySQL Installer – Community” to customize it. Click “reconfigure” in the “Quick Action” tab after selecting “mysql server” in the product tab. The mysql database will be reconfigured as a result of this.

Failed to Connect to MySQL
Rate this post

Advertisement

No comments.

Leave a Reply

Visit blogadda.com to discover Indian blogs DMCA.com Protection Status