How to fix the error establishing a database connection in WordPress?

How to fix the error establishing a database connection in WordPress?

You can check if there are any WordPress database repair plugin to fix this error. If not, scan through the following list of solutions on how to repair a database in wordpress. It is advisable that you take sufficient backups before making any database changes.

  1. Is the same error occurring on wp-admin?
  1. Check if you are getting the same error on both, the front-end and the back-end of the site (wp-admin).
  2. If you are seeing the same wordpress error establishing a database connection on both the pages, move on to the next step.
  3. Sometimes, you may get different errors on both the pages. On the wp-admin page, you may get an error message reading “One or more database tables are unavailable. The database may need to be repaired”, you will have to repair a database in wordpress.
  4. The question is how to repair a database in wordpress. You can repair it by adding ‘define(‘WP_ALLOW_REPAIR’, true);’ line just before ‘That’s all, stop editing! Happy blogging’ line in your wp-config.php file.
  5. Aftr adding the ‘define(‘WP_ALLOW_REPAIR’, true);’ line, see the the settings by visiting http://www.yoursite.com/wp-admin/maint/repair.php.

Note:

You don’t have to be logged in to access this functionality.             After fixing the error establishing a database connection issue in wordpress and optimizing your database, remove this from your wp-config.php.

This was the first solution to fix the error, if the error establishing a database connection is not solved in wordpress, try the next solution.

How to fix the error establishing a database connection in WordPress?

  1. Check the WP-Config file:

WP-Config.php is the most important file in WordPress installation where you specify the details for WordPress to connect your database. Any change that you make to your root password or the database user password should be changed in the WP-Config file also. Make sure to match your wp-config.php file with the following:

  1. define(‘DB_NAME’, ‘database-name’);
  2. define(‘DB_USER’, ‘database-username’);
  3. define(‘DB_PASSWORD’, ‘database-password’);
  4. define(‘DB_HOST’, ‘localhost’);

Everything should be the same, check for typos too.

Sometimes, your DB_Host value might be other than the localhost, like HostGator, BlueHost, and SiteGround.

Sometimes, you can this error by replacing the localhost with the IP.

  1. define(‘DB_HOST’, ‘127.0.0.1:8889’);

Note: If you have availed online web hosting services, IP’s may vary.

If everything in this file is correct then the problem might be on the server end.

 

  1. Check your Web Host (MySQL Server):

The error establishing database connection in WordPress may also occur when your site gets a lot of traffic. When this happens, your host server cannot handle the load and your site will get really slow or you mught get to see this error. If this seems to be the root cause, get in touh with your hosting provider and request them to check if your MySQL server is responsive.

If you want to test if MySQL server is running yourself, you can:

  • Test other sites on the same server. If the other sites are also having the same issue and displaying the error, the problem may be with your MySQL server.
  • If you have non other sites hosting on the same server, go to your cPanel. Then, connect the database by accessing phpMyAdmin. If you are able to connect, then you will have to check if your database user has sufficient permission. Test by creating a new file called testconnection.php and paste the following code in it:
  1. <?php
  2. $link = mysqli_connect(‘localhost’, ‘username’, ‘password’);
  3. if (!$link) {
  4. die(‘Could not connect: ‘ . mysqli_error());

}

  1. echo ‘Connected successfully’;
  2. mysqli_close($link);
  3. ?>

Replace the username and password. If codes works and you are able to connect successfully, your user has sufficient permission. Therefore, the problem must be with something. Check your wp-config file and make sure that everything is correct, give special attention to typos.

If this code doesn’t help something with your server. Either MySQL server is down or your user does not have sufficient permission.

Remember, these solutions may not work for you. Use these solutions at your own risk. Don’t forget to take sufficient backups.