
HOW TO SOLVE ERROR ESTABLISHING A DATABASE CONNECTION IN WORDPRESS?
Last updated on June 26th, 2025 at 10:57 am
The “Error Establishing a Database Connection” occurs when your WordPress site is unable to access or retrieve data from its database. Common reasons include incorrect settings in the wp-config.php file, invalid database credentials, or corrupted database files.
If not resolved promptly, this error can affect your site’s availability and degrade the user experience. The good news is that it’s usually straightforward to diagnose and resolve.
In this guide, we’ll cover what the “Error Establishing a Database Connection” means, what causes it, and the proven steps to fix it. You’ll also learn simple tips to prevent this issue from recurring.
What Is Error Establishing a Database Connection?
The “Error Establishing a Database Connection” occurs when your WordPress site cannot connect to its database. This can lead to site downtime or functionality issues.
Common causes include incorrect configuration settings, invalid database credentials, database server failures, or corrupted core WordPress files.
When a visitor accesses your WordPress site, PHP commands request data from the database to display the correct web page in the browser.
If there are configuration errors or corrupted data, the connection to the database can fail. As a result, WordPress displays the “Error establishing a database connection” message instead of the website content.

This error may not appear right away if a cached version of the page is available, which can make it harder to detect at first. However, being unable to access the WordPress admin dashboard is a strong indication of a database connection issue.
What Causes “Error Establishing a Database Connection” in WordPress
Several common factors can cause the “Error Establishing a Database Connection” in WordPress:
- Incorrect database login details: WordPress uses the credentials stored in the wp-config file to connect to the database. If you change the MySQL username or password with your hosting provider but don’t update them in wp-config.php, the connection will fail.
- Corrupted database: Malfunctioning plugins or incompatible themes can damage database tables or their contents, resulting in this error.
- Database server issues: Problems with your hosting provider’s server, such as permission errors or sudden traffic spikes, can prevent the database from responding properly.
- Core file corruption: The core files of WordPress are essential for managing your site’s functionality.
If this error message appears in your WordPress admin dashboard, it usually indicates that the database login credentials are incorrect.

Fixing the WordPress “Error Establishing a Database Connection” Using Different Methods
After identifying the common causes of the “Error Establishing a Database Connection” message, you can try these different methods to resolve it.. Before starting, make sure to back up your WordPress site to prevent any data loss from accidental misconfigurations.
- Verify Your Database Server
A database has a maximum limit on the number of simultaneous connections it can handle. If this limit is exceeded, the server may drop connections, leading to downtime and preventing your site from retrieving data.
Traffic spikes often cause the “Error Establishing a Database Connection.” To ease server load, consider using caching plugins such as W3 Total Cache or WP Rocket.
Additionally, network issues can lead to this error. Reach out to your hosting provider to check if other WordPress sites on the same server are also impacted.
Another reason could be insufficient database user permissions. Without the correct access, WordPress cannot connect to the website database.
- Log in to cPanel.
- Click Website at the top menu and select Dashboard for your site.
- On the management page, choose File Manager to proceed.

- Open the public_html folder.
- In the left sidebar, click Create a New File..
- Name the file (e.g., save.php).
- Insert the following code into the file.
- Replace the MySQL username and password with your actual credentials
<?php
$link = mysqli_connect(‘localhost’, ‘username’, ‘password’);
if (!$link) {
die(‘Could not connect: ‘ . mysqli_error());
}
echo ‘Connected successfully’;
mysqli_close($link);
?>
Click the disk icon in the upper-right corner to save the file.
- Open a new browser tab.
- Navigate to the file by entering the following URL:
yourdomain.com/public_html/save.php
If the WordPress debug script displays no errors, it means your database user has the correct permissions and the connection is functioning correctly.

If an error appears, update the user permissions to fix the connection issue:
- In cPanel, go to Databases → List of Current MySQL Databases and Users.
- Click the three-dot icon next to the database user.
- Select Change Permissions.
- Check all permission boxes and click Update.
- Run the debug script again to see if the problem is resolved.
- Verify the Database Credentials
Incorrect login details are a common cause of the “Error Establishing a Database Connection” message, especially after migrating a WordPress site to a new host.
To fix it:
- Verify that the database host, username, and password in your hosting control panel match the values in the wp-config.php
- If you’re unable to access the hosting control panel, use an FTP client such as FileZilla to view and edit your site’s files.
- Depending on your hosting provider, you might need to download the file, make the necessary edits, and then reupload it manually.
- You can edit the file directly using the File Manager in cPanel.

