How to Fix the “Error establishing a database connection” in WordPress

If you have been using WordPress for any significant length of time, you likely have experienced a number of errors. Some are minor, some grievous, but errors are bound to occur sooner or later. Sometimes this is the result of a breakdown in your system, and other times, due to an action on your part.

One of the most common errors that can occur with WordPress is the Error establishing a database connection.Fix Error Establishing Database ConnectionWordPress is made up of a combination of code written in PHP and a database, typically running MySQL. PHP is required for running the functions on your WordPress site that allow the posting comments, sharing files, retrieving pages, and so forth. WordPress also requires a database to store settings, posts, contributions, links to uploaded files and much more. If the connection to the database cannot be established, only an error message will be visible instead of your website. Although this database connection error can show up anytime, most commonly it occurs shortly after installation.

In this article, we will learn the probable causes of Error establishing a database connection and workable solutions.

Causes of Error Establishing a Database Connection

Some of the reasons are as follows:

#1 The Wrong Database Access Data Is in the WordPress Configuration

This error frequently occurs when the incorrect details for accessing the database connection are stored in the WordPress configuration file (wp-config.php), and as such, WordPress cannot access the database. For example, if you have the incorrect “host” name for the database server, the WordPress will give the database connection error.

#2 Unresponsive Database Server

There are times when your server or host for your database may be unresponsive or dead, causing the error to be generated.

#3 Incorrect Login Details

If you have recently changed your database login details, be sure to keep safe an accurate copy. Incorrect login details containing typing errors, extraneous spaces and errant or missing punctuation marks can also BLOCK the access to the database.

Fixing the Error Establishing a Database Connection Problem

The recommended solutions are:

#1 Checking the Wp-config.php File

The wp-config.php file is responsible for managing the connection to the database. It is the file that contains the data needed to establish a successful connection. In most cases, the error in establishing a connection with the database is in the following lines:

define(‘DB_NAME’, ‘database-name’);

define(‘DB_USER’, ‘database-username’);

define(‘DB_PASSWORD’, ‘database-password’);

define(‘DB_HOST’, ‘localhost’);

These details vary with how and where your WordPress is installed. Typically at the time of installation a database is created with a name, a username and a password, but when migrating to a new hosting account, those details may not be the same.This difference in data will result in the connection failing and the error occurring as a result.

The host of “localhost” is by far the most popular, but not always the correct host. Sometimes you can resolve this wp-config.php error by simply replacing localhost with the IP of your web server. This can often be provided by support for your web host, or found in the upper-left details of your cPanel (if your host uses cPanel — not all do). You can use a free program like Notepad ++ or Sublime Text to edit the wp-config file.

If you have a shared hosting account with a cPanel, you can login to the cPanel, open the File Manager, locate the wp-config.php file and make and save the changes.

If your web hosting account has restricted access, you should contact your provider, for instructions on how to access that information.

There are several ways to access the database and to view or modify the user and password. Let’s start with the simplest:

Again, within cPanel, you may go to the Databases section to make these changes. No cPanel? Look for a place within your hosting’s control panel to edit the “Database”. Once inside there you (hopefully) will be able to see and change the following data:

  • Database Name
  • Username
  • Password

It is key for these database details to be identical to the ones in the wp-config.php.

If there are any discrepancies, you will need to edit the file online, or download it, change the data and re-upload it. To move files by FTP between your computer and the server, FileZilla is one excellent free program.

Once you have edited the wp-config.php file with the new data, your WordPress database connection error should be resolved, and your website will once again be visible.

#2 Check Your Web Host

A large spike in visitor traffic on your site, may also trigger this Error establishing a database connection issue. This can occur if your host server is unable to handle the traffic load which is more likely to happen if you are on shared hosting. To remedy this, you can call or chat with your host provider’s customer support and check with them if the MySQL server is functional.

In case you want to test the MySQL server on your own, you can try some of the following:

#1 Test other sites on the same server and check if they are facing the same issue.

#2 Go to your cPanel, and try to open the phpMyAdmin and connect to the database. If you are able to connect to the database, you should verify if your site database user has enough permission. To do this, create a new file and name it as testconnection.php, and paste the code below in it:

<?php
$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

Check and replace the username and password. Then, save this testconnection.php files to the root directory of your hosting. You can then access it by going to http://mywebsitedomain.com/testconnection.php. You should then see either “Could not connect” followed by an error code, or “Connected successfully”. If you get connected to the database successfully, it means user permission is sufficient, and the issue lies elsewhere. Check your wp-config.php file once more for any typos and extra spaces.

If you are unable to access the database via phpMyAdmin, the problem is most likely related to your server. The best way then to handle this issue is by seeking specialist support. We recommend the team at Maintaingo.

How to Fix the “Error establishing a database connection” in WordPress
Rate this post

Know Everything About WordPress
Best Products in Your Inbox

Subscribe to our mailing list and we will surprise you with amazing WordPress freebies

Jupitar Theme

Know Everything About WordPress
Best Products in Your Inbox

Subscribe to our mailing list and we will surprise you with amazing WordPress freebies