One fine morning, you wake up, brew some coffee, and sit at your workstation. Shockingly, you encounter a blank white screen page having a simple curt message: “Error Establishing a Database Connection”
No matter how many times you refresh, you’re unable to access the website.
For each minute that your website is down, unfortunately, you’re losing the chance to generate leads, have conversions, and enjoy sales.
The gravity of the situation is serious, but calm your nerves down and don’t panic! Keep reading this guide to better understand and learn some practical approach on how to fix the error establishing a database connection in WordPress.
Why WordPress Database Error Occurs?
First, let’s examine what this error message actually means.
Your database uses a language called MySQL. Your website is reliant on a server-side language called PHP. Whenever your website URL is entered into the browser, WordPress uses PHP to retrieve all the information pertaining to the page from the database and sends it to the visitor’s browser.
The error establishing a database connection you tend to experience is because this MySQL-PHP connection is severed for some reason, which we must investigate.
Any of the following scenarios may cause this error:
- Your login credentials (username and password) may have been changed. Even if you didn’t make the change yourself, this could have happened somehow.
- If you install a poorly coded plugin, your database may corrupt.
- Your website may be experiencing a heavy traffic spike, which is beyond your server’s capacity.
- Your WordPress web hosting may be at fault and the database is not functioning due to some problem at their end.
Before any attempt to fix the error establishing a database connection in WordPress, I advise you to take a backup! You have the following options to make a backup:
- Download all web files from FTP (SFTP). To do this, log into your hosting account (I’m using Cloudways).
- Export MySQL database through the Export function.
- Alternatively, you can use UpdraftPlus to backup your data.
If you are a Cloudways customer, we make things easy and allow you to take the manual as well as automated WordPress backups via the platform. The following image shows how Cloudways allows easy and periodical backups.
Now, let’s narrow down your problem.
Determine Error Establishing a Database Connection
Are you encountering the same error when you try to enter the website URL (front-end), and when you attempt to login to /wp-admin/ (back-end)? If the error is the same, i.e., error establishing a database connection, move onto the next step.
However, if you encounter the following error when trying to login to /wp-admin/, your database has been corrupted:
“One or more database tables are unavailable. The database may need to be repaired”
In your root WordPress file directory, you must locate your wp-config.php file. You must do this to enable WordPress’ built-in repair feature.
Launch Filezilla —–> (WordPress install folder) —–> public_html
Right-click the wp-config.php file and select a code editor, such as Sublime Text Editor to open the file.
After you’ve opened the wp-config.php file, add the following line at the bottom of the file:
Now navigate to:
(Replace “yourwebsite.com” with your actual URL)
You will see the above-mentioned options. We recommend you to choose the “Repair and Optimize Database” option. Although it takes more time, it is a more comprehensive attempt to fix the issue.
This repair page can be accessed without the need to log in, so it creates a security issue. Immediately after the repair and optimization have finished, remove the line of code you added to the wp-config.php file.
Why Speed is Crucial for SEO & UX?
These expert tips will show you how to improve the speed of your WordPress Websites.
Your Ebook is on its Way to Your Inbox.
Did this method fix WordPress error establishing a database connection? If so, congratulations! If not, carry on reading this guide to find another fix regarding the error.
Check Database Login Credentials in wp-config
If the procedure outlined in the preceding paragraphs did not work for you, then the possible cause of your problem is your database settings.
As mentioned earlier, your database login settings may go haywire if you change other details of your user information or database. This happens if the wp-config.php file is not updated properly by the system.
wp-config.php is the most essential file of WordPress. It contains all the database information.
To open this file go to:
Launch Filezilla —–>(WordPress install folder) —–> public_html
Right-click the wp-config.php file and select a code editor such as Sublime to open the file.
Once you’ve opened the wp-config.php file, you will see four essential variables defined in the code, which is required to get connected to the database.
These are database name, database username, database password, and database host that is localhost in most of the cases (there may also be an IP address in place of localhost).
// ** MySQL settings - You can get this info from your web host ** // /** The name of the database for WordPress */ define( 'DB_NAME', 'database_name_here' ); /** MySQL database username */ define( 'DB_USER', 'username_here' ); /** MySQL database password */ define( 'DB_PASSWORD', 'password_here' ); /** MySQL hostname */ define( 'DB_HOST', 'localhost' );
Note down these values. Now, login to your hosting, in my case it’s Cloudways platform, and select the “Applications” tab. Get into your WordPress application, from the “Access Details” tab, click “Launch Database Manager”.
Click the table named wp_options. It should reveal your website name, URL and other settings. This reveals whether you have the correct information in your wp-config.php file or not.
If the ‘siteurl’, and ‘home’ field information is different, then edit wp-config.php file and insert the same values as in the wp_options table.
Reset Username and Password
You must ensure that the username and password in your wp-config.php file match the in phpMyAdmin. In our case, it’s “MySQL Access”.
If the username and password do not match, open your wp-config.php file and edit it to include the values from the “MySQL Access” panel.
That’s it! If you were attempting to do this via cPanel, you’d have to put in a lot more effort.
Database Host Value
The fourth important value in WordPress wp-config.php file is DB_HOST. For most web hosting providers, this value needs to be localhost, which is the value in the file by default.
If you’ve carried out all the aforementioned steps, and you are still facing the error establishing a database connection in WordPress, then check this official list of DB_HOST values for various web hosting providers. Otherwise, you can contact your web hosting provider and ask them to provide you with the DB_HOST value and enter it into the wp-config.php file.
We hope you find this guide helpful and that you were able to solve the WordPress error establishing a database connection issue.
Did you figure out any other way to get rid of this problem? Let us know in the comments section below and mention what worked and what didn’t work for you. It will benefit others who are facing the same problem as you.
Frequently Asked Quetions
- What is the database connection error in WordPress?
Well, for short, this error occurs because WordPress is not able to establish a connection to a database. And the reason for this disability can vary. This could be because:
- Your login and credentials are false or have been changed.
- Your database server is not responding.
- Your database has been corrupted.
Most of the time this error occurs because of some server error, but there may be other factors as well.
Start Growing with Cloudways Today!
We never compromise on performance, security, and support.
Mustaasam is the WordPress Community Manager at Cloudways - A Managed WordPress Hosting Platform, where he actively works and loves sharing his knowledge with the WordPress Community. When he is not working, you can find him playing squash with his friends, or defending in Football, and listening to music. You can email him at firstname.lastname@example.org