Error Establishing A Database Connection – WordPress Fix

Fix Error Establishing A Database Connection

Error Establishing A Database Connection is one of the most common errors in the most popular CMS WordPress. You might have seen this type of error at least once while browsing different websites over the Internet and this time, maybe this error is bothering you, that’s why you’re here to find a fix. 😅 As the error justifies itself, your WordPress Site is not able to establish a connection with the Database. As you might be knowing that most of the data is stored in the Database, so without connection, your site can’t simply work. If you’re facing this error, don’t worry, you’re not alone a lot of people face this error daily because of numerous reasons and the good thing is it is easily fixable.

But before finding the solution to this problem, you need to find the cause first. There is no exact or one particular cause for this problem. It could be caused due to any of the multiple issues. In this article, I’ve tried to cover all those issues because of which in common this error is caused and have also provided an easy fix for all.

It is a connection error and the chances are there is no problem with the data in the database and its structure. So, before we start to find out the cause and trying to solve the problem, I highly recommend you to backup your site’s database first of all. It’s really important if you don’t want to lose anything on your site that you’ve created after lots of hard work. For a better assurance of your site’s overall data safely, prefer doing a complete backup of your files and the database as well.

If you don’t know how you can backup your WordPress website or blog’s database, then here’s an easy tutorial for the same.

And in case you also want to do the complete backup (files + database), here’s another tutorial.

Now there could be two states.

  1. Your complete website (all pages + admin area) showing this error.
  2. Only frontend website pages (viewable to the public) are showing this error.
  3. You’re getting Database errors on both but Errors are Different for Admin area and Frontend Pages.

If there’s the first case, then you are not even able to login to your WordPress website and make any changes via the Admin Area. (your-site.com/wp-admin/)

In such a case, you also have to do the backup of your database manually by going to database software like phpMyAdmin and to do files backup you have to connect to your host via FTP Client of File Manager offered by your control panel like cPanel.

In the second case, you’re able to access your wp-admin area and things are lesser complex. You, visitors, are not able to view any of the frontend pages where this error is being displayed by WordPress. In such a case, you can do a backup using methods specified in the above-given tutorials by easily logging in to the WordPress Dashboard. In second case, sometimes, chances are that the wp-admin area might not show the Error Establishing A Database Connection Error but show some other types of warnings. You can try understanding those warning to find a solution quicker.

The third case is that you’re getting an error on both but the thing is it is showing some different error in the Admin Area and some different in the frontend. Most commonly chances are in Frontend if there’s any database error, it will only show the connection error as any internal database tables level errors should not be revealed to the public. But obviously, Admin Area errors need to be more justified so the website admin can have a better idea of the problem.

Steps to Solve the Error Establishing A Database Connection

Anyways, here below, I’ve given the different steps that you could try to solve the problem.

  1. Try Repairing the Database
  2. Check WP-Config For DB Configuration Details
  3. Check the SQL Server Errors and Fix them if any
  4. Try Replacing WordPress With Fresh Core Files
  5. Try Other Suggested Solutions by the users

Try Repairing the Database

You should try repairing your database if you feel that connection is not being established due to structural issues or missing tables in the database.

Find out the wp-config.php file in your WordPress Site’s root folder and open it in any editor. Add the following line of code just before the ending line of the wp-config.php file that says, ‘That’s all, stop editing! Happy blogging’

define('WP_ALLOW_REPAIR', true);

Allowing Repair In Wp Config.php File

This will allow you to repair the database of your WordPress site. Thanks to WordPress that has an automated database repair mechanism. After adding this in the configuration file, save it and now go to the following URL.

http://your-website.com/wp-admin/maint/repair.php

This will open the Database Repair page of your WordPress site. As you’ve said to allow the repair of the database in the configuration file, you need not log into your dashboard to access the page. It is publically accessible. The reason for this page being public is that in most cases of such errors, users are not able to access the wp-admin area and that’s why this page has to be accessed without logging in. If you would not add the above-specified line of code in the wp-config.php file, it will ask you to add so if you want to access the repair page as shown in the screenshot given below.

Allow Repair In WordPress Configuration File

If added, the repair page will open as shown below and you can simply click any of the two buttons, to simply repair or repair & optimize the database.

Database Repair For Error Establishing A Database Connection Error

As this page is public, so make sure to remove the added line of code from your site’s wp-config.php file as soon as you’re done repairing or optimizing the database.

If this doesn’t solve your error, then the problem is something else and is not related to the structure of the database. It is more specifically something in between your host and database connectivity. Move on to the next section.

Checking Settings in wp-config.php File

The wp-config.php file contains the information and credentials that allow WordPress to connect with your database. You need to check if there’s an error in this information. In order to connect with the database in any of the applications, we usually need four things.

  • Host Name or Address
  • Database Name
  • Database User Name
  • Database User Password

All these four things should be correct for a successful connection. in WordPress, all these four things are defined in the wp-config.php file. You need to find out the following lines in the configuration file.

define('DB_NAME', 'your-database-name');
define('DB_USER', 'your-database-username');
define('DB_PASSWORD', 'your-database-password');
define('DB_HOST', 'localhost');

