WordPress – Error establishing a database connection repeatedly and intermittently (MySQL / MariaDB) – Fix What to do if you're randomly and repeatedly getting the "Error establishing a database connection" with your WordPress installation using MySQL or MariaDB

Wordpress - Error establishing a database connection repeatedly and intermittently (MySQL / MariaDB) - Fix

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:

Wordpress - Error establishing a database connection repeatedly and intermittently (MySQL / MariaDB) - Fix

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:

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
  • Linux:  

Here’s an example, allowing 1024 max_connections and 16364 open_files_limit:

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:

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:

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.

 

RELATED POSTS

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies.

View all posts by Ryan