The "Error establishing a database connection" WordPress error is a rather common one. You can either get it upon each request, meaning that you WordPress site is entirely down, or intermittently (randomly and repeatedly). The latter is a nasty issue, because it's not immediately evident: your website seems to be working fine, yet your users will occasionally experience this message:
Here's the text-only version:
Error establishing a database connection
This either means that the username and password information in your wp-config.php file is incorrect or we can’t contact the database server at ds02.ryadel.com. This could mean your host’s database server is down.
Are you sure you have the correct username and password?
Are you sure that you have typed the correct hostname?
Are you sure that the database server is running?
If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums.
In this post, we'll try to have such problem fixed for good, whenever you're getting it everytime or on random basis.
Always-On Error
If you're getting the error everytime, it most likely means that you have to fix your MySQL or MariaDB connection details within your wp-config.php file. More specifically, the options you'll need to triple-check are DB_NAME, DB_USER, DB_PASSWORD and DB_HOST:
1 2 3 4 5 6 7 8 9 10 11 12 |
// ** MySQL settings - You can get this info from your web host ** // /** The name of the database for WordPress */ define( 'DB_NAME', 'database_name_here' ); /** MySQL database username */ define( 'DB_USER', 'username_here' ); /** MySQL database password */ define( 'DB_PASSWORD', 'password_here' ); /** MySQL hostname */ define( 'DB_HOST', 'hostname:port' ); |
The DB_HOST is the most common cause of such error, because sometimes it's not reachable - or not properly mapped in your OS host file:
- On Windows, it's C:\WINDOWS\system32\drivers\etc\hosts
- On Linux systems, it's \etc\hosts
Also, be sure that the TCP port value - the one after the column - is correct: such value can be omitted if your MySQL or MariaDB database is open to the 3306 default TCP port, otherwise you need to specify it.
Intermittent Error
If you've stumbled upon this post, it most likely means that you're not getting this issue every time, but only sometimes: in other words, it happens repeatedly and randomly. The frequence can easily increase (thus becoming consistent) during high-traffic hours, when the website activity reaches its perks.
The first (and hopefully) last thing you can try to address this issue is to increase the max_connections and open_files_limit parameter values of your MySQL / MariaDB database server. This can be easily done by editing the configuration file, which is located here (depending on your OS):
- Windows: C:\Program Files\MySQL\MySQL X.Y\my.ini
- Linux: /etc/my.cnf
Here's an example, allowing 1024 max_connections and 16364 open_files_limit:
1 2 3 4 5 6 |
# The maximum amount of concurrent sessions the MySQL server will # allow. One of these connections will be reserved for a user with # SUPER privileges to allow the administrator to login even if the # connection limit has been reached. max_connections=1024 open_files_limit=16364 |
Before increasing these values, it might be wise to check how your server is currently running: that way you'll be able to increase them slightly, without wasting unnecessary resources on your server.
max_connections
The default value for max_connections is 151 to improve performance when MySQL is used with the Apache Web server, but you can easily check your current server's value by typing the following SQL command:
1 |
SHOW GLOBAL VARIABLES LIKE 'max_connections'; |
The maximum value for the max_connections is 100000, but you'll hardly need that much. For additional info, take a look to the max_connections variable on the MySQL official docs.
open_files_limit
The open_files_limit default value is often 0, because this is often an OS value that MySQL / MariaDB isn't allowed to change in any way. You can check yours by executing the followin MySQL query:
1 |
SHOW GLOBAL VARIABLES LIKE 'max_connections'; |
If you cannot change that value with the open_files_limit value, it means that you have to change your OS open files limit by using ulimit: here's an excellent guide explaining how to do that on most Linux distributions.
Conclusion
As soon as you've applied the changes to your my.cnf or my.ini file, restart the MySQL server and try again: your website database connection error issue should have been fixed for good.