This article will assist you to fix the common causes of MySQL connection errors along with steps to resolve them.
Check If MySQL Server is Running
When you receive MySQL connection errors the first thing you should check is if MySQL server is running or not. MySQL server needs to be started before it accepts the connections from clients. On Windows machines MySQL service can be started from the services control panel (Start >> Run >> type services.msc + enter). On Linux machines MySQL server can be started either through cPanel/WHM or from Linux terminal. You can also check our knowledgebase article on How to start MySQL service in Windows and Linux machines.
Check if MySQL server is listening on the default port
By default MySQL server listens on port 3306 however, it can be changed for each MySQL instance. If you have configured MySQL server to listen to some other port, the same port must be specified while clients connect to MySQL server. To find the port on which MySQL is listening to, check out your mysql configuration file (my.ini or my.cnf).
Check if skip-networking configuration option is enabled in my.ini or my.cnf
Make sure that your MySQL server is not configured to ignore network connections. If the skip-networking option is not commented out in the MySQL configuration file, clients would not be able to connect to MySQL instance via TCP/IP. To turn off the skip-networking option, edit the MySQL configuration file (my.ini or my.cnf), locate the line containing the text skip-networking and comment it out appending with #.
Database username does not have privileges to connect
MySQL database users must have appropriate connection privileges to be able to connect to MySQL instance. These privileges can be specified while you create a database username or else you can edit an existing username and specify necessary permissions. You can directly set permissions from web based MySQL server manager like phpMyAdmin or through Linux terminal. For example, following MySQL statements will give the user dbuser all privileges, including connection privileges (for localhost only).
GRANT ALL PRIVILEGES ON *.* TO 'dbuser'@'localhost';
Following command can be used to give user the dbuser all privileges including the permission to connect remotely.
GRANT ALL PRIVILEGES ON *.* TO 'dbuser'@'localhost' IDENTIFIED BY 'your-password' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'dbuser'@'%' IDENTIFIED BY 'your-password' WITH GRANT OPTION;
bind_address configuration option is set to 127.0.0.1 in my.ini or my.cnf
If the bind_address configuration is set to 127.0.0.1 in the MySQL configuration file, MySQL server won't accept connections from remote hosts. To allow remote connections, you can remove or comment out the line bind_address line in the MySQL configuration file.
Check if firewall is blocking incoming connections to MySQL server
The firewall where the MySQL server resides must be configured to allow transmission via the port on which MySQL is listening. If the MySQL port is blocked in server firewall, the client will not be able to connect. Check out the following articles to open ports in Windows and cPanel/WHM machines.
Check if MySQL Server has reached the maximum number of connection limit
The FLUSH HOSTS command is used specifically with the host cache tables. If you are unable to connect to your MySQL server, the most common reason is that the maximum number of connections has been reached for a particular host so it’s throwing errors. You need to fire the following command in the terminal to clear the cache.
When MySQL gets numerous errors on connection, it assumes something is missing and it just blocks any additional connection attempts to that particular host. The FLUSH HOSTS command resets this process and again allows to connect to that particular HOST.