Here you need to make sure that the constants, DB_NAME, DB_USER, DB_PASSWORD and DB_HOST have the correct values as per your database. DB_HOST mostly has the value localhost unless you’re using any Remote SQL.

Some web hosting services have different values for the DB_HOST parameter. You need to check this value with your host. DB_HOST value could be as complex as given below.

define('DB_HOST', '156.14.11.234:3483');

define('DB_HOST', 'localhost:5532');

define('DB_HOST', '127.0.0.1');

In the above code, 3483 is the Port Number of SQL Connection. Also, make sure you’re using the correct port number. When people migrate from one web hosting to another and get this error, then most probably the error because of these parameters.

Usually, the issues are with the wrong username, password or database name. So, this error occurs if recently you changed any of these. So, simply correct it, save the file and try reloading the pages of your site and see if the error is resolved or not.

If the pages of your website still say, Error Establishing A Database Connection, issues maybe with your SQL Server, move on to the next section.

SQL Server Errors

If WordPress has the correct connectivity details, even then it is not able to establish the connection, then there are chances that the issue is with your SQL Server. You can contact your web host to check.

One most common cause of this error related to the SQL Server is the traffic. A lot of people observer this error when they start getting a huge number of visitors to their website. The SQL Server is not able to serve so many requests and is not able to establish multiple connections when it crosses a particular limit of the server, this error occurs. Your web host sets a number of how many multiple connections are allowed. So, you can check with your hosting details.

If you feel this error is not coming for all users and it comes sometimes, and sometimes later, this goes on normal and error disappears. Then the error is definitely due to SQL Server issues. WordPress makes use of PHP as the server-side scripting language. You can, therefore, test the database connectivity by creating a test connection file.

Go ahead and create a PHP file in the WordPress root directory. Give it any name, let’s say, dbcheck.php

Add the following code to check the database connectivity. The following code is for MySQL Server.

<?php
$host = "localhost";
$username = "yourUsername";
$password = "yourPassword";
$database = "yourDatabase";

// Establish Connection
$conn = mysqli_connect($host, $username, $password, $database);

// Connection Verification
if (!$conn) {
    die("Could Not Connect To Database: " . mysqli_connect_error());
}
echo "Database Connection Established Successfully.";
mysqli_close($conn);
?>

You can run this PHP file by visiting the following URL if your file is at the same location as of the wp-config.php file.

http://your-site.com/dbcheck.php (If file’s name is dbcheck.php)

If it works and says that Database Connection Established Successfully, the MySQL Server is working properly. Try running it multiple times to check if it successfully establishes connections all the time or not.

If there’s an error this will also you the error. One example of an error that this file may show is given below.

Could Not Connect To Database: Access denied for user ‘your-username’@’%’ (using password: YES)

This is one of the most common errors in MySQL if you’re trying to connect with the database with the Wrong Password. If you’re getting this, make sure you’re using the right database user password. For many people, there are database privilege issues. You can try entering root credentials.

Simply proving credentials of the root user of the SQL Server solves this error for many people.

Replace WordPress Core Files with Fresh New Files

You can try replacing the core files from your WordPress Installation with fresh new files. Simply download the fresh WordPress files from the official website wordpress.org, extract it and replace it with the existing files. Make sure you’ve complete files backup before doing any of these changes. Kisnta has suggested the different ways to reinstall and replace the WordPress files with fresh new files in this linked article.

Other Suggested Solutions for Error Establishing A Database Connection

As there could be different reasons for this error for different people. A lot of people explain online what worked for them and why it may works for you as well. So, we’ve compiled this section of other suggested solutions by reviewing what different people say and how they solved their problem of Error Establishing A Database Connection in WordPress.

Changing siteurl in wp_options Table

Sometimes, some malicious plugin or an attack on your WordPress site may change the siteurl option in your WordPress website’s database. So, changing this option to the actual and correct URL may really help to fix the error establishing a database connection error.

You can execute the following SQL query to change the siteurl option in the WordPress options table.

UPDATE wp_options SET option_value='YOUR_ACTUAL_SITE_URL' WHERE option_name='siteurl'

Also, you can update the siteurl option using the phpMyAdmin GUI provided by most of the web hosting providers. The screenshot for the same is given below. Simply open the wp_options table, the siteurl is at the top of the table simply double click on the value to edit.

PhpMyAdmin Wp Options Table Changes

Database Privileges Error

Sometimes, the issue is because of privileges to the user who is demanding the connectivity. As I already suggested, try connecting with the root user, if error is resolved with it, then the issue is definitely with the SQL Server user and its privileges. Try assigning a new database user with full privileges.

As we’ll come to know about more causes and their solutions, we’ll update this section accordingly.

I hope, this article helped you. If so do share it with others who are looking for a solution to this problem. The comments box is open for your questions, feel free to ask!

Also, if you’ve also faced this error now or in the past, please share your solution or what works in your case in the comments section below. This could really help a lot of users in getting rid of this error! Also, don’t forget to subscribe to WTMatter for the latest and useful articles.


Subscribe To WTMatter

Leave a Reply

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