How to fix the error establishing a database connection in WordPress

How to fix the error establishing a database connection in WordPress
Share With Friends

Whether during the installation or later during operation: the error message “Error establishing a database connection” is not uncommon. But this does not mean that you have to set up your blog from scratch. Here we will tell how to fix the error establishing a database connection in WordPress with solution.

# Possible causes of the error

Your WordPress blog needs a database to run smoothly. If the connection to this database cannot be established, yourBlogs
A blog is a special form of a website that has the character of a public diary or magazine. Morethe above error appears in the browser. Since there can be different causes for this problem, you need to narrow down the reasons first to solve how to fix the error establishing a database connection in WordPress are.

First test whether the error occurred when establishing the database connection both on the start page and in the Admin area
The administration area is the WordPress control center for important system settings and is only accessible to authorized registered users. More
is specified. If you see another error message in WP-Admin, your database is damaged. This can happen, for example, if a database entry is being written while the connection to a visitor is lost.

Here are the best solotion to How to fix the error establishing a database connection in WordPress

# What causes the error message “Failed to establish a database connection” in WordPress?

The reasons WordPress can’t connect to its database server can be many. The most common and common are listed below and fix it how to fix the error establishing a database connection in WordPress with solution:

  • Invalid database credentials: The main reason this error occurs is due to invalid credentials being used by WordPress to connect to the database server. If the credentials WordPress is using to connect to its database server are not valid, then this error occurs because WordPress cannot connect to the database due to incorrect credentials.
  • Corrupted Database: If your database has been corrupted, there is a very good chance you will get this error. By corruption I mean that some of the tables / columns in the database may be changed and WordPress will not be able to get the desired data values ​​from those particular tables / columns in the database.
  • Database server problems: Another common cause of this error is a failed database server. For example, if you’ve hosted your website somewhere on a web server and the database server is there too but is not online or unable to connect, you will see this error.
  • Corrupted WordPress Files: Another possible reason for this error is that there are corrupted files in your WordPress installation directory. Often times, hackers can access your WordPress website and modify the files in your WordPress directory by adding new files or deleting existing ones, or optimizing with your original files. Well, whatever the case, if you have corrupt files in your WordPress directory, you might get this error because of that.
  • Insufficient PHP memory: The error message can also appear if your web server does not have sufficient PHP memory. In this case you will need to update the php.ini file to increase the PHP memory.

# Solution 1: Check your wp-config.php file in your WordPress installation directory

The first thing you should do to fix  this error is to look at the wp-config.php file in your WordPress directory. You should see whether the content of the wp-config.php file has been changed or not.

To do this, you can either use the file manager provided in your web hosting’s cPanel to access the wp-config.php file, or you can use an FTP application such as Filezilla to connect to your web server and access the file in a text editor installed on your local computer.

If you have access to the wp-config.php file, make sure these lines are present:

define (‘DB_NAME’, ‘database name’); define (‘DB_USER’, ‘Database Username’); define (‘DB_PASSWORD’, ‘Database Password’); define (‘DB_HOST’, ‘localhost’)

how to fix the error establishing database connection in WordPress

FileZilla Manager

The value of DB_HOST might not be localhost if the database server does not have the same IP address as your web server (this can usually be the case if you did not host your website locally on your computer).

# Solution 2: Try making a dummy connection to your database server

Next, you should verify that you can successfully connect to the database server using the username and password for your database user account.

Create a new test data base connection .php file in the root directory of your website with the following contents and access it through your browser. If you get a “Successfully connected” message, the MySQL database server is not faulty and is working properly.

Remember to change the Username and Password fields with the username and password for your database user account.

If you are wondering about the username and password of the database user account, then don’t worry. You can see it in your cPanel of your web hosting account in the database section. Or if you created / configured the database yourself, you will probably remember the username and password.

If you get an error message instead of the “Successfully connected” message, make sure that you are using the correct username and password to connect to your database server.

An alternative would be to create a different user for your database server with full permissions and use the same user account to access your database.

After you have created a new user account, you can test this account using the method described above. If this succeeds, you will need to modify the contents of the wp-config.php file to match the username and password of the database account you are using just created.


# Solution 3: repair your database (in case it is damaged)

If the above steps fail and you are still unable to connect to your database, then you should consider repairing your database as it may be damaged. The good news here is that WordPress has a repair database mode that you can initiate by adding a line to your wp-config.php.


  1. To do this, add this line to your wp-config.php.define (‘WP_ALLOW_REPAIR’, true);
  2. After adding and saving the file, go to this link to start the database repair mode. Http://
  3. Change to match your website’s domain.
  4. When you have accessed the page, click the “Repair Database” option.

Repair database
It will take time and it will be over. Then you should check whether this error still occurs or not.

# Solution 4: running a SQL query in PhpMyAdmin

One user suggested eliminating this error message by running the following SQL query in PhpMyAdmin on the web host cPanel (PhpMyAdmin is a web based application used to manage various aspects of your database).

UPDATE wp_options SET option_value = “YOUR_SITE_URL” WHERE option_name = “siteurl”

Remember to change the YOUR_SITE_URL with the link on your website.

After that, you can see whether or not your error has been fixed.

php My Admin how to fix the error establishing database connection in WordPress

# Solution 5: reinstall the core WordPress files (don’t worry, it won’t affect your website)

After trying all of the above solutions and not getting anything working for you, the best thing to do is to try reinstalling the core WordPress files.

  1. To do this, go to and download the latest WordPress zip file.
  2. Then use Filezilla to connect to your web server and navigate to the WordPress installation directory.
  3. There delete the folder ‘wp-content’ and the file ‘wp-config-sample.php’ and upload the downloaded WordPress zip file. After the upload you have to extract the contents of the zip file in this directory (not in a subdirectory).

Remove WordPress

  • 4. Extracting zip files can be done either via Filezilla or via the file manager of your cPanel, as most web servers support extracting zip files.
    5. That’s it. Now check again if your WordPress website is still getting the same error or not.
Share With Friends