- Open File Manager and go to the public_html folder.
- Search wp-config.php, right-click on it, and select Edit..
- In the file, find the following database details:
- DB_NAME – database name
- DB_USER – database username
- DB_PASSWORD – database password
- DB_HOST – database host
- Securely save this information for reference and comparison.
- Go back to cPanel and select Manage My Databases.

- In cPanel, check the database information under List of Current MySQL Databases and Users.
- If the credentials match those in wp-config.php but the error remains, move to the next solution.
- If they don’t match, update wp-config.php with the correct details. Add this code:
define(‘DB_NAME’, ‘your_current_database_name’);
define(‘DB_USER’, ‘your_current_username’);
define(‘DB_HOST’, ‘your_current_host’);
- Replace the placeholders with your actual values.
- Click the disk icon to save the file.
- Reload your WordPress site to see if the error has been resolved..
- If the Error persists, reset the database password:
- Navigate to the Current Users.
- Click the Change Password.
- Enter a new password and click Update.

If the database username and password are correct but you still see the “Error Establishing a Database Connection” message, check the DB_HOST value in wp-config.php.
- It should be set to localhost, 0.0.1, or a specific URL, depending on your hosting provider’s requirements.
- Contact your host to confirm the correct value.
- If you haven’t recently moved your WordPress site, the DB_HOST setting is unlikely to be the issue.
- Repair Corrupted Files
Corrupted files may also trigger the “Error Establishing a Database Connection” error, often due to conflicts between a theme or plugin and the WordPress core files..
To fix it:
- Deactivate the most recent plugin or theme you installed, as it’s the likely cause.
- If you’re using multiple third-party plugins, deactivate them all and then reactivate them one at a time to identify the issue.
- In your WordPress dashboard:
- Go to Plugins from the sidebar.
- Select all plugins, check the box at the top.
- Select Deactivate from the Bulk actions drop-down menu.
- Then, click Apply..

- You cannot disable all themes at once from the WordPress admin panel.
- Instead, go to Appearance → Themes and activate them one by one to test.
- If the database error blocks access to the dashboard, disable plugins manually:
- Open File Manager or connect using an FTP client..
- Go to public_html, then open the wp-content
- Right-click the plugins folder and select Rename.
- Enter a new name (e.g., plugins_old) and confirm.
This will temporarily deactivate all plugins.

Once the folder is renamed, you should regain access to your site’s backend and admin area.
Go back to File Manager and rename the folder back to plugins to restore normal plugin functionality.
- All deactivated plugins will now appear in your WordPress admin dashboard.
- Activate each plugin one at a time to find the faulty one.
- Once found, click Delete under the plugin’s name to remove it.
If the error still exists, your WordPress core files may be corrupted. You will have to replace them:
- Get the most recent WordPress version from the official website.
- Unzip the downloaded file and access the folder.
- Remove the wp-content folder and the wp-config-sample.php file from it.
- Using File Manager or an FTP client, upload all other files to your website’s root directory.
- Fix the Database in WordPress
A damaged database may trigger the “Error Establishing a Database Connection” message. This often happens when plugins or themes are installed and removed frequently, leaving behind unnecessary data.
If the database is damaged, you might see:
“One or more database tables are unavailable. The database may need to be repaired.”
To fix the database:
- Open File Manager and navigate to public_html.
- Right-click wp-config.php and choose Edit.
- Insert the following line at the end of the file:
define(‘WP_ALLOW_REPAIR’, true);
- Click the disk icon to save changes.
- In your browser, go to:
http://www.your-site-domain.com/wp-admin/maint/repair.php - You’ll see two repair options. Click Repair Database and allow the process to complete.

- Return to your WordPress site and see if the error is fixed..
- If the issue is fixed, remove the define(‘WP_ALLOW_REPAIR’, true); line from wp-config.php immediately.
- Keeping it active may let unauthorized users access your site’s backend..
5. Create a New Database
If the issue persists, your current database may be fully corrupted. You might have to set up a new MySQL database and restore your site from a backup.
The process varies by hosting provider. Follow these steps:
- Sign in to cPanel and access your website management panel.
- Go to Files → Backups from the sidebar.
- Click on Database Backups to download your latest database backup.

- Choose the problematic database from the drop-down list. Click Show Database to see the available backups.
- Choose the backup date you want and click Download.
- Wait for cPanel to prepare the file. Once ready, click Download Backup.
- It’s wise to download several backups in case the newest one fails..
Next, create a new database and restore your data using phpMyAdmin:
- In cPanel, go to Databases → Management from the sidebar.
- Enter a new MySQL user, database name, and password.
- Click Create Database to complete the setup.

- Scroll down and click Enter phpMyAdmin for the new database.
- In phpMyAdmin, open the Import tab.
- Click Choose File and select your downloaded backup file.
- Go to the bottom and click Import to restore your database.

- Open File Manager and navigate to the public_html folder.
- Right-click wp-config.php and choose Edit.
- Enter your new MySQL username, database name, and password.
- Save the file.
- Your WordPress site should now connect successfully, and the dashboard should function normally.
- Ensure Your Domain Points to Your Hosting Provider
When switching to a new host, your files move to a new server. If your domain doesn’t point to the right database host, data retrieval fails. This results in connection errors on your site.
To resolve this:
- Verify that your hosting control panel nameservers match a WHOIS lookup.
- In cPanel, go to Websites → Dashboard → See Details under your plan.
- Or, check your domain’s nameservers using an online tool like DNSChecker.

- If the nameservers match, your domain is set up correctly and should work.
- If they don’t, update them in your domain registrar’s panel with the correct details from your host.
For domains:
- In cPanel, navigate to Domains → Manage.
- Under Domain Information, click Change beside the nameservers.
- Enter the new nameservers and save.
Alternatively, you can connect your domain to hosting with an A record, which links your domain to an IP address..
Note: This method works only if your server has a static IP address.

To do this, use an online DNS lookup tool to check your domain’s current A record.
Then, sign in to your hosting account and find your server’s IP address.
Then:
- Access your domain registrar’s control panel.
- Update the existing A record with the IP address provided by your host.
- Update Your WordPress Site URL
If you see a database connection error after migrating to a new host, your WordPress site URL may have changed. An incorrect URL can stop MySQL from sending data to your site.
To fix it:
- Access phpMyAdmin. Go to cPanel → Databases → Management and click the access button.
- From the left sidebar, select your WordPress database.
- Click the SQL tab and enter the following commands:
UPDATE wp_options SET option_value = replace(option_value, ‘http://www.old-domain.com’, ‘http://www.new-domain.com’) WHERE option_name = ‘home’ OR option_name = ‘siteurl’;
UPDATE wp_posts SET guid = replace(guid, ‘http://www.old-domain.com’,’http://www.new-domain.com’);
UPDATE wp_posts SET post_content = replace(post_content, ‘http://www.old-domain.com’, ‘http://www.new-domain.com’);
UPDATE wp_postmeta SET meta_value = replace(meta_value,’http://www.old-domain.com’,’http://www.new-domain.com’);
- Replace http://www.old-domain.com with your old URL and http://www.new-domain.com with your new URL.
- Scroll down and click Go to execute the script.

How to Avoid the “Error Establishing a Database Connection”
This error can seriously affect your website’s traffic and SEO. After resolving it, follow these best practices to avoid future issues:
- Enable automatic backups
Regularly back up your WordPress files and database. This provides a restore point if issues arise and helps prevent data loss. - Optimize your database:
Optimize your database by using plugins like WP-Optimize to remove unnecessary data. An overloaded database may slow your site and cause connection errors. - Install only trusted themes and plugins:
Download themes and plugins only from trusted sources like the official WordPress library.Avoid unfamiliar sources, as they may have malware or unstable code. - Choose a reliable web host:
Select a host with strong uptime and dependable support. - Stay up to date:
Regularly update WordPress core, themes, and plugins. - Monitor your site regularly::
Perform routine checks on your site and database to detect and fix issues early before they escalate.
Conclusion
The “Error Establishing a Database Connection” occurs when WordPress fails to retrieve data from the database. Common causes include incorrect login credentials, corrupted files, or server issues.
This guide covered the most effective solutions:
- Run a test PHP file to check the database status and user permissions.
- Ensure your MySQL hostname, username, and password match in both your hosting control panel and wp-config.php.
- Disable any plugins or themes causing conflicts, and replace corrupted core files with fresh ones from a new WordPress installation.
- If the problem remains, repair the database or create a new one using your backup.
- After a domain change, update your site URL in phpMyAdmin and verify nameservers with a DNS checker.
To prevent this error in the future:
- Regularly clean and optimize your database using tools like WP-Optimize..
- Host your site with a reliable provider. 24x7wpsupport offers 24/7 support and excellent uptime to keep your site running smoothly.
Need Expert Help?
If you’re still facing the “Error Establishing a Database Connection” issue or any other WordPress problem, we’re here for you!
👉 Live Chat: Connect instantly with our WordPress experts at 24×7 WP Support.
📞 Call Our Support Team:
- US & Canada: 888-818-9916
- UK: 800-069-8778
- Australia: 1800-990-217
Get fast, friendly, and reliable assistance—24 hours a day, 7 days a week!
Looking for more WordPress/WooCommerce help? Subscribe to our YouTube Channel for expert video tutorials. Join us on Twitter and Facebook for updates, tips, and insights.