It’s confusing to go to your website expecting to see your beautiful design, only to be presented with a blank screen and a message saying “error connecting to database”. This error occurs when WordPress cannot connect to that database. Several things can cause this connection error. In this article, we will explore what causes the database connection establishment error message and see how to fix it.
What is a WordPress database?
A database is a table that stores content such as your posts, pages, widgets, colors, titles, etc. This data can be accessed, organized and retrieved by WordPress for display to your visitors.
This works very well. Until it doesn’t.
If the database is not accessible, you will see a message saying “error establishing database connection” and a blank screen. It is empty because it does not contain any data to display anything else.
What causes the database connection error?
First of all, don’t panic. This is a common error, and it does not mean that your content is lost. It can be fixed.
There are several factors that can cause this error. The most common ones are:
- Incorrect information in your WordPress settings.
- A corrupted database.
- Database server not responding.
- Corrupted WordPress files.
- Too much traffic.
Let’s see the steps to troubleshoot and repair the error establishing a database connection error. Once you have found the problem, there is no need to follow the rest of the steps.
1. Check your database credentials
This is the most common cause of the database connection establishment error. Incorrect database credentials do not change on their own, but this can be the problem if someone has made changes to the files, hosting, etc.
WordPress stores data in a database management system called MySQL. This database is stored on a server. To access the database on this server, WordPress needs some information:
- Database name
- User name
- Password
- Database server
This is the credentials of the database. All this information is stored in the wp-config.php file. If any of this information in the file is incorrect, WordPress cannot establish a connection and you get the connection error.
Check the wp-config file
To verify the credentials of your WordPress database, you need to have access to your server files. In your host’s cPanel, select the file manager, then select your website’s folder.
Your files may be different from mine. I have a main site and many test sites under that site. For this reason, I have a public_html folder that contains the files for my main site and individual folders for all other sites on the server. Open the folder for the site you want to check.
Look for the file named wp-config.php and right click to open the options. Download a backup before making any changes, then select Edit.
Scroll down to find a section called MySQL settings. Look for the database name, user name, database password and host name information. Next, you will need to verify that this information is correct.
Confirming the database credentials
In your host’s cPanel, select MySQL Databases.
Then look at your current databases to confirm your database name and username.
Next, scroll down to the current users section. Here you can see the usernames and change the password.
Make sure they match
If the database name, username and password do not match, make the necessary changes and try reloading the website.
2. Check your WordPress dashboard
Try opening your WordPress dashboard by adding /wp-admin to the end of your URL like this:
examplewebsite.com/wp-admin
If you see the error message “making a database connection”, go to the next step. If you see a different message, follow the information it provides. This may lead you to the solution.
If you can get into the dashboard, then try disabling plugins, themes or code until the problem is resolved.
3. Fix your database
Try logging into your website as an administrator and see a message that says something like “One or more database tables are unavailable. The database may need to be repaired.
In this case, you can repair the database by adding a line of code to your wp_config.php file.
Open the file and scroll down to the section that says “That’s it, stop editing! Happy blogging.” Just above that line, add this code:
define('WP_ALLOW_REPAIR', true);
Then, go to the repair page of your website: examplewebsite.com/wp-admin/maint/repair.php
This gives you two options:
- Repair the database – WordPress will look for common problems and fix them.
- Repair and optimize the database – WordPress will repair and look for things it can optimize to improve performance.
Both options may take some time. Anyone can access this page. Once you have completed the repair, remove the code from your configuration file.
4. Check if your server is down
We don’t expect our servers to go down, but it is difficult for a server to have 100% uptime. This is more likely to happen if you are on a single server. If you have a cloud-based plan, your site will simply rely on another server if one goes down.
If you are on shared hosting, your server may go down due to heavy traffic. In this case, your site may give this error if the server is slow enough.
First of all, if you have multiple websites on the server, make sure they are working.
Second, if you have a VPS, dedicated or local server, go to your hosting account and look for a notification about the current status of your server. If your server is not working, you may need to restart it.
5. Restart your server
Even if your server is running, restarting it can be helpful. In your hosting account, look for an icon regarding restarting your server and click on it. This option is only available for VPS, local and dedicated servers. Shared hosting does not have this option.
6. Update WordPress or remove corrupted plugins and themes
WordPress files may be corrupted due to an outdated plugin or theme, or even the WordPress core. It may have failed during an update or when connecting the website to an external service.
To solve this problem, you can delete the files via FTP or by accessing the files in the host’s admin panel. The file structure is the same.
To access the files in cPanel, go to the Files section and select File Manager.
If your website is the main site on your server, find the public_html folder and click on it. If it is another site on your server, find the folder with that site’s name and click on it.
Find a folder called Content and click on it.
Then look for your themes and plugins folders. Rename the folder you suspect is the culprit. If you’re not sure, think about what has been updated or was supposed to be updated. Consider the quality of your themes and plugins. I find that plugins are usually the root of the problem if something corrupts my files.
If this solves the problem, rename the folder to its original name and enter the folder. Then rename one of the plugins or themes and test the site until the problem goes away. If it disappears, delete the plugin or theme that caused the database connection error.
If you have renamed the plugins and themes folder and the problem persists, the plugins and themes are not the cause.
7. Update your site URL
Another possible problem could be the site URL for the database. This is less likely to be the problem, but if nothing else works, it’s worth a try. In the host’s cPanel, go to Databases and select phpMyAdmin.
This will open a query editor allowing you to run SQL on your database. First, choose your database from the list. Then select SQL. Then, add this line of SQL with your URL:
UPDATE wp_options SET option_value="examplesite.com" WHERE option_name="siteurl"
Finally, click Go. This will update your URL for the database. Once complete, re-upload your website to see if the problem is resolved.
8. Replacing Corrupted WordPress Files
If your WordPress files are corrupted, download a new copy of WordPress and unzip it.
Delete the wp-content folder and the file called wp-config-sample.php.
Finally, upload the files to the WordPress root folder. This will overwrite all the files except the two you deleted in the previous step. Now try your site to see if it works.
9. Contact your web host
If nothing else works, it’s time to contact your web host for help. They will be able to tell you what to do. Many web hosts will help you resolve the problem. It is also possible that they can solve it for you.
Final thoughts on the database connection error
That’s how we covered troubleshooting and fixing the database connection establishment error message on your WordPress website. These steps are not difficult, but sometimes it takes time to locate the problem.
Always use quality themes, plugins and hosting. Also, make sure you have a backup in case there are no other solutions. Before deleting important files, contact your hosting company for help.
We want to hear from you. Did you get the database connection setup error? What did you do to solve it? Let us know in the comments