Fix the Error Establishing a Database Connection in WordPress
Home » Blog » How to » How to Fix the Error Establishing a Database Connection in WordPress

Has your website become inaccessible to users? Has WordPress started sending you that dreaded ‘Error establishing a database connection’ notice?

If yes then you may be experiencing an error in WordPress that stops your website from accessing the database. This fatal error can be caused by several reasons. Therefore troubleshooting this issue may be difficult for beginners.

This article will help you fix an ”error establishing a database connection” problem easily in your WordPress site.

Fix the Error Establishing a Database Connection in WordPress

Before we begin troubleshooting, let’s first understand

How a WordPress site works

All of your website information is stored in a MySQL database. WordPress uses a coding language called PHP to retrieve and store information from that database. The data retrieved from your MySQL database is the very framework needed for your website to function.

Why you face Error Establishing a Database Connection in WordPress?

So if you are experiencing an error establishing a database connection that means for some reason, the PHP code was unable to connect with your MySQL database. Thus your website does not have access to the database that makes up its structure. Since your website cannot access your MySQL database then it has no way of knowing what to display.

How to Fix the Error Establishing a Database Connection in WordPress? Click To Tweet

You may be experiencing this issue for a large number of reasons, but generally, these causes may be summarized into four categories. Let’s tackle each category and show possible solutions.

Read our aticle to fix DNS_PROBE_FINISHED_NXDOMAIN Error.

Step-1: The Login Credentials For Your Database Are Wrong

This is the most common cause of an error establishing a database connection. Your database uses separate login credentials, so if you recently changed either of them then your website won’t be able to access the database with the old credentials.

To fix this issue, check your database credentials, these are found in the wp-config.ph file. Here you will find information from your WordPress configuration file that contains settings crucial for building your WordPress site.

If you haven’t configured the wp-config.php file in the past then follow these steps to ensure your proper login credentials.

Inside the wp-config.php file look for the following lines of code

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

Once you do, make sure that the database name, password, username, and host are correct. 

To confirm this login and open to your WordPress hosting account. Once in, click your MySQL database under the database section. From here you will go to the database management page where you can find out more about your database name and username.

MySQL database under the database section

You will also find the database users and links to change your password below.

After confirming the correct database name, password and username, configure your wp-config.php file if needed.

Now try visiting your site again and see if the error establishing database connection is gone. If not then this means something else is causing the error.

Step-2: Your database server is down

server down causes Error Establishing Database Connection

Your website hosting provider may be experiencing an issue at the moment causing your database to be inaccessible. They may be experiencing a big traffic spike beyond what they are capable of handling. If so then your site is bound to run slower and end up triggering an error in the output of the users.

If this happens then it’s time to contact your hosting provider via call or live chat. Ask them if your database server is responding or not. 

If you own other websites with the same server, you can check those as well to see if your SQL server is down.

If you only own one site on your hosting provider then simply open your hosting dashboard and access the phpMyAdmin to connect to your database. We need to verify if your database user has sufficient permission. 

Make a new file named testconnection.php. Copy and paste the following lines into it:

<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>

Don’t forget to replace the username and passwords with the correct data of your account. 

Upload this file to your website and open it on a web browser. If the code was successfully connected then that means your user has sufficient connections and something else is causing the problem of your database error. 

You can revisit your code and re-check for typos. If you find however that it doesn’t change the situation of your site then try the next options.

Step-3: Your database is corrupted

corrupted database cause error Establishing a Database Connection

If you get an error when you try accessing the WordPress admin dashboard then you may have to repair your database. There are many reasons for your WordPress database to become corrupted. To troubleshoot this, WordPress has a built-in tool to repair your corrupted database. You have to enable this feature first to do this.

To do this, start by adding the following line in your wp-config.php file. 

define(‘WP_ALLOW_REPAIR’, true);

Make sure to add the line before ‘That’s all, stop editing! Happy blogging’ line wp-config.php.

After adding the code check your settings by visiting: http://www.yoursite.com/wp-admin/maint/repair.php.

Revisit your website to see if the database error is gone….

Step-4: Incorrect database host information

Let’s say you have tried all the steps above and you are confident that your database name, username, and password information is correct, you may want to confirm if you are using the correct database host information.

The majority of WordPress hosting providers use localhost for database hosting. Other hosting providers use separate servers to host databases. If this is the case then your database host information will not be the same as that of your localhost.

If so, contact your hosting provider to confirm your correct database host information.

Once you get the information, configure your database host information for your WordPress site the revisit your website to see if the error has been resolved.

How to Fix the Error Establishing a Database Connection in WordPress? Click To Tweet

Overview

Hope this article helped you fix the database error in your WordPress website. In conclusion, there may not be a 100% fail-proof way to protect your site against any error establishing a database connection, but you can always be safe by having quality hosting and backups.

Don’t neglect to check your database in case of an error. If left unnoticed, it can lead to making severe problems for your users trying to access your website.

Hey, I am the guy behind wpblog.info. Actually, when I start using Wordpress I was very curious about customizing my website with New themes and Add new features with plugins but I always try to do things with free themes and plugins. Every time I try to figure out the best themes and plugins I faced different problems and gain different kinds of experiences. That's why I decided to start a blog where I will share and suggest the best theme and plugins so that you can find the best things for your websites.

You may also like

Leave a reply

Your email address will not be published. Required fields are marked *

More in:Guide