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.
- Is the same error occurring on wp-admin?
- Check if you are getting the same error on both, the front-end and the back-end of the site (wp-admin).
- If you are seeing the same wordpress error establishing a database connection on both the pages, move on to the next step.
- 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.
- 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.
- 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.
- 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:
- define(‘DB_NAME’, ‘database-name’);
- define(‘DB_USER’, ‘database-username’);
- define(‘DB_PASSWORD’, ‘database-password’);
- 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.
- 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.
- 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:
- <?php
- $link = mysqli_connect(‘localhost’, ‘username’, ‘password’);
- if (!$link) {
- die(‘Could not connect: ‘ . mysqli_error());
}
- echo ‘Connected successfully’;
- mysqli_close($link);
- ?>
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.