Error Establishing a Database Connection in WordPress

You try to visit your site, and you are seeing the Fatal “Error establishing a database connection”, which makes your website not accessible to the users or you the admin.

This error occurs when WordPress can’t make a connection to your site database. This can be caused by a fair number of things, making it difficult for beginner’s in WordPress to troubleshoot.

In this article, we will show you a few easy fixes that are easy for you as a beginner in fixing the error, establishing a database connection!!!

First, you need to understand the Problem and understand certain factors!!!!! WordPress uses two main languages to host its site, which are PHP and MySQL.

What is PHP?

PHP is a server-side scripting language, which enables it to develop Static websites or Dynamic websites, or Web applications. Which stands for Hypertext Pre-processor, which earlier stood for Personal Home Pages. PHP scripts interpret only on a server that has PHP installed. Learn more about PHP Here

What is SQL?

MySQL is a database technology. WordPress uses it to store your website inclusive your pages, posts, media content, and every information that your site contains. Even the least information is stored on MySQL.

What Causes “Error Establishing a Database Connection in WordPress”?

A lot of issues can cause this error, and we will be listing the most common factors for you.

  1.  Database name, username, password, and server

Without this properly set up your site, your database will fail to connect, and your site remains inaccessible. So, you will need to crosscheck each detail yourself manually, and we will be working you through the process.

You can find this information in your WordPress configuration file called wp-config.php

One of the most common WordPress errors!! Moreover, this error can also happen if your database is corrupted or your hosting server is down.

Fix the Error Establishing a Database Connection in WordPress

Let’s take a look at the most common fix

  1. Cross Check your WordPress Database Credentials.

If your site credentials are incorrect, you definitely will have this error. This error is mainly caused if you recently move your websites to new hosting; if you did, then this is likely to be the cause.

Your WordPress Credentials are stored on a file called wp-config.php. This file contains essential settings about your site and is edited with caution.

How to access your file manager and get to wp-config.php on your cPanel below

hostfacts error in database connection

error in establisihing database connection

clicking on the arrow will take you into your WordPress files scroll down and click on wp-config

error in database connection

error in database connection

when you open the wp-config file, you should look for a line with the code below

You need to be sure that your credentials match, on your MySQL database username, password and host are all correct. You can get all this information from your hosting cPanel.

Confirm this information from your WordPress hosting account dashboard. Log in to your hosting account and click on MySQL databases under the database section.

error establishing a database connection

error in database connection

select your database if you have multiple sites

error in database connection

Select the username of your database and make sure it matched that which you have on your wp-config.php file

After confirming all your database information and all information are correct. Try visiting your website; it should load properly now with no error.

Still seeing the error, cross-check if you missed something wrong using the steps above, you didn’t, right? Then there’s something else wrong its, time to check your database information.

Checking Database information

Your username, password, and database name are all correct, time to check your database localhost information.

90% of WordPress hosting providers use localhost as their database host; only a few WordPress hosting providers use a separate server. You might need to contact your hosting providers to know. Additionally, you can use PHP MyAdmin to find out.

Troubleshoot your database using wp-repair

On a few occasions, you might have a page showing you to repair the database, or you might need to repair your site database, to be sure your database isn’t faulty.

You can do this by adding, the following code to your wp-config file and accessing the wp repair option by using this

define(‘WP_ALLO W_REPAIR’, true);

NB: make sure to replace the URL with your site URL, and you don’t need to be login to do this. After you have done make sure to remove the line of code you added above.

Make sure the database server is up

If your site Is still inaccessible and all your details are correct and you are very sure, you will have to perform a server test, don’t get scared we will put you through this process.

If you are on shared hosting it’s possible that your host server can’t handle the heavy traffic, on its server hence it’s down.

You can contact support to know if your site hosting is down, or preferable if you have several sites on the server you can quickly check to see if it outputs the same error.

If that’s the only site you have, you will need to perform a server connection test to know if your website can connect with your database server.

you need to create a new file and write the below line of code.

N: B Replace the username and password with your site details you can find that on your wp-config file

If the connection is successful, you know your database server is up and running.

By accessing it using your URL with the file name and the extension.

If successful, you get an echo saying successful connection image below.

https mike whogocode com testconnection php

Other troubleshooting methods if you can’t still access your site, but make sure to cross-check for typo errors when filling your database name, username, and password.

However, you can try updating your URL if you recently move your website to another host or you previously built it on a subdomain and transferred it to your main URL.

How to update your site URL?

You can update your site URL by merely performing an SQL query that automatically updates your URL using the following code.

UPDATE wp_options SET option_value = replace(option_value, ‘’, ‘’) WHERE option_name = ‘home’ OR option_name = ‘siteurl’;UPDATE wp_posts SET guid = replace(guid, ‘’,’’);UPDATE wp_posts SET post_content = replace(post_content, ‘’, ‘’);UPDATE wp_postmeta SET meta_value = replace(meta_value,’’,’’);

You might have a different table prefix, make sure to confirm.

Technical Support

You can contact your hosting provider or message us using our contact page, and we will try to put you through.

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email
Share on whatsapp

Leave a Comment


Get our latest blog posts in your inbox