How to Fix Error Establishing a Database Connection in WordPress
If you encounter an Error establishing a database connection notification in the window of your browser, your website is not able to connect to the WordPress database. The inability to establish a database connection is a serious error that prevents visitors from accessing your website. In this article, we’ll show you how to fix the error in establishing a database connection in WordPress using several different methods:
What Is Error Establishing a Database Connection?
All the relevant information about your WordPress site, such as information about posts, pages, plugin settings, user info, are kept in the database. WordPress connects with this database every time someone visits your website.
The database in question does not store media files (images and video) and plugins/themes core files such as index.php, wp-login.php, etc.
When somebody visits your webpage, PHP executes the relevant code on the page and retrieves information from the database, displaying them to the site visitor in the browser.
To successfully establish the connection, you need the following information:
This information is found in the WordPress configuration file wp-config.php. In case one of these parameters is not correct, WordPress won’t be able to connect with the database.
In that case, the browser window will display the Error establishing a database connection notification.
Due to this error, you cannot access the front end of your site and WordPress admin panel.
Before you start troubleshooting, create a complete backup of your WordPress site, since some of these steps will require editing the information about the database. It is recommended to regularly backup your site, especially if it is frequently under maintenance.
One of the most common reasons for the error is incorrect access information, so this is where your troubleshooting needs to start. This issue is known to arise as you’re moving the WordPress site from one host to another.
The information you need to access the database is located in WordPress configuration file wp-config.php. You’ll find it in the root folder of your WordPress website. This file contains four lines of code (with the database name, user name, password, and host of the database) that have to be accurate to successfully establish a database connection.
You can easily find this information in your cPanel. Assuming your host uses cPanel, they should provide you with a direct link to access it somewhere in your welcome emails.
But, if you can’t find that link, here’s a tip to help you access your site’s cPanel dashboard at any time:
If your site’s URL starts with http, append :2082 to the end of your domain name.
If your site’s URL starts with https, append :2083 to the end of your domain name.
So, you should be able to access cPanel by going to http://www.yoursite.com:2082 or https://www.yoursite.com:2083.
Once you’ve found your cPanel address, log into your hosting account and click on the MySQL databases option in the Databases section.
When you access the MySQL databases section, you’ll see a single database belonging to your site, or a list of databases if your server is hosting multiple sites. Click on your site’s database to open it.
Then, if you want to find information about or make changes to a user account, find the column called Privileged Users and select the appropriate user name. When you click on Manage User Privileges you will be able to edit the user’s database privileges and activate/deactivate them as appropriate.
After you check the access information, you also have the option to change the password. Simply find your site’s database in the MySQL list and click on the option to change the password.
This is a good solution if you’re not sure which password you chose or you want to reset it. You can also create a new user for your database.
When you create a new user, simply add it to your database using the following option.
The name of the database and its compatibility with your website can be verified using the phpMyAdmin option from cPanel.
When you click on this option, you’ll see the name of your database below the information_schema database used by your host. Click on your database name to see all the tables and data it stores.
To verify that the database is compatible with your website, click on the wp_options table. In the field siteurl check the entered value for the URL address. If everything is working fine, then your database is compatible with the website.
After you verify the information about the database name, username, and password, you can change this information by modifying your wp-config.php file if there’s a need (if you created a new user, it is mandatory).
After that, refresh your site to check if the error is gone.
If the name of the database, username, and password are correct and the error is still there, you need to verify if the information about the database host is input correctly.
A lot of WordPress hosting companies use localhost as the host for the database. However, some hosting providers use separate servers for host databases. In that case, contact your WordPress hosting provider to check information about the host of your database.
You can also consult a list of POSSIBLE DB_HOST VALUES provided by WordPress. It contains parameters used by some of the popular WordPress hosting providers so you might find the information you’re looking for there.
In some cases, a damaged database may be the cause of connection error. This is a rare occurrence. It usually happens when you add a large number of tables to the database in the process of adding and removing plugins and themes.
After you log into your admin panel, you’ll see error notifications indicating that your database is damaged, such as One or more database tables are unavailable. or The database may need to be repaired.
If this happens, it’s important to know that WordPress has a mode that you can initiate to eliminate this error.
Access the wp-config.php file and insert the following code before the line ‘That’s all, stop editing! Happy blogging’
To see the settings for fixing the database, add the following link to your browser.
Replace yourwebsite.com with your website’s URL.
You don’t have to be logged into the admin panel to access this page. When it opens, it’s enough to click on the button for fixing the database. This will start the process that may take a while. When you fix the database, remove the code you previously added to the wp-config.php file.
If your information for database access is correct, and you still cannot establish a database connection, you should inspect the database server (MySQL server).
Large traffic on the server is one of the possible causes of a database server crash. Many hosting providers have limitations on their servers regarding traffic. This means it’s more likely that your server won’t be able to deal with large traffic, especially if you have shared hosting. Also, if there is an overload on your database server, your website’s speed decreases and some visitors may even get an error message when they try to access it.
One way you can solve this is by using cache plugins. They can help decrease database interactions on your website. A cache plugin creates a lighter HTML version of your page that it serves up to visitors. That way the server doesn’t have to execute PHP code and MySQL database queries with each new page visit.
Alternatively, contact your hosting provider and ask them to examine whether your MySQL server is functioning properly.
If you have multiple sites on the server, check all of them to see if the MySQL server is working at a decreased speed. And after the hosting provider verifies your database server is functioning properly, run a quick test with your live site to see if the error is still there.
There is no way to completely prevent the error establishing a database connection in WordPress. However, there are ways to minimize the risk—ensure your database access credentials are valid, purchase high-quality hosting, and do regular website backups.
Choose hosting providers whose platforms are optimized for WordPress. This way, you can always count on their support. At the same time, you’ll avoid small glitches and errors caused by incompatibilities with WordPress.
Regular site backups should be a part of your site management daily routine. You can do this using backup plugins that allow you to automate the process, or you can backup your WordPress website manually. This way, if your site crashes, you’ll always have a ready-to-go copy that allows you to revert to your last working settings before you start troubleshooting.