Here is How to Fix Error Establishing a Database Connection in WordPress

Error Establishing a Database Connection is one of the most commons yet fatal errors that make your WordPress site inaccessible. This error means that WordPress is unable to make a connection to the database. A database is a software which makes it easy to store, organize, and retrieve data into other software. Now, there can be different reasons behind this error and for a beginner to find out the solution is not so easy.

In this post (and video at the end of the post), we will show you how to easily and quickly fix the error establishing a database connection in WordPress. 

Main Causes of Error Establishing a Database Connection in WordPress

This is one of the primary and most important question that what are the factors or reasons those cause this error. There can be many reasons behind it but the top reasons include incorrect database information in your WordPress settings, corrupt database, or an irresponsive database server.

How WordPress Connects to the Database?

It is very important, especially for a beginner, to understand how WordPress connects to the database. As stated earlier, a database is a software which makes it easy to store, organize and retrieve data into other software. WordPress, as a Content Management System (CMS), uses a database to store all your content and other website data. It then connects to the database each time someone visits your website.

What Information WordPress Needs to Connect with Database?

Primarily, WordPress needs the following information for connecting to the database:

  • Database name
  • Database username
  • Database password
  • Database server

This information is stored in your WordPress configuration file called wp-config.php.

If any of the above information is incorrect, WordPress would fail to connect to your database server, and eventually you’ll see the ‘Error establishing a database connection’ error. Isn’t it simple? Well, it should be now.

Apart from the above common factors, Error Establishing a Database Connection can be caused if the database server is down, or the database files are corrupt.

Interested in Learning WordPress? Great News! Here is the complete WordPress Course for FREE right now.

Fixing Error Establishing a Database Connection

Anyhow, you’re facing any type of error, the most possible solutions are discussed as under. The solutions are based on the cases those have worked for most of the sites. You need to start from the first one and move to the later options.

  1. By Checking Your WordPress Database Credentials
  2. By checking Your Database Host Information
  3. By Repairing WordPress Database
  4. By Checking if Database Server is Down
  5. By Updating WordPress Site URL
  6. By Rebooting Web Server

By Checking Your WordPress Database Credentials

Earlier we’ve talked by WordPress Database credentials which include database name, username, password, and server. Error in any of these causes the error. This case usually occurs if you have recently moved your WordPress site to a new host.

wp-config.php

wp-config.php file is the place where your WordPress database credentials are stored. It is the WordPress configuration file that contains important WordPress settings including database information. In the wp-config.php file, look for the following lines:

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

All you need to do is to check whether the information about database name, username, password and host are correct.

Accessing wp-config.php File in cPanel

Follow the steps to access or check your wp-config.php file in cPanel.

First Step – You need to login to your cPanel. To access cPanel, type your site name and add /cpanel for instance techurdu.net/cpanel. Write your username and password and then login.

Second Step – Go to File Manager. Click on it. It will open a new window.

Third Step – Locate public_html file and click on it.

Fourth Step – On the right side panel, scroll to find the wp-config.php

Image Source: Tech Urdu YouTube

Fifth Step – Double tap on it and it will be downloaded as a notepad file.

Now you can simple check your WordPress database credentials.

Checking and Editing WordPress Database Credentials

Now, it may happen that you will not sure of don’t know your database information. Well, worry not. Just follow these simple steps:

First Step – You need to login to your cPanel. To access cPanel, type your site name and add /cpanel for instance techurdu.net/cpanel. Write your username and password and then login.

Second Step – Go to DATABASES section and clink on MySQL Databases. This will take you to the database management page in your hosting dashboard. From here, you can Create New Database, Modify Databases, check Current Databases, check MySQL Users, Add New User, Add User to Database, and you can check Current Users. You can find out your database name and the username and change the user password.

Image Source: Tech Urdu YouTube

Again back to wp-config.php, you have not got your database name, username, and password, you can change that information in your wp-config.php file if needed.

Editing wp-config.php File

