One of the fatal errors that prevent websites from working normally or make people can’t get access to your website is the ‘Error establishing a database connection’ notice.
This error shows that WordPress can’t be able to make a connection to the database due to the number of things affecting your WordPress database connection. It may appear a little bit difficult for beginners to troubleshoot.
We recommend you to read this article as we shall show you a step-by-step guide on how to easily fix the error in establishing a database connection in WordPress.
The ‘Error establishing a database connection’ issue is mostly caused by incorrect database information in your WordPress settings, corrupt database, or an irresponsive database server.
Here’s an example of “error establishing a database connection” on a website.
A WordPress database helps a website store, organize, and retrieve data into other software.
The database of WordPress helps you store all your content and other website data to connect them to the database each time someone visits your website.
For WordPress to be able to connect your website files to the database, it needs your configuration file called wp-config.php such as.:
- Database name
- Database username
- Database password
- Database server
Your website shows an ‘Error establishing a database connection error to your users if WordPress finds any of the configuration file items incorrect because it can’t connect to your database server.
The “Error establishing a database” on a website is one of the most common WordPress errors.
If the database server is down, or the database files are corrupt, this error can also appear.
Hope you have known what the error establishing a database connection is and also what that makes it occur.
Now, let’s take a look at how to fix it by troubleshooting step by step.
Check the credentials of your WordPress database
Once the error establishing a database connection issue has occurred on your website, the first thing you should check is your database credentials as the incorrect ones are the most reason for this issue.
It also happens if you have recently moved your WordPress site to a new host.
The credentials of your website configuration database are stored in the wp-config.php file containing the important settings including database information.
Next, look for the following lines in the wp-config.php file as shown in the screenshot below.
Make sure that the information for your database name, username, password, and database host is correct.
If in doubt, confirm it from your WordPress hosting account dashboard by logging in to your hosting account and clicking on MySQL databases under the database section as shown below.
Clicking on the “Databases tab and “MySQL Databases” buttons after logging in to your Cpanel account will take you to the database management page in your hosting dashboard where you can find out your database name and username.
Below that you will find the database users and links to change the user password.
Once you have confirmed your database name, username, and password, you can change that information in your wp-config.php file if needed.
After that, try visiting your website again to see if the database connection error has gone.
If you can still see the error, then this means that something else is wrong. Continue reading for more troubleshooting steps.
Check the information in the database of your host
If you are confident that your database name, username, and password information are correct, then you may want to make sure that you are using the correct database host information.
Most WordPress hosting companies use localhost as your database host. However, some managed WordPress hosting companies use separate servers to host databases. In that case, your database host information will not be localhost.
You need to contact your WordPress hosting company to confirm your database host information.
Repairing the database of your WordPress
If you are getting a different error on the wp-admin, for instance, something like “One or more database tables are unavailable. The database may need to be repaired”, then you need to repair your database.
You can do this by adding the following line in your wp-config.php file. Make sure to add it just before ‘That’s all, stop editing! Happy blogging’ line wp-config.php.
Once you have done that, you can see the settings by visiting this page: http://www.yoursite.com/wp-admin/maint/repair.php. You may read our expert guide on how to set up a WordPress blog.
Note: the user does not need to be logged in to access the database repair page. Once you are done repairing and optimizing your database, make sure to remove this code from your wp-config.php.
Check if the database of your server is down
If everything seems to be correct, and WordPress still cannot connect to the database, then your database server (MySQL server) may be down.
This could happen due to heavy traffic on a server. Basically, your host server just cannot handle the load especially when you are on shared hosting).
Your site will get really slow and for some users, it may even output the error. So the best thing you should do is get on the phone or live chat with your hosting provider and ask them if your MySQL server is responsive.
If you have other websites running on the same server, then you can check those sites to confirm that your SQL server is down.
If you do not have any other site on the same hosting account, then simply go to your hosting dashboard and try to access phpMyAdmin, and connect the database.
If you can connect, then we need to verify if your database user has sufficient permission. Create a new file, testconnection.php, for example, and paste the following code in it as shown below:
Make sure to replace the username and password. You can now upload this file to your website and access it via web browser.
If the script connected successfully, then it means that your user has sufficient permission, and there is something else that is wrong.
Go back to your wp-config file to make sure that everything there is correct (re-scan for typos).
Frequently Asked Questions
We’ve been helping people fix errors such as 503, database establishing a connection, etc. on their website. Here we’d compiled some frequently asked questions.
If all these tips don't work, what can I do?
If the above-mentioned tips and methods didn’t help you fix an error establishing a database connection on your website, then you may try the additional tips below.
1. Update WordPress Site URL
Update the WordPress site URL using phpMyAdmin. Simply access phpMyAdmin from your hosting account dashboard, and select your WordPress database.
After that click on 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'
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. Reboot your web Server
The rebooting option is for people who use WordPress dedicated servers, local servers, or virtual private servers (VPS).
After rebooting your web server, your website and its database server will restart which may fix some temporary glitches causing the error.
3. Last but not least, seek help
In this option, you would need to seek the help of an expert if everything above fails.
First of all, contact your web hosting company to help you troubleshoot your website, point you in the right direction, or even fix it for you.
Hope this article has helped you know how to fix an error establishing a database connection on your WordPress website or blog. You also need to read an expert guide on how to install WordPress on Cpanel.
Please subscribe to our newsletter for more important updates from us. Also, don’t forget to share this article with your friends.