How to Fix the Error Establishing a Database Connection in WordPress

How to Fix the Error Establishing a Database Connection in WordPress

Do you notice the ‘Error establishing a database connection’ message on your WordPress website? This tutorial will discuss how to fix the error establishing a database connection in WordPress.

It is a destructive error that makes your WordPress website unavailable to the users. This error appears when WordPress cannot make a connection to the database. 

A lot of things can affect your WordPress database connection which makes it difficult for users to fix the issue.

Don’t worry. Step ahead to fix the error establishing a database connection in WordPress.

Why does the Error Establishing a Database Connection occur in WordPress?

The ‘Error establishing a database connection problem may occur by incorrect database details in your WordPress settings, an irresponsive database server, or corrupt database.

A database easily organizes, store, and retrieve data into other software.

As a CMS, WordPress utilizes a database to store your content and all other website data. Each time someone visits your site, it connects to the database.

WordPress requires the Database name, username, password, and server details for connecting to the database.

These details are stored in your WordPress configuration file named wp-config.php.

If any of the information is found to be incorrect, WordPress fails to connect to your database server, and you will see the ‘Error establishing a database connection’ alert.

This is the most common WordPress error you may counter. Besides these incorrect details, this error may also occur if the database files are corrupt or the database server is down.

Now, let us explain how to fix the error establishing database connection problem in WordPress.

Check Database Credentials In Your WordPress 

The most common cause for database connection errors in WordPress is incorrect database details. If you have newly shifted your WordPress site to a new host, then this may be the most likely reason.

In WordPress, your database credentials/details are stored in the wp-config.php file. It contains important WordPress settings together with database details.

Now, edit the wp-config.php file and look for the following lines in your 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' );

You have to check that the database name, username, password, and database host details are right and accurate.

You can also confirm these details from your WordPress hosting account dashboard. For that, log in to your hosting account and click the MySQL databases under the database menu.

Now you will reach the database management page in your hosting dashboard. You can see your database name and the username here.

Under that, you will notice the database users and links to change the user password.

Once you confirm your database name, username, and password are correct, or if not, you can change that in your wp-config.php file if needed.

After that, visit your website again to see if the database connection error has disappeared or not.

If you still notice the error, then something else is creating the error. If so, move on to the next step to fix this.

Check Your Database Host Details

If you are sure that your database name, username, and password details are correct, then you can check out that you are using the correct database hosting details.

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

To confirm your database host details, you must contact your WordPress hosting company.

Repair WordPress Database

If you are noticing a different error on the wp-admin, such as “Database tables are unavailable. The database needs to be repaired”, In that case, you have to repair your database.

To do this, You have to add the following line in your wp-config.php file. Add it just before the line says, ‘That’s all, stop editing! Happy blogging’ line wp-config.php.

 define('WP_ALLOW_REPAIR', true);

After that, you can notice the settings by visiting the page: http://www.yoursite.com/wp-admin/maint/repair.php

Note: To access the database repair page, the user does not have to be logged in. After repairing and optimizing your database, you can remove this code from your wp-config.php.

Check whether Your Database Server is Down.

In case everything appears to be correct, but WordPress still cannot connect to the database, your database server or (MySQL server) may be down.

This may occur due to heavy traffic on your server. Meaning your host server cannot handle the load (especially when you are using shared hosting).

Due to this, Your site gets very slow, and for some users, it may even display the error. 

In that case, you can live chat or get in touch with your hosting provider and ask them if your MySQL server is responsible.

If you are running other websites on the same server, then you can test those sites to confirm that your SQL server is actually down or not.

In case, you don’t run any other site on the same hosting server, then visit your hosting dashboard and try to access your phpMyAdmin and connect the database.

If you are able to connect, then we have to verify if your database user has sufficient permission. 

For that, create a new file with the title 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);

?>

Note: Please replace the username and password with yours in the above code. 

Then, upload this file to your website and access it via a web browser.

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

Now return to your wp-config file to check everything is correct there (re-scan for typos).

Other Solutions That May Work 

If the ways mentioned above fail to fix the database connection error on your site, then you can try these additional ways. 

1. Update WordPress Site URL

To fix the database connection error, you can try to update the WordPress site URL using phpMyAdmin. 

To do that, access your phpMyAdmin from your hosting account dashboard, and select the WordPress database.

Then, click the SQL option on the top and enter the MySQL query given below:

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

Note: Enter your own site URL and change wp_options to your own table name as you may have renamed the WordPress table prefix.

2. Rebooting Web Server

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

Your web and database servers will be restarted, which may resolve the temporary problems or malfunctions causing the error.

3. Ask for help

If everything fails to resolve the database connection error, then you have to contact your web hosting company. All good and user-friendly WordPress hosting companies will help you fix the problem, give you the right instructions, or even fix it for you.

You can also contact WordPress developers you may know that can help you fix this issue at a low price.

We hope this helped you fix the error establishing a database connection in your WordPress. We have also covered articles on How to fix the critical error in WordPress and a complete guide on what to do when you are locked out of the admin area.

Leave a Comment