By this time, you have understood that WordPress stores your database information in the wp-config.php file. Without this information your WordPress website will not work, and you will get the ‘error establishing database connection‘ error.

Important: wp-config.php contains a lot of sensitive information, it is recommended that you don’t mess with this file unless you have absolutely no other choice.

Follow the steps given in this article to edit your wo-config,php file.

Once you’re done with the editing part, try visiting your website again to see if the database connection error has gone. If you still see the error, then keep reading the post (or watch the below video).

By checking Your Database Host Information

Once you see no problem in your WordPress Database Credentials, you need to check that you are using the correct database host information. You can simply get this information by contacting your WordPress hosting company to confirm your database host information.

By Repairing WordPress Database

The error being discussed can also result from some other error in WordPress activity like you are getting a different error on the wp-admin for example something like “One or more database tables are unavailable. The database may need to be repaired”, then you need to repair your database.

Again you need to edit and add the following line in the wp-config.php file.

define('WP_ALLOW_REPAIR', true);

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

Once you have done that, you can see the settings by visiting this page: https://www.techurdu.net/wp-admin/maint/repair.php [Replace techurdu.net with your site name]

Image Source: Tech Urdu YouTube

Once you are done repairing and optimizing your database, make sure to remove this code from your wp-config.php as this is no more needed.

By Checking if Database Server is Down

After trying above steps and everything seems to be correct, and WordPress still cannot connect to the database, then your database server (MySQL server) may be down. This could happen due to heavy traffic on a server. Basically, your host server just cannot handle the load (especially when you are on shared hosting).

Your site will get really slow and for some users it may even output the error. So the best thing you should do is get on the phone or live chat with your hosting provider and ask them if your MySQL server is responsive or upgrade from a Shared Hosting.

To check whether your database server hosting is down or not, if you have other websites running on the same server, then you can check those sites to confirm that your SQL server is down.

You may also be interested in How to Solve Cpanel – Disk Usage Warning & Inodes Problem

Checking Database Server Down or Not from cPanel

Step 1– Go to your hosting dashboard and try to access phpMyAdmin (under Databases) and connect the database.

Step 2 – If you can connect, then need to verify if your database user has sufficient permission. Create a new file called testconnection.php and paste the following code in it:

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

Important: Make sure to replace the username and password. You can now upload this file to your website and access it via web browser.

If the script connected successfully, then it means that your user has sufficient permission, and there is something else that is wrong.

By Updating WordPress Site URL

This case happens in least circumstances, yet this is one of the solutions. Here, we will simply try updating the WordPress site URL using phpMyAdmin by following these simple steps:

Step 1 – Access phpMyAdmin from your hosting account dashboard/cPanel. It will open in a new window.

Step 2 – Select your WordPress database.

Step 2 – Click on the SQL menu on the top and enter the following MySQL query.

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

Image Source: Tech Urdu YouTube

** Don’t forget to provide your own site URL and change wp_options to your own table name as you may have changed the WordPress table prefix.

By Rebooting Web Server

If you are on dedicated servers, local server, or virtual private servers (VPS), then you can try rebooting your server. This will restart your web and database server which may fix some temporary glitches causing the error.

By Contact Web Hosting Company

If everything else fails, then you may need to contact your web hosting company. All good WordPress hosting companies will help you fix the problem, point you in the right direction, or even fix it for you.

[Video Tutorial] (Solution) Error Establishing a Database Connection in WordPress [Hindi/Urdu]

So let us know in comments below which method worked for you. If there is another working method, do let us know.

Courtesy: wpbeginner

Naeem Javid

Naeem Javid Muhammad Hassani is working as Deputy Conservator of Forests in Balochistan Forest & Wildlife Department (BFWD). He is the CEO of Tech Urdu (techurdu.net) Forestrypedia (forestrypedia.com), All Pak Notifications (allpaknotifications.com), Essayspedia, etc & their YouTube Channels). He is an Environmentalist, Blogger, YouTuber, Developer & Vlogger.

Please, Let us know what you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.