Fix Error Establishing A Database Connection In WordPress

Error establishing a database connection” is a fatal error that if appears on screen, may turn the WordPress website down for users, and it immediately needs to be fixed soon. If you are experiencing the error with your WordPress site or blog, this article will guide you through some solutions that can fix the error.

Why Error Establishing a Database Connection Error Occurs?

It’s well known that WordPress just whole based on PHP code, and in order to work properly, it needs a database action. And if the PHP code fails to access MySQL database then it becomes failure to load all site’s resources and page. And here comes the Error establish a database connection appears before users.

WordPress needs a number of information from a connected database, and the information can be Database name, Database username, Database password, and Database server. Means, if the WP script fails to retrieve any of these information, it will start to show errors on browsers.

error establishing a database connection

And if this error happens to occur with a WordPress site, the site will instantly stop to open. Technically, there’s a number of reasons why the problem may happen with a WordPress installation. Some of the reasons include:

Incorrect Database Credentials: Since WordPress uses to connect with a database to work with, and it requires login credentials like username and password. Means, if something goes wrong with these credentials in wp-config.php settings, then the database connectivity error is expected.
Corrupted Database: Database can get corrupted as well sometimes if installed some defective theme/plugin, if the the hosting server is damaged somehow. This can also be a possible reason for failure of database connectivity.
Corrupted WordPress Files: Corrupted WordPress files are found to be the most common reasons for eruption of “Error establishing a database connection” in WordPress. Actually, it’s likely for users to modify some core WP codes or to do that accidentally, which corrupts the files and leads to throw database issue.
Downtime With Host Server: Many a times, a hosting server may also be a culprit due to server down time. However, this wouldn’t be an issue for long time as once the server comes up, the site will start working properly.

Means, if your WordPress site is down due to aforementioned error, then probably the issue might be occurring due to above reasons. And obviously we will figure out problem by looking into each of those aspects. Let’s discuss every aspect one by one to get back your site online.

Also Read:   Fix “add-apt-repository command not found” Error on Linux

How to fix “Error establishing a database connection” In WordPress?

As we discussed what could the possible causes of this database connectivity error in WordPress, we will learn some methods here in order to overcome the issue.

Before you start trying the steps or solutions here, we highly recommend you to contact your web hosting provider first to confirm if there’s no server maintenance downtime or upgrades underway. In such instances, you don’t need to do anything as once the hosting server is up, your site will start opening normally.

However, in case if your hosting provider confirms there’s no issue with server side, then obviously you will have to figure out Error establishing a database connection on your own end. And since we are going to discuss checking out the database and WordPress core files in the steps, we suggest you to take a backup of your site. Read here to create a full backup of WordPress site.

Once you are done with backup process, you can start troubleshooting the error with the solutions below:

Approach 1: Repair WordPress Database

It’s a good thing to start with default WordPress database repair option. We recommend you to follow this step if you notice a different error on site while opening wp-admin page. Means, you may notice that one or more of the essential tables in database is unavailable. In such instance, you need to repair the database.

To do so, log in to your hosting cPanel, and navigate to directory where core WordPress files are stored. Now, open wp-config.php file in edit mode and add below mentioned line before “That’s all, stop editing! Happy blogging”.

define('WP_ALLOW_REPAIR', true);

Save the file, and navigate the following URL in your browser.

http://www.example.com/wp-admin/maint/repair.php

repair database wordpress

As seen in the image above, you have two options to do, first one is to Repair Database, whereas the second one is to Repair and Optimize Database. Just select the first option to repair your database, alternatively, the second option will take some time to complete as it will repair then optimize the database as well. Once the process is done, check if your site is working properly.

Note: Since the inserted code requires not the users to be logged in to repair connected database, and the page still can be accessed anonymously, it’s a good idea to remove the inserted code from wp-config.php once you are done with its usage.

Approach 2: Check Database Login Credentials and Settings

The most common reason to see Error establishing a database connection in WordPress is the incorrect database credentials in wp-config.php file. If you have actually moved your WordPress site to another host or recently altered some values in configuration file, then it’s likely to see the error while opening site on browser.

Also Read:   Top 10 Forum Plugins For Wordpress

So, just open the file once again and look into the following code snippet to check if every detail is accurate.

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

It’s recommended to check and verify the values for database name, username, password and the MySQL host name. If you find any of these values are incorrect, just fix those values and save the file. Once done, try opening the site on browser.

Approach 3: Testing If Database Credentials are valid

In order to justify if the database username and password are valid, we suggest you to create a new PHP file within your WordPress installation directory with name databasetest.php. Once created, add the following code and save the file.

<?php

$testlink = mysql_connect(‘localhost’, ‘db_username’, ‘db_password’);

if (!$testlink) {

die(‘Connection was not successful: ‘ . mysql_error());

}

echo ‘Connection was successful!’;

mysql_close($testlink);

?>

In the code snippet above, make sure to change the values for entities marked in red according to your database credentials. Once you are all set, save the file and navigate the following URL in browser.

http://www.example.com/databasetest.php

If you see a connection successful message on screen, then your database credentials are correct and valid and you should check the wp-config.php file to fix any incorrect values. However, if the database connection seems failure while, then you would have to create a new database user name, set database access privileges to new user, and put the new values on configuration file.

Approach 4: Reinstall WordPress Core Files

Since it’s mentioned that one of the root cause for getting Error establishing a database connection in WordPress can be corrupted core files in WP installation, replacing the old corrupted files with new one can help. Just download the latest version of WordPress zip file from official site, and upload it to installation directory of your site. Make sure to delete all old files, and extract just downloaded WordPress zip file in the root directory.

Also Read:   Guide To Check UEFI or BIOS In Windows/Linux System

Now, it’s time to use your created backup which we recommended earlier. From the backup file, copy wp-content directory and wp-config.php file and paste it into the WordPress site’s root directory. Make sure if the settings and values in wp-config.php file is accurate.

Once done, open your browser and visit the site to check if the error resolves and site opening properly.

Note: Before you browse the site after replacing old core files, we suggest you to clear your browser cache to load the site’s new files properly. Also, if you are using any cache plugin, purge all previous cached items as well.

What if none of these solutions works?

Although, most of the users fixed Error establishing a database connection issue through above mentioned approaches, if you are still having hard time and unable to troubleshoot the error, you should try more other alternative solutions which are discussed here.

Update WordPress Site URL

At first, login to access WordPress database using phpMyAdmin and check wp_options table to find if siteurl value is all correct. If you find the URL mismatch, it might be the culprit of your issue. To update the URL, run the following MySQL query:

UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'
Note: Replace YOUR_SITE_URL value according your own domain.

Reboot Web Server

In case if you are running your site over local server, VPS, or dedicated server, then you might have forgot to power-cycle (reboot) the server since a long, and this might be a reason why the web and database server may working improperly.

To fix the issue, just restart your server to resolve all temporary glitches and the problem will be solved.

Ask Senior Developer For Help

If you end up with situation where all additional solution fails, it’s time not to contact your web hosting provider for technical help, and they will fix the issue for you. Hiring third party WordPress developers can also be helpful.

Conclusion

The Error establishing a database connection error is really a disappointing fatal error that might need to be resolved at the earliest. It turns your site inaccessible to users, even you. But, checking out through some recommended guidelines might help you to fix the problem. Do share this tutorial with your friends. If you have any questions, you can click to Visit Our Discussion Board.

Nishant

Nishant Verma is a senior web developer who love to share his knowledge about Linux, SysAdmin, and more other web handlers. Currently, he loves to write as content contributor for ServoNode.