How to Fix the Error Establishing a Database Connection in WordPress

How to Fix the Error Establishing a Database Connection in WordPress

1. Check Your WordPress Database Credentials
The most frequent cause of the database connection error is incorrect credentials, especially if you’ve recently migrated your WordPress site to a new host.

Your WordPress database credentials are stored in the wp-config.php file, which contains essential configuration settings, including database details.

If you’re new to editing the wp-config.php file, check out our beginner’s guide on how to edit it for detailed instructions.

Focus on these specific lines in the wp-config.php file:

Ensure that the database name, username, password, and host details in your wp-config.php file are correct.

You can verify these details through your WordPress hosting account dashboard. For this guide, we’ll use Bluehost as an example, but the steps will be similar with other hosting providers.

Log in to your hosting account, click on the ‘Website’ tab in the left column, then switch to the ‘Advanced’ tab. Scroll down to the ‘Database’ section and click the ‘Manage’ button next to it. This will open the MySQL Databases page in cPanel.

On this page, you can find your database name and username under the ‘Current Database’ section. Copy these details into a notepad for reference and add them to your wp-config.php file.

Next, scroll down to the ‘Current Users’ section and click the ‘Change Password’ link next to your database name. Here, you can set a new password for your database.

After confirming all your database details, update them in your wp-config.php file as needed.

Visit your website again to see if the database connection error is resolved. If the error persists, there may be another issue that needs addressing.

2. Check Your Database Host Information
If you’re sure your database name, username, and password are correct, the next step is to verify your database host information.

While most WordPress hosting providers use localhost as the database host, some managed WordPress hosts may use a different server for databases. If that’s the case, localhost won’t be the correct host.

To confirm the correct database host, you’ll need to contact your WordPress hosting provider for accurate details.

3. Repair WordPress Database If you’re seeing errors in the wp-admin dashboard such as “One or more database tables are unavailable” or “The database may need to be repaired,” it indicates that your database requires repair. To fix this, you need to add the following line to your wp-config.php file. Place it just before the line that says “That’s all, stop editing! Happy blogging”:
After adding the repair line to your wp-config.php file, visit this URL to access the repair settings: http://www.yoursite.com/wp-admin/maint/repair.php, replacing “yoursite.com” with your actual domain. Click the ‘Repair Database’ button to begin the repair process. Note that you do not need to be logged in to access this page. After completing the repair and optimization, remember to remove the repair line from your wp-config.php file. Alternatively, you can repair your database through your hosting account’s cPanel without adding any code. Log in to your hosting account, navigate to the ‘Websites’ page, switch to the ‘Advanced’ tab, and click ‘Manage’ in the Database section. This action will open cPanel in a new tab. In cPanel, scroll down to the ‘Modify Databases’ section, select your database from the ‘Repair Database’ dropdown menu, and click ‘Repair Database’ to initiate the repair process. Your hosting panel will handle the repair, and you should receive a success message once it’s completed.
4. Check If Your Database Server Is Down If everything seems correct but WordPress still cannot connect to the database, the issue might be with your database server (MySQL server). This problem can occur due to heavy traffic or server load, especially on shared hosting environments. When the server is overwhelmed, it can cause your site to slow down or generate connection errors. To resolve this, contact your hosting provider via phone or live chat and inquire about the status of your MySQL server. If you have other websites on the same server, check if those sites are also experiencing issues to confirm if the SQL server is down. If you only have one site on the hosting account, log into your hosting dashboard, switch to the ‘Advanced’ tab, and click ‘Manage’ next to the phpMyAdmin section. This will open phpMyAdmin in a new window. In phpMyAdmin, select the ‘Database’ option at the top and click on your database’s name to access its settings. If you can access it, verify that your database user has the necessary permissions. To further diagnose, create a new file named testconnection.php and insert the following code:
After pasting the code, replace the placeholder username and password with your own credentials. Upload this file to your website and access it through a web browser. If the script connects successfully, it indicates that your database user has the necessary permissions, suggesting that the issue lies elsewhere. Return to your wp-config.php file and carefully review it to ensure all details are correct and free from any typos.

Do you need an expert? HIRE US NOW!