Error-Establishing-a-Database-Connection-in-WordPress-How-to-fix-it

Error Establishing a Database Connection in WordPress & How to fix it

Most of the time creating a Database Connection in WordPress will work very well. But there will also be some times when you will encounter errors called “Error Establishing a Database Connection”. You will have to fix the error as fast as possible. It can be a very good idea to do whatever you can in order to back up all your data in order to be safe.
Error Establishing a Database Connection in WordPress & How to fix it

Why do you get this error?

Usually when this error appears it means that WordPress can’t create a database connection. There are many reasons behind the scene. The credentials are wrong or they might have been changed. The server might not be responsive. In some cases the error for Database Connection in WordPress might mean that your database can be corrupted, and that is obviously an issue. The server errors tend to be the main culprit when it comes to stuff like this, and that’s exactly why you need to give this a shot as much as possible.

The same problem occur on /wp-admin/ as well?

When you want to solve this problem you want to be certain that you are getting the same error both on the backend and frontend of your website. In case you get a different error on the WP-Admin side, you will have to repair the database as a good solution. You will be able to do that via adding the wp-config.php file this line “define (‘WP_ALLOW_REPAIR’, true);

Now you can go to https://www.domain.com/wp-admin/maint/repair.php to complete the repair. The user doesn’t have to be logged in. The idea here is that you want to repair the database, so you don’t have to login if the database on its own is corrupt. However, after you repaired this, you want to remove the line from your wp-config.php file just to be safe. In case this did not solve everything, then you might want to use some of the fixes below. Those might be able to assist you, so try to consider all of that.

Recheck the WP-Config file

The WP-Config.php file is where you share the details that WordPress will use in order to connect to that database to begin with. In case you change the database password or the root password, you have to make those changes here as well.

Check if all the stuff in your WP-Config.php file is the same, as below:

define(‘DB_NAME’, ‘database-name’);

define(‘DB_USER’, ‘database-username’);

define(‘DB_PASSWORD’, ‘database-password’);

define(‘DB_HOST’, ‘localhost’);

There are times when the DB_Host value will not be the same as your localhost. Some hosts will differ, but more often than not most popular hosts will use the localhost. Some people solved this simply by replacing the localhost with their own IP, something like “define (‘DB_HOST’, ‘127.0.0.1:8889’);”. DO keep in mind that the IP will vary based on your hosting service provider. So yes, if this happens you will eventually see that it was started from the server end and you can solve it in one way or the other.

Check the MySQL server

The problem can also appear when you are having a lot of server load and the server just can’t handle that. The site will be very slow and some people will even get errors. That’s why you need to talk with the hosting provider to see if the MySQL Server is responsive or not. You can try to test the MySQL Server on your own if you want. Testing other sites on the same server will help you figure out this issue. Usually if the other sites have that same issue, then there are server problems. You can also use the cPanel and use phpMyAdmin to connect the database. If you can connect, then what you need to do is to check whether the database has enough permission or not.

You can create a testconnection.php file and then you can add this:

<?php

$link = mysqli_connect(‘localhost’, ‘username’, ‘password’);

if (!$link) {

die(‘Could not connect: ‘ . mysqli_error());

}

echo ‘Connected successfully’;

mysqli_close($link);

?>

Changing the username and password is important. Now the user has enough permission, if there are other issues then there’s something else wrong. In case this phpMyAdmin approach does not work, you might end up with server issues and you have to try and solve those.

If you encounter errors as you try to establish database connection, these ideas will help a lot. We recommend you to test them out as you actively figure out which are the best options. It’s always important to check these out and give them a shot, they might be able to solve the problems you are encountering!

Do you want us to solve your Error Establishing a Database Connection? Do you want us to help you boost your overall WordPress website performance? We can deliver solution for every WordPress and WooCommerce issues whether it’s WordPress speed optimization, security improvement, error fixing etc. We are available 24/7, You can open Live Chat session or drop us an email at [email protected], Check our Monthly WordPress maintenance plans or Once off fix. Also we provides WordPess Speed optimization service don’t hesitate to ask if you will any questions related to our services.

WordPress Error, WordPress Tips

3 Comments. Leave new

You must be logged in to post a comment.