How To Fix "Error Establishing A Database Connection"

How To Fix “Error Establishing A Database Connection”

One of the most common errors with WordPress is the “Error establishing a database connection”. There are so many reasons why it happens and solutions as well.

The most common solution is if you’ve entered your database credentials wrong or they have changed. So open up an FTP client, edit your wp-config.php file, and ensure your database credentials are correct. If they are, then you can try some of the other solutions.

You can find your WordPress database credentials which look something like this:

define('DB_NAME', 'database name');
define('DB_USER', 'username');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'host');

Reason For Error Establishing A Database Connection

Usually, this error shows when your WordPress website can’t connect to your database. And it looks like this:

Error Establishing A Database Connection

Your database is where all the content lays, all of your data and information. WordPress is primarily split into 2 parts. Your database, which contains the data, and your web server, which contains your WordPress website, images etc.

One doesn’t work without the other, so that’s why you will see your WordPress website being useless without one of these elements.

To connect your WordPress website to a database, you need a host, username, password and a database name. All these are added to your wp-config.php file, as shown above, and then you have a connection.

But when an accident happens and your WordPress website loses the connection to the database, there are multiple things you can do to solve it. Let’s get into them.

Fix Error Establishing A Database Connection

Your WordPress is now not connected to your database, and this doesn’t mean all your data is lost. This is rarely the case. It might be something simple, from a spelling mistake in your database name to a more severe issue where your database has broken down.

I have a solution for each of these cases, so let’s get into it.

[cboxarea id=”cbox-DZ7YzH84qT48YAf1″]

1. Confirm Your Database Credentials

This is the most common solution that will solve your issue 9/10 times. Either you have entered your database credentials wrong, or they have been changed without you knowing.

Maybe your host is wrong, username or even password.

The easiest to do is to sign into your host and copy and paste the credentials into your wp-config.php file. You’ll need a file manager through CPanel or an FTP client like FileZilla to connect to your web server to access wp-config.php.

In wp-config.php, search for DB_NAME for the database name, DB_USER for the username, DB_PASSWORD for the password and DB_HOST for your database host. Your host can provide all this, and often they can even fix it for you.

Now if this doesn’t solve the issue, it might be because your MySQL database is IP restricted. If this is the case, you’ll have to open it to your server IP.

To get your server IP, you can open your terminal on your computer and write: ping domain.com

In return, you’ll get the IP of the server, which you’ll then have to whitelist for your MySQL database.

ping domain.com

2. Repair Your WordPress Database

If step one doesn’t work, then you can try and repair your WordPress database. Now this step only works if your WordPress website can connect to the database, but there is something wrong.

You need to, just like in step 1, edit your wp-config.php file, and then you need to add the following line anywhere at the top of the file:

define('WP_ALLOW_REPAIR', true);

After this, go to your website, which should look like the image below, where you can click on repair database.

You should go to your website with the following URL: https://yourdomain.com/wp-admin/maint/repair.php

wordpress repair database

You can also use this tool if you don’t have any issues with your database but just want to optimise it for performance.

3. Your Database Server Might Be Down

This one is tricky, but your database server might be down.

Just like your web server is a server, it provides files for your WordPress website, and then your database is also running on a server.

The best way is to reach out to your host and ask them to look into why you can’t connect to the database server.

If you’re hosting the server yourself, then the first thing you should try is a restart. When restarting your database server, you’ll get the error log of why it can’t start again, and then you can go from there.

4. Reboot your MySQL, PHP or Web server

In continuation with the last point, not only can you restart your MySQL, but you can also restart your web server and your PHP in general.

If you’re using something like Kisnta, for instance, they have the option to restart your PHP setup, and your host might have the same too.

restart php

The same goes for your web server; you might be having issues connecting to your database because something is cached on your server or something is malicious.

A simple restart will sometimes do wonders.

5. Ask Your Host For Help

Another option is simply to ask your host for help.

Almost any host, no matter where in the world, has an employee who knows a thing or two about WordPress.

Creating a support ticket, describing the issue and letting your host look into it is sometimes the best solution.

You save time, and they’re experts. They know just what to do to solve the issue.

6. Check For Corrupt WordPress Files

You might have experienced a hacking attempt, and to solve this, I would always suggest you use Wordfence. But you can’t, in this situation, as you can’t access your WordPress admin because you don’t have access to your database.

The next best thing is to reinstall WordPress on your server simply.

  1. Head over to WordPress and download the newest version of WordPress.
  2. Connect to your web server by using an FTP client like FileZilla.
  3. On your server, delete all files and folders except for wp-content
  4. Upload the newest WordPress installation you just uploaded.

As soon as you’re in your WordPress admin area again, I recommend downloading Wordfence and running a scan to see if there are any other files you need to delete.

7. Use A WordPress Backup

The last option is to use a WordPress backup.

Hopefully, you’ve automatically made backups of your website, and it’s plug-and-play for you to roll back.

If you haven’t, I’ve made a guide on how to set up automatic backups for your WordPress website seamlessly.

It might be an idea to ask your host as well. They often take a daily backup of the entire server, and they might be able to recover your website for you before you experience the error.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *