Bobcares

Cannot connect to MySQL Server on localhost 3306 | How to fix it?

by | May 30, 2023

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:

cannot connect to mysql server on 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 <ip of the remote server>

$> 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 <host name/ip>

if the port is different from the default port 3306

$> mysql -u root -p -h <host name/ip> -P <port>
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.

# to refer the localhost
bind-address = 127.0.0.1

# to refer for any address
bind-address = 0.0.0.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.

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

C:\Program Files\MySQL\MySQL Server\bin>mysqld --initialize
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.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

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

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF