How to Fix WordPress Database Connection Error

How to Fix WordPress Database Connection Error - When you access your WordPress website or blog and access fails, then an error message appears saying "Error Establishing a Database Connection" in your browser, it happens because there is an error that occurs in the database which is usually caused by various things.

As a new user of WordPress, this can be a frustrating problem especially when it happens automatically without you making any changes and of course the WordPress users as time goes on will also be sure to encounter this problem, because it is indeed a problem of establishing database connection errors this often happens.

How to Fix WordPress Database Connection Error

To overcome this problem, in this article, I will show you how to fix "Error Establishing a Database Connection on WordPress" in various ways.

Overcoming WordPress Database Connection Errors

Make sure every time you want to make changes to the database to always backup first. This is important because to avoid things that are not wanted later and the database is an important part of your site.

Why do you get this Error?

You get this error because WordPress cannot establish a database connection. Now the reason why WordPress cannot make database connections can vary. It could be that your database login is wrong or has changed. It could be that your database server is not responsive. and it can also be caused by your damaged database. In my experience, most of these errors occur because of several types of server errors but there may be other factors that can cause a lot of errors why this error can occur. And now let's see how to overcome this problem.

Does the problem occur because of WP-Admin?

The first thing you have to do is to make sure that you get the same error on both parts of the site, namely the front-end and back-end part of the site (wp-admin). If the error message is the same on both pages "Error Establishing a Database Connection", then continue to read this article to the next step.

But if what happens is you get a different error in the back-end (wp-admin) for example a message appears like "One or more database tables are unavailable. The database may need to be repaired ", so to solve this problem is to repair the database.

How to Fix WordPress Database Connection Error

How to repair a WordPress database You can do this by adding the following line of code in your wp-config.php file, if you have already saved the wp-config.php file.

define('WP_ALLOW_REPAIR', true);

Then access in your browser url with the following url:

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

After that you will see a page with two buttons as seen in the picture above. And to make improvements please click the "Repair Database" or "Repair and Optimize Database" button, and wait a few moments until WordPress has finished repairing the database.

And remember, users don't need to log in to access this function when you have added the code. This is because the main intention is to repair a damaged database, Users can often not be able to enter when the database is damaged. So after you have finished repairing and optimizing your database, make sure to delete the repair code from your wp-config.php file.

If this repair does not solve the problem, or you have difficulty running the repair. Please try other solutions as below.

Check the Wp-Config File

Wp-config.php is probably the single most important file in your entire WordPress installation. This is where you specify the details for WordPress to connect your database. If you change your root password or database user password, you also need to change this file too. The first thing you should always check is that the contents of the wp-config.php file are in accordance with your database information.

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

Remember for your DB_HOST, it may not always be localhost and this depends on your hosting. You can get this information from the hosting you are using.

Check Your Web Hosting (MYSQL Server)

Often you will see this Error Error Establishing a Database Connection when your site has been invaded with a lot of traffic. Basically, your host server cannot handle the load (especially if you are on a shared hosting type of hosting) and make your site really slow for some users even for all users to feel the same way. So, the best thing you have to do is ask for help to support telephone or LiveChat with your hosting provider and ask them to resolve the problem if your MySQL server is having a problem.

For users who want to test if the MySQL server runs on its own, you can do several things. Test other sites on the same server to see if they have problems. If they also get the same error, then most definitely something is wrong with the MySQL server. If you don't have another site on the same hosting account, just go to your cPanel and try to access phpMyAdmin and connect the database. If you can connect, then we need to verify if your database user has sufficient permissions. Create a new file called testconnection.php and paste the following code into the file:

<?php $link = mysql_connect('localhost', 'root', 'password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($link); ?>

Be sure to change your username and password. If it is successfully connected, then that means that you have sufficient permissions, and there is something else that might cause the error to occur. To make sure again, please go back to your wp-config file and make sure that everything is correct (pay careful attention to check because there might be a typo).

If you have done all of the above solutions and you still cannot connect to the phpMyAdmin database, then that can be caused by your server. That doesn't always mean that your MySQL server is down. This can mean that you do not have sufficient permission.

So if you get denied access and errors in connecting to phpMyAdmin or through the results of testconnection.php, then you should contact the support or support department of your hosting immediately to get help from them to overcome this.

The above are some solutions that you can apply to resolve the problem if your site encounters an error in the database and the message "Error Establishing a Database Connection" appears. Hopefully with tips that I can share this about How to Fix WordPress Database Connection Error, it can be useful for all of you. Good luck.

1 comment

  1. AnonymousJune 01, 2021

    This comment has been removed by the author.

    ReplyDelete

Provide comments relevant to the posted articles and provide critiques and suggestions for the progress of the blog