error establishing a database connection
Are you seeing the ‘Error establishing a database connection’ notice on your WordPress website? It is a fatal error that makes your WordPress website inaccessible to the users.
This error occurs when WordPress is unable to make a connection to the database. A number of things can affect your WordPress database connection which makes it a bit difficult for beginners to troubleshoot.
In this article, we will show you how to easily fix the error establishing a database connection in WordPress.
What Causes Error Establishing a Database Connection in WordPress?
The ‘Error establishing a database connection’ issue can be caused by incorrect database information in your WordPress settings, corrupt database, or an irresponsive database server.
A database is a software which makes it easy to store, organize, and retrieve data into other software.
As a content management system, WordPress uses a database to store all your content and other website data. It then connects to the database each time someone visits your website.
WordPress needs the following information for connecting to the database:
- Database name
- Database username
- Database password
- Database server
This information is stored in your WordPress configuration file called wp-config.php.
If any of these items are incorrect, WordPress would fail to connect to your database server, and you’ll see the ‘Error establishing a database connection’ error.
It is one of the most common WordPress errors. Apart from incorrect credentials, this error can also appear if the database server is down, or the database files are corrupt.
Let’s take a look at how to fix error establishing database connection issue in WordPress with step by step troubleshooting.
1. Check Your WordPress Database Credentials
The most common reason for database connection error in WordPress is incorrect database credentials. If you have recently moved your WordPress site to a new host, then this could be the most likely reason.
Your WordPress database credentials are stored in the wp-config.php file. It is the WordPress configuration file that contains important WordPress settings including database information.
If you have not edited wp-config.php file before, then take a look at our guide on how to edit wp-config.php file in WordPress.
You need to make sure that the information for the database name, username, password, and database host is correct.
2. Check Your Database Host Information
If you are confident that your database name, username, and password information is 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.
3. Repair WordPress Database
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
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.
4. Check if Your Database 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.