Originally published: September 26, 2023 by Rianna MacLeod
Experiencing the ‘Error Establishing a Database Connection‘ on your WordPress website? This common error indicates that your site is unable to connect to its database, rendering it inaccessible to users — and potentially impacting your visitors, user experience, and sales.
In this blog post, we’ll explain what the database connection error means, common reasons for the issue, and how to troubleshoot and fix the problem to get your website back online.
Quick answer: The “Error Establishing a Database Connection” message usually means WordPress cannot reach the database credentials defined in wp-config.php or cannot connect to the MySQL/MariaDB server. Start by restoring a clean recent backup if one is available, then verify DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST, confirm the database server is running, repair corrupted tables if needed, and scan for malware if configuration and hosting checks do not explain the outage.
Contents:
- What is ‘Error Establishing a Database Connection’?
- How to fix a database connection error in WordPress
- What causes the ‘Error Establishing a Database Connection’ issue?
- Can website malware cause errors connecting to a database?
- Frequently asked questions
What is ‘Error Establishing a Database Connection’?
The “Error Establishing a Database Connection” is a WordPress error message that appears when the CMS cannot connect to its MySQL or MariaDB database.

This error occurs because WordPress needs to fetch site information from the database to display it on visitors’ browsers. Without a successful database connection, no data can be retrieved, resulting in the error message on a blank page.
The most common reason for this error is incorrect credentials in the wp-config.php file; however, it can also appear if database tables are corrupted, the database server is unavailable, the site has exhausted database connections, or WordPress files involved in the connection process are damaged.
Understanding how WordPress connects to the database
WordPress consists of two key parts: your website files and a database.
Your website files consist of media content (such as images) along with theme, plugin, and core files such as index.php and wp-login.php, while your database stores the meta, page, and post data, custom settings, user accounts, roles, and password hashes, and content.
When a visitor navigates to your website, the files communicate with the database via PHP and MySQL functions to extract the data and visualize it on a web page.

