How to Fix the Error Establishing a Database Connection in WordPress

Are you encountering ‘Error establishing a database connection on your WordPress site? Don’t panic, it’s just one of the frustrating errors that make your WordPress site unusable. Such error usually happens when WordPress is missing to have a connection to the database. In this article, I will walk you through some of the easiest ways to fix error establishing a database connection in WordPress.

What Causes Error Establishing a Database Connection in WordPress?

The ‘Error Establishing a Database Connection issue is mostly caused by database information in a website WordPress settings which is incorrect, an irresponsive database server, or simply a corrupted database. Simply WordPress uses a database to store and organize your website content and other data, which makes it connect to the database each and every time a visitor tries to access your site.

Information needed by WordPress for connecting to the database:

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

If you provide incorrect information, WordPress will fail to connect to the database server, and you will encounter an ‘Error establishing a database connection error. Also, this error can happen if the database is down or its files are corrupted. Here are some steps to fix error establishing database connection issue in WordPress.

How to Fix the Error Establishing a Database Connection in WordPress

1. Make sure Your WordPress Database Credentials are Correct

As I mentioned earlier, the main reason that causes this error in WordPress is when you provide incorrect database credentials. Simply if you have moved your website between hosts, then make sure to provide the correct database credentials. These credentials are located in the wp-config.php file if you want to access it. You will make use of the following lines in the wp-config.php file.

1
2
3
4
5
6
7
8
9
// ** 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' );

Just don’t forget to check if your database credentials are correct. You can use your Hosting to confirm this information. In your hosting dashboard, go to Database and select MySQL.

How to Fix the Error Establishing a Database Connection in WordPress

You will then be redirected to the database management page, where you can find your database credentials, both name, username, and password.

After confirming if the database credentials are correct, try to access your site again and see if the error is fixed. If you are still encountering this error, I recommend you keep reading for more troubleshooting methods.

2. Configure Your Database Host Information

If you are very sure that your database credentials are correct, then try again to configure them. Try to access your database credentials with your host, it can be localhost or separate servers. If your database host information is not being hosted locally, I recommend you contact your web hosting service provider to confirm your database host information.

3. Repair WordPress Database

You might be encountering errors such as “One or more database tables are unavailable. The database may need to be repaired”. You can fix this error by adding this line in your wp-config.php file.

1
define('WP_ALLOW_REPAIR', true);

After you have done that the right way, you can access the settings by going to this page:

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

How to Fix the Error Establishing a Database Connection in WordPress

When you are done repairing this error, don’t forget to remove this code from wp-config.php.

4. Check if Your Database Server is Down

If you are sure enough that your database host information is correct, and you are still encountering this error, then try to see if your database server a.k.a your MySQL server is down. This can be caused by simply high traffic accessing your website especially if your site is being hosted on a shared web hosting.

To solve this issue as fast as possible, I recommend you to live chat with your hosting provider or you can make use of the following lines of codes in the testconnection.php file. Don’t forget to enter your username and password when uploading this file to your site.

1
2
3
4
5
6
7
8
<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>

If you successfully connect, then this means the error is resolved, if not then try to configure your database credentials in the wp-config file and try running the code again.

Final Thoughts

There are many different ways that you can use to fix the database connection error on your WordPress website. You can even make use of YouTube videos if you are unable to do this yourself. Some solutions that have been confirmed working to fix this error include, updating WordPress site URL, rebooting the web server, and more. Thank you for stopping here, please leave a comment below.

Leave a Comment