The “Error Establishing a Database Connection” is one of the most frustrating errors for any WordPress user. This error happens when your website doesn’t have access to the database. Resolving this error is necessary because it can affect your sales and analytics. Once you get a hang of it, this issue can be easily detected and fixed.
Why Does This Error Occur?
When WordPress is unable to establish a connection due to varied reasons, you will get this error. The reason can be anything from incorrect database login credentials to an unresponsive database server to a corrupted database. Mostly, any kind of server error causes this error. For instance, if you are developing a WordPress website in Brampton, you might encounter this error while setting up your WordPress Website Development Brampton project.
Troubleshooting the Error
WP-Admin Issue
Check the Front-End and Back-End Errors: Ensure that both the front-end and back-end of your site are getting the same error. If you are getting different errors, such as “database tables aren’t available,” then repair your database.
- Add the following line in your
wp-config.php
file:
define('WP_ALLOW_REPAIR', true);
To see the repair page, visit:
http://www.yoursite.com/wp-admin/maint/repair.php
- The user doesn’t have to be logged in to access this page because to repair a corrupted database, users can’t login. Once you have repaired the database, remove this line from your
wp-config.php
.
Check the WP-Config File
Verify Database Credentials: The most important file in a WordPress installation is the wp-config.php
. Here, you will specify all the details for WordPress to connect to your database. Once you change the database user password, you’ll need to change the file too.
To check everything in the wp-config.php
file is correct:
define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');
DB_HOST
isn’t always the localhost; this will be different depending on the host.- To fix your problem, replace the localhost with the IP if you are running WordPress on a local server.
Check the Web Host
- Server Load: This error occurs when the traffic on your website increases. It just means that the host server is unable to handle the load, making the site slow.
- For this, you have to ask your hosting provider whether your MySQL is responsive.
- To test MySQL server:
- Test other sites on the server.
- If they have the same error, then your MySQL server has an issue.
- If your sites aren’t on this hosting account, access
phpMyAdmin
through cPanel and connect to your database. Once connected, you will create a new filetesting_connection.php
and paste this code:
<?php
$link = mysqli_connect('localhost', 'username', 'password', 'database-name');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>
- Make sure you replace the username and password. You’ll get to know your user has sufficient permission when the script gets connected.
- Double-check for any typos in your
wp-config
file. If you can’t connect to the database, then the issue is with your server and can be either due to MySQL server is down or your users don’t have permission.
Additional Solutions That Have Worked for Users
Update the WordPress Site URL: You can try updating the WordPress site URL using phpMyAdmin
in cPanel.
- Simply access
phpMyAdmin
from your hosting account dashboard and select your WordPress database from the list. - After that, switch to the SQL menu on the top and enter the following MySQL query:
UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl';
It should look like this:
UPDATE wp_options SET option_value='http://www.yoursite.com' WHERE option_name='siteurl';
- Don’t forget to provide your own site URL and change
wp_options
to your own table name, as you may have changed the WordPress table prefix.
2. Rebooting Web Server: Users on dedicated servers, local servers, and virtual private servers (VPS) can try rebooting their servers.
- This will restart your web and database server, which may fix some temporary glitches causing the error.
Ask for Help: If everything else fails, then you may need to contact your web hosting company. All good WordPress hosting companies will help you troubleshoot the problem, point you in the right direction, or even fix it for you.
- You can also hire WordPress developers using a platform like WPBeginner Pro Services to help you fix this issue for reasonable rates. We have a dedicated team of expert developers that you can trust to resolve any of your WordPress issues.
Conclusion
The “Error Establishing a Database Connection” is a common issue in WordPress that can be frustrating to resolve. However, by following these steps, you can troubleshoot and fix the error. Remember to check your database credentials, server load, and server configuration to resolve the issue. If you are still unable to resolve the error, consider seeking help from your hosting provider or a WordPress developer.