For the healthiest setup, run WordPress on a currently supported PHP version and a modern MySQL/MariaDB server. WordPress.org recommends PHP 8.3 or greater and MariaDB 10.6 or MySQL 8.0 or greater. Older PHP and database versions may still run WordPress, but end-of-life software increases security, compatibility, and performance risks.
If your website fails to connect to the database, it can’t retrieve the data required to display the page and triggers the “Error Establishing a Database Connection” message. This usually blocks access to the WordPress dashboard and may return a 500-level response, depending on your server, caching layer, and error handling.
While visitors may not immediately see database connection errors on your site due to caching, it’s important to address the cause and fix the issue as soon as possible to keep your website online and accessible.
How to fix a database connection error in WordPress
Before you start troubleshooting, you’ll want to check for a recent website backup. Restoring your website from the most recent backup and rebooting your web server may be the path of least resistance and a good starting point.
If you don’t have a backup to restore from, then you’ll want to start now. Please make a backup of your entire WordPress site and database (if you can still connect to it using other tools) before proceeding with these next steps. This will help you easily restore your site in the event of an unexpected issue.
There are many tools and services you can use to backup your website. If you like the command line, you can use WP-CLI to export your WordPress database with wp db export, then use SFTP, SSH, your host’s backup tool, or another trusted backup process to copy your WordPress files. WP-CLI’s database commands use the credentials in wp-config.php, so they are useful when WordPress configuration loads successfully, but they should not be treated as a complete site-file backup by themselves.
If your host provides a staging environment, test database repairs, core-file replacement, or malware cleanup there first. On production, keep a record of what you change and preserve copies of wp-config.php, .htaccess, and any database exports so you can roll back quickly if a troubleshooting step causes a new issue.
Once you’ve got a backup of your website files and database, you can follow these steps to fix the database connection error in WordPress.
- Check your database name and login credentials in wp-config.php
- Make sure your database is up and running
- Fix any corrupted WordPress files
- Repair your corrupted database
- Check with your host for issues with your database server
- Scan your server and database for malware
Step 1: Check your database name and login credentials in wp-config.php
Quite often, the “Error establishing a database connection” message occurs due to incorrect database login credentials. This is especially common right after people migrate their WordPress site to a new hosting provider.
The connection details for your WordPress site are stored in the wp-config.php file, which is most often found at the root of your WordPress site.
The wp-config.php file contains four critical pieces of information required for a successful database connection:
- Database Name – DB_NAME
- Database username – DB_USER
- Database password – DB_PASSWORD
- Database hostname (server) – DB_HOST
So, let’s go ahead and check your credentials to make sure they match.
To access your wp-config.php file, connect to your site via an SFTP client and navigate to the root of your site. Alternatively, if you are using cPanel, you can click on File Manager, then browse to the root of your site and right-click to edit the file.
Check the database name:
Once you’ve opened up wp-config.php, you’ll see your database name. Check these values against those on your server.
For cPanel users:
- Log in to phpMyAdmin under the Database section. You should see your database name on the left.
- Compare this name against the DB_NAME value in your
wp-config.phpfile.
If your database names match, then this isn’t the problem. But if they don’t match, you’ll need to update the DB_NAME value in your wp-config.php file to match the database name found in phpMyAdmin.
Check the database username and password:
If your database name is correct but you’re still getting a connection error, you’ll want to check your username and password next.
A safer way to test the same credentials is to use SSH, WP-CLI, or the MySQL client so you avoid placing a database test script in a publicly accessible directory. Try one of these options if your host supports command-line access:
wp db checkwp db climysql -h database_hostname -u database_username -p database_name
If you must use a temporary PHP file, restrict access to your IP address, include the database name, use mysqli_connect_error() for connection failures, and delete the file immediately after testing:
You can try creating a new PHP file in the root directory of your WordPress site that attempts to connect to the database using the credentials in your wp-config.php file.
<?php $connection = mysqli_connect('database_hostname', 'db_user', 'db_password', 'db_name'); if (!$connection) { die('Database connection failed: ' . mysqli_connect_error()); } echo 'Database connection succeeded'; mysqli_close($connection);
This test can help confirm whether the database credentials work, but it does not prove that WordPress can fully render the site. Plugins, core files, file permissions, object caching, or server configuration can still cause problems after the database connection succeeds.
If this connection fails, we’ve now verified that your username and/or password are incorrect. To fix this, you will need to change your password and update it in the wp-config.php file to match what is specified on your database server.
For cPanel users:
- Click on MySQL Database under the Database section.
- Locate your MySQL user.
- Select the Change Password link.
- Update your user with a strong and unique password.
- Click Change Password to proceed.

- Update the DB_USER and DB_PASSWORD values in your
wp-config.phpfile to match your database credentials.
If you’re on a shared hosting environment, you might only have one database username and you might not be able to change it. In this case, contact your hosting provider for assistance.
Check the database hostname (server):
If you’re still receiving the error after checking your database name, username, and password, then your hostname (DB_HOST) could be the issue. For many traditional shared-hosting accounts, DB_HOST is localhost, but managed, containerized, and cloud hosting environments may use a different internal hostname, socket path, port, or private IP. Check your hosting control panel or provider documentation before changing DB_HOST.
If you’ve followed all these steps and are still receiving the “Error establishing a database connection” message, move to the next step.
Step 2: Make sure your WordPress database is up and running
In some cases, heavy server traffic can overload your host server; this is a more common issue for shared hosting environments.
To begin, try connecting to your database through phpMyAdmin from your cPanel environment, or use Adminer to ensure your database server is actually running. If you have other sites in the same environment, try checking those sites as well to check if your database server is down or not.
If the WordPress dashboard is still accessible, go to Tools > Site Health > Info and review the Database and Server sections to confirm the database extension, server version, PHP version, and other environment details before you contact your host.
If the connection works fine for other environments, move to the next step. But if the database is down, you’ll want to get in touch with your hosting provider and ask about your database server.
Step 3: Fix corrupted WordPress core files
Corrupt core WordPress files could be yet another reason you’re seeing the “Error establishing a database connection” message. Corrupted files could happen due to a failed update, a problem with transferring files via FTP, or even be an indicator of compromise.
If you have SSH access, you can also run wp core verify-checksums before replacing files. This checks WordPress core files against the official WordPress.org checksums and can help identify missing or modified core files. It does not check custom themes, premium plugins, uploads, or files outside WordPress core.
If verification is difficult because of locale/version mismatches, the command can be run with explicit --version and --locale arguments.
To fix corrupted WordPress core files:
- Download a fresh copy of WordPress from the official website.
- Unzip the downloaded file on your local computer and open it.
- Delete the wp-content folder to avoid overwriting your existing themes and plugins.
- Delete the wp-config-sample.php file. It’s just a sample and you don’t need it.
- Connect to your website using your favorite SFTP client, such as FileZilla.
- Navigate to the root directory of your WordPress installation. This is usually named ‘public_html‘ or ‘www’.
- Upload the remaining files from your local WordPress folder to your website to overwrite existing files and replace everything except for your ‘wp-content‘ folder and ‘
wp-config.php‘ file with fresh copies. - Clear your browser cache to ensure you’re not loading any old files from your cache.
- Visit your website to see if the error has been resolved.
After replacing core files, confirm file ownership and permissions are still correct for your hosting environment. Incorrect ownership or restrictive permissions can prevent PHP from reading WordPress files or writing temporary/cache files, creating symptoms that look like deeper application problems.
If you’re still seeing the connection error after replacing the WordPress files, there might be an issue with your database or server. Proceed to the next step.
Step 4: Repair your corrupted database
In some cases, the “Error establishing a database connection” message can occur because your database has become corrupted.
If the database is corrupted but MySQL is still working, you can try restoring your database from a backup. If you don’t have a backup, you can take a stab at restoring corrupted database tables.
Corrupted database tables can occur if tables have been added or removed by new plugins and themes, or due to more unexpected issues like server crashes or website malware. While it isn’t directly related to this particular database connection error, it may help avoid other problems and preserve some of your website data.
WordPress includes a built-in feature to repair your database tables. To enable this feature, you need to add a line of code to your wp-config.php file:
define('WP_ALLOW_REPAIR', true);Once you’ve added that line, save the changes to wp-config.php and navigate to the following URL in your web browser:
https://www.examplewebsite.com/wp-admin/maint/repair.phpReplace “www.examplewebsite.com” with your actual website URL. You should now see a page with two options: “Repair Database” and “Repair and Optimize Database“.
Since you’re troubleshooting an outage and you have a backup, it’s recommended to use the “Repair Database” option — it’s quicker.
Click on “Repair Database” to initiate the repair process. At this point, WordPress will attempt to fix any issues with your database. Once the repair is done, you should see a success message.
Important: Remember to remove the line of code you added to your wp-config.php file after the repair process is done. If you leave this line of code in wp-config, the repair page remains accessible to anyone — this is a potential security risk.
If you have SSH access and WP-CLI is available, you may also be able to repair the database from the command line with wp db repair. This runs a repair process using the database credentials defined in wp-config.php.
Repairing a corrupted database with phpMyAdmin
If you’re a phpMyAdmin user, this tool offers the option to quickly repair MySQL database tables. Keep in mind, a table repair is not a substitute for a clean backup.
To repair a corrupted database with phpMyAdmin:
- Connect to phpMyAdmin.
- Select the affected database.
- Click on the Check all option.
- Click on the With Selection dropdown and choose Repair Table.
Depending on the storage engine and type of corruption, a repair command may not be able to recover all data. If phpMyAdmin reports that a table does not support repair, or if the database server continues crashing, stop making changes and ask your host or database administrator to investigate from the server side.
If you’ve tried repairing the database and are still seeing the error, take a look at the next troubleshooting step.
Step 5: Check with your host for issues with your database server
If none of the above steps resolves the issue, it’s possible that the problem lies with your database server. This could be due to the server being down or overloaded, or there may be a configuration issue.
You may need to contact your hosting provider for assistance. They should be able to check the status of the server and fix any server-related problems that might be causing the error.
When contacting your host, ask them to check the database server status, database quota, disk space, maximum connections, slow queries, recent restarts, and any MySQL/MariaDB error logs around the time the issue began. If the error is intermittent, include timestamps and traffic patterns so they can correlate the outage with server load or connection spikes.
Too many concurrent connections to your database can also result in database connection errors, which happens when your server reaches its maximum capacity of handling connections at once. This is because many hosting providers limit the number of simultaneous connections their servers can handle.
Shared hosting environments can face this issue, as multiple websites share the same resources, making it easier for the server to reach its limit. In such cases, consider upgrading to a higher-performance hosting plan or a managed WordPress hosting provider that can handle a larger amount of traffic and minimize server-related issues.
Remember to be patient when dealing with server-related issues. It might take a bit of time for your hosting provider to diagnose and fix the problem.
Step 6: Scan your server and database for malware
While much rarer, website malware is another potential cause of the “Error establishing a database connection” message. If hackers manage to inject malicious code into your site, it could corrupt your files or database.
Before cleaning malware, take a forensic copy of the files and database when possible. This preserves evidence of the infection and gives you a restore point if cleanup removes something required by the site. After cleanup, rotate database passwords, WordPress administrator passwords, SFTP/SSH credentials, hosting control panel passwords, and security salts in wp-config.php.
There are a couple of ways you can check for indicators of compromise and clean up malware on your WordPress site:
- Install a security plugin: WordPress security plugins like Sucuri or Wordfence can make it easy to scan your site for malware. These plugins will check your WordPress files and database for any signs of malicious code. If they find anything suspicious, they’ll alert you and provide you with steps to clean up the infection.
- Manually inspect your database tables and website files: If you suspect a specific file might be infected, you can download it via FTP and inspect it manually. Look for any code that appears out of place or suspicious. Be cautious with this method, as editing WordPress files without understanding the code can break your site.
- Update your software: Make sure all your WordPress themes, plugins, and core are updated to the latest versions. Updates often include security patches for known vulnerabilities that can help prevent malware infections.
- Prioritize abandoned or unsupported plugins and themes: If a plugin has not been maintained, replace it with a supported alternative rather than simply updating everything else around it. Outdated extensions are a common path to reinfection, even after the database connection error has been resolved.
- Seek help: If you’re not comfortable dealing with malware or if the infection is severe, consider hiring an expert. Sucuri offers professional malware removal services and quick turn-around times to help you clean up an infection in your server or database.
- Strengthen your website security: Beyond dealing with the immediate issue, consider steps to improve your website’s security overall. This might include using strong passwords, limiting login attempts, implementing a website firewall, and regularly backing up your site.
Remember, dealing with malware can be complex. It’s crucial that you ensure you’ve completely cleaned your site to prevent reinfections. Always keep a backup of your site before making any changes. If in doubt, consult with a professional.
What causes the ‘Error Establishing a Database Connection’ issue?
In short, the most common causes are incorrect database credentials, an unavailable database server, corrupted database tables, damaged WordPress core files, exhausted server resources, and malware that modifies configuration files or overloads the database.
Let’s take a look at some common causes for database connection errors in WordPress.
Incorrect database login credentials
One of the most common causes of the “Error Establishing a Database Connection” is incorrect database login credentials.
WordPress uses separate login credentials to connect to its MySQL or MariaDB database. If the user or password are changed for any reason — or you migrate your blog to another host and the WordPress files are not updated with the new details — the connection to the database will fail. This disconnect between stored credentials and actual database login details can disrupt the operation of your website, resulting in a connection error.
This is especially likely after a migration, domain move, host change, password reset, database user update, or restoration from an old backup. If the file and database were restored from different points in time, wp-config.php may contain credentials that no longer match the active database user.
Corrupted database
A corrupted database is another common cause of this error. A WordPress database can become corrupted due to a variety of factors including abnormal process termination, sloppy server administration — or, much more rarely, MySQL bugs, software issues, and server-level malware; for example: ransomware that encrypts your database files.
When critical information within the database is missing or corrupted, the database can’t function properly, which disrupts the website’s ability to connect to the database, ultimately leading to a DB connection error.
Issues with the database server
Sometimes the issue lies not with the website itself, but with your database server — especially if it’s hosted on a shared server.
The most obvious circumstance could be that your database server is currently under maintenance – for example, updating important software or moving to another server. Or it could have simply run out of space and resources. But in other cases, sudden surges in traffic or too many concurrent connections can overload the server and cause it to become unresponsive. This issue can be especially common in shared hosting environments where resources are used by multiple websites simultaneously.
Database server issues may be temporary or intermittent. A site can work normally for some visitors and fail for others if the database reaches connection limits during traffic spikes, automated bot activity, backup jobs, malware scans, or long-running queries.
Corrupted WordPress core files
Your core WordPress files dictate the functionality and operation of your website. If these files become corrupted due to malicious plugins, incorrect modifications, website malware, or issues during file transfers, they can interfere with your website’s ability to connect to the database.
If core files are repeatedly corrupted after replacement, treat that as a sign of a persistent compromise, deployment problem, or server-level issue. Replacing core files may restore availability, but it will not remove the root cause if an attacker still has access.
Can website malware cause errors connecting to a database?
Yes. Malware can cause WordPress database connection errors by changing wp-config.php, corrupting database tables, deleting or moving required files, exhausting database connections, or encrypting files and database data during a ransomware incident.
One common way malware can cause database connection errors is by altering the website’s configuration files.
For example, wp-config.php contains crucial information about the website’s database including its name, the username, password, and host. If malware manages to modify or corrupt your wp-config.php file, it can lead to connectivity issues and errors.
Our malware remediation team frequently removes malicious code from compromised wp-config.php files during cleanup engagements. A buggy malware injection or sloppy malware removal rules can easily corrupt this file, making the WordPress database unreachable.
In other cases, malware might not alter the configuration files directly, but instead overload the database server. Malicious scripts can create an unnaturally high number of requests to the database, causing the server to reach its maximum capacity and refuse any additional connections. This can manifest as intermittent database connection errors, especially during peak traffic times.
Malware can also lead to other types of database connection errors. For instance, some types of malware, known as ransomware, can lock you out of your database entirely. The malware encrypts your database, making it inaccessible. It then demands a ransom to decrypt it. In such cases, you might see a database connection error because your website can no longer access the data it needs.
Additionally, malware can also cause connection errors by attacking the server’s file system. For example, it might include code that deletes or moves crucial files. And if your website or database files are deleted or moved, your website may not be able to connect to your DB.
Scanning your website for indicators of compromise, using a website firewall, and keeping all software patched with the latest updates are some of the best defenses against website malware.
Once the site is back online, monitor it for recurring errors. Review server logs, database logs, security plugin alerts, uptime checks, and traffic spikes for at least a few days. If the error returns, the original fix may have addressed a symptom rather than the root cause.
If you suspect your website or database has been infected, you can check out our free guide on how to clean up a hacked WordPress website. And if you need a hand, our trained security analysts are available 24/7/365 to help clean up a website malware infection!
Frequently asked questions
What is the fastest way to fix “Error Establishing a Database Connection” in WordPress?
The fastest safe fix is usually to restore a clean recent backup if you have one. If not, check the database name, username, password, and host in wp-config.php, then confirm the database server is online.
Can I fix this error from the WordPress dashboard?
Usually, no. This error often blocks access to wp-admin because WordPress needs the database to load the dashboard. You’ll typically need SFTP, SSH, phpMyAdmin, Adminer, cPanel, or help from your hosting provider.
Does this error mean my WordPress site was hacked?
Not always. Incorrect credentials and server issues are more common. However, malware can cause this error by changing wp-config.php, corrupting files, overloading the database, or damaging database tables, so a security scan is a smart step if normal troubleshooting does not explain the outage.
Should I repair or optimize the WordPress database?
If your site is down, start with Repair Database rather than Repair and Optimize Database. Optimization can take longer on large sites and is not always necessary during an outage. Remove WP_ALLOW_REPAIR from wp-config.php immediately after the repair page is no longer needed.
Why does the error keep coming back?
Recurring database connection errors often point to intermittent database server overload, too many concurrent connections, long-running queries, bot traffic, malware, insufficient hosting resources, or a plugin/theme creating excessive database requests. Check logs and ask your host to review database performance around the exact times the error appears.








