WordPress Error Establishing a Database Connection








Are you trying to solve "Error establishing a database connection" WordPress error?

This guide is for you.


WordPress error "Error establishing a database connection" is a fatal error which can disrupt the functioning of your WordPress Website.

It make your Website inaccessible to the users. 

Here at Ibmi Media, as part of our Website Development Services, we regularly help our Customers to fix WordPress related errors.

In this context, we shall look into how to easily fix the error establishing a database connection in WordPress. 


More information about WordPress error "Error establishing a database connection" ?

The "error establishing a database connection" is probably one of the most common and frightening errors that WordPress users can encounter. 

This error means your website is no longer communicating or has access to your WordPress database, and thus your entire website goes down.

A database is a software which makes it easy to store, organize, and retrieve data efficiently into other software. 

As a content management system, WordPress uses a database to store all your content and other website data. It then connects to the database each time someone visits your website.

WordPress needs the following information for connecting to the database:

i. Database name

ii. Database username

iii. Database password

iv. Database server


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


If any of these items are incorrect, WordPress would fail to connect to your database server, and you will see the "Error establishing a database connection" error.

It is one of the most common WordPress errors. Apart from incorrect credentials, this error can also appear if the database server is down, or the database files are corrupt.


What triggers WordPress error "Error establishing a database connection" ?

The "Error establishing a database connection" error can be caused by the following reasons;

i. Incorrect database information in your WordPress settings.

ii. Corrupt database.

iii. An irresponsive database server. 


When this error occurs, you will see an error message such as;

Error establishing a database connection

How to fix WordPress error "Error establishing a database connection" ?

To fix this WordPress error, follow the step by step troubleshooting process provided below. 


1. Check Your WordPress Database Credentials

The most common reason for database connection error in WordPress is incorrect database credentials. If you have recently moved your WordPress site to a new host, then this could be the most likely reason.

Your WordPress database credentials are stored in the "wp-config.php" file. 

This is the WordPress configuration file that contains important WordPress settings including database information.


If you have not edited "wp-config.php" file before, then take a look at our guide on how to edit wp-config.php file in WordPress.


Then, you will see the following lines in the wp-config.php file:

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

Here, you need to make sure that the information for the database name, username, password, and database host is correct.

If you are using cPanel, You can confirm the information from your WordPress Website hosting account dashboard by follow the steps below;

i. Simply log in to your hosting account and click on MySQL databases under the database section. 

ii. This will take you to the database management page in your hosting dashboard. 

iii. From here, you can find out your database name and the username. 

iv. Once you have confirmed your database name, username, and password, you can change that information in your "wp-config.php" file if needed.


After that, try visiting your website again to see if the database connection error has gone.

If you can still see the error, then this means that something else is wrong. Read below for the other troubleshooting tips.


2. Check Your Database Host Information

If you are confident that your database name, username, and password information is correct, then you may want to make sure that you are using the correct database host information.

Most WordPress hosting companies use localhost as your database host. 

However, some managed WordPress hosting companies use separate servers to host databases. In that case, your database host information will not be localhost.

You need to contact your WordPress hosting company to confirm your database host information. 


3. Repair WordPress Database

If you are getting a different error on the wp-admin, for instance, something like "One or more database tables are unavailable. The database may need to be repaired", then you need to repair your database.

You can do this by adding the following line in your wp-config.php file. Make sure to add it just before "That’s all, stop editing! Happy blogging" line "wp-config.php".

define('WP_ALLOW_REPAIR', true);

Once you have done that, you can see the settings by visiting this page: "http://www.yoursite.com/wp-admin/maint/repair.php".

Note: the user does not need to be logged in to access the database repair page. Once you are done repairing and optimizing your database, make sure to remove this code from your wp-config.php.


4. Check if Your Database Server is Down

If 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.


If you have other websites running on the same server, then you can check those sites to confirm that your SQL server is down.

If you do not have any other site on the same hosting account, then simply go to your hosting dashboard and try to access phpMyAdmin and connect the database.

If you can connect, then we need to verify if your database user has sufficient permission. Create a new file called test_database_connection.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);
?>

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.

Go back to your wp-config file to make sure that everything there is correct (re-scan for typos).


Other Methods to fix WordPress error "Error establishing a database connection" ?

If the above-mentioned troubleshooting tips failed to fix the database connection error on your website, then you may try these additional steps.

Reported by our customers, these steps have helped some users resolve the database connection error on their websites.


1. Update WordPress Site URL

Try updating the WordPress site URL using phpMyAdmin. Simply access phpMyAdmin from your hosting account dashboard, and select your WordPress database. 

After that 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'

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. 


2. Rebooting Web Server

Users on dedicated servers, local server, and virtual private servers (VPS) can try rebooting their servers.

This will restart your web and database server which may fix some temporary glitches causing the error. 


[Still facing WordPress errors? You can also hire WordPress developers that can help you fix this issue for reasonable rates. ]



Conclusion

This article will guide you through the steps to fix the error establishing a database connection in WordPress which can make your WordPress website inaccessible to the users. 


For Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on LinuxAPT.com

Also for Tech related tips, Visit forum.outsourcepath.com or General Technical tips on www.outsourcepath.com