WordPress stores all your site’s information in a database, which is managed by a database server. If, for some reason, WordPress is unable to connect to the database server, then you will get “error establishing a database connection” notice.

It may take some time to detect and fix the problem if you are a new to WordPress.

What Does Error Establishing a Database Connection Mean

The WordPress CMS is written using PHP and MySQL. MySQL database is used to store all the website information, and WordPress uses PHP to store and retrieve information from that database.

Whenever a page is loaded in a browser, there’s a PHP query to retrive information from the database to display a page. The error pretty much says it all – it’s a database connection error. For some reason, the PHP code was unable to connect with your MySQL database to retrieve the information it needs to display that page, hence error!

What Causes Error in Establishing a Database Connection?

The reason why WordPress is unable to establish a database connection can vary. However, there are mainly three reasons why your website could be having trouble connecting to your database.

Sudden Spike in Your Website Traffic: When your WordPress website could be experiencing a big traffic spike your server just couldn’t handle.

Corrupt Database: WordPress database can become corrupt for any reason. One reason can be uploading a bad or non compatible plugin.

Database Server is Down: Your database’s server may be down due to a problem or error is the part of your WordPress web hosting company,

Wrong Login Credentials: Your database uses separate login credentials (username and password). If those credentials have recently been changed, your website won’t be able to access the database with the old credentials.

Troubleshooting “Error Establishing a Database Connection”

Repair Your Database: Before carrying out this step, make sure the error occurs on front-end of the site as well as  back-end, For example: wp-admin, While accessing the wp-admin, when you get an error like “One or more database tables are unavailable,” you may need to repair your database.

WordPress has a built-in way to attempt to repair your database, but you’ll have to enable this feature first. To repair your database simply add the following line of code in your wp-config.php file.

define('WP_ALLOW_REPAIR', true);

You can find wp-config in your root WordPress file directory. You can access it by using an FTP client or by logging into your cPanel, selecting File Manager, and navigating to the folder where you installed WordPress.

Once done, you need to visit http://www.yoursite.com/wp-admin/maint/repair.php (just replace “yourwebsite.com” with your actual URL).

Once you are done repairing and optimizing your database, make sure to remove this code from your wp-config.php since it can be accessed by anyone.

Other ways of repairing and optimizing a corrupt database, is through PHPMyAdmin in cPanel.

Check Login Credentials in wp-config.php: Log into your hosting cPanel and open the File Manager. Navigate to the directory where you installed WordPress, and look for a file called wp-config.php Open that file and take a look at your database settings in your wp-config.

It should look something like this:

// ** 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', 'localhost' );

There are four important data that a WordPress CMS needs to access your database:

  • The name of the database (“DB_NAME”)
  • The login username (“DB_USER”)
  • The login password (“DB_PASSWORD”)
  • The database host (“DB_HOST”)

If any one of these values are changed recently, WordPress will not be able to connect with the database.

Test Your MySQL Server: When you are on shared hosting a sudden spike in traffic will lead to this Error establishing database connection notice. Login to your hosting cPanel and try to access phpMyAdmin and connect the database. If you cannot connect to the database by going to phpMyAdmin, then you know it is something with your server.

Create a New Database User and Password: You can use tool available in cPanel called “MySQL Databases.” Click on that, then click MySQL Users: “Add New User.” Choose a username and  password.Next, Click “Create User.” Now scroll down to the heading “Add User To Database,” and choose your new username and your WordPress database, and click “Add.” Once done you need to update your wp-config file with this new username and password.

Check With Your Hosting Company: If you still not able to fix Error Establishing A Database Connection notice, then its time to contact your hosting company. It’s possible that your MySQL server might have low memory. A recent upgrade to your WordPress core files might have changed your memory requirements. MySQL tends to crash when memory is too low for it to work. You can see this in your server when you try to start your MySQL server, but you get an error and cannot start it.

When nothing seems to be working for you, you can always delete the existing WordPress installation on the server and replace it with a fresh copy from WordPress.org.