How to Fix a WordPress Database Connection Error

How to Fix a WordPress Database Connection Error

https://images.openai.com/static-rsc-4/HiIrlCRRBV4jG64H6LtOKwdVJUzpsbwi9jlQDy5d6pmYPlP3JMS9Jfy2sl4PbJN-g832EJWH4JPLF23AL5SuDxRaA2v8xJD5XdvUfitJRvjMfuELVzbY2DYWV8xx98gf2OuEadkFSnh0qMCwbonE1ZGn6G9rTcRs0nV1Qkk8li6mk4RTbs128ZBNRAZ7h0JQ?purpose=fullsize
https://images.openai.com/static-rsc-4/chJoUpWJt6Ik-HOI5kMgcLKIkJ15M_ZZGKXludjRCxuOm4-KxxL2HqXbRqxBQNt9FFWZsV16iJkW2pRKe6SHxogHgQIWJsR04jXiw5cOWRijSg7OXenNEI8ZUC327Wrgt1IbFr6Xxhduw9uNktpZkM7ueOrgbkIGVRxqTeKteCYwFu_0HB_dbjzk4IWnJ7XL?purpose=fullsize
https://images.openai.com/static-rsc-4/LxHAxT3sGwEdhgBj4q8TzcSdsmV8DJaj8mSIvcY46SDjA3yJFnTWwsk4reMnXw1sHYyZNTeOTY0p9zOR3QeCXEJDEDI1_lS_f-fqyPnw4OeX4wKLAokfqH-igLuwNw8EKakneBoFg-oSsD6eh2bBtTRqUh4sTCjM8SHRXS0peC_3gyVdpnKV0xzPGAzj7cZV?purpose=fullsize

4

Seeing the message “Error Establishing a Database Connection” on your WordPress website can be alarming. Instead of loading your homepage, WordPress displays an error message and your website may become completely inaccessible.

The good news is that this problem is usually fixable. In many cases, the cause is related to incorrect database credentials, a database-server problem, corrupted database tables, hosting issues, or a misconfigured WordPress installation.

This guide explains what the WordPress database connection error means, why it happens, and how to fix it step by step.


What Is a WordPress Database Connection Error?

WordPress stores most of your website’s important information in a database.

This includes:

  • Posts and pages
  • User accounts
  • Website settings
  • Comments
  • Plugin settings
  • Theme settings
  • Categories and tags
  • Other WordPress configuration data

When someone visits your website, WordPress needs to communicate with the database to retrieve this information.

If WordPress cannot establish a connection to its database, it may display:

Error Establishing a Database Connection

https://images.openai.com/static-rsc-4/mTUzgSzmXCVL034zFFzs2UMNor6w32SYY2TGsHKbZ4330o2FeF5rqJ95oTQvZ_YnqXRjtYQ5C0RzB5kuC_0ipvRIxxqiVldx3BoU8rLW10_gcQP_guQFxElrGF0NPuLvoTc0oN6uGfvP6KpRVrqHX_Zb3Ot6CCIYSpclxtX2FHNin6XXiVbXJZ9cgpt4Wa9s?purpose=fullsize
https://images.openai.com/static-rsc-4/HiIrlCRRBV4jG64H6LtOKwdVJUzpsbwi9jlQDy5d6pmYPlP3JMS9Jfy2sl4PbJN-g832EJWH4JPLF23AL5SuDxRaA2v8xJD5XdvUfitJRvjMfuELVzbY2DYWV8xx98gf2OuEadkFSnh0qMCwbonE1ZGn6G9rTcRs0nV1Qkk8li6mk4RTbs128ZBNRAZ7h0JQ?purpose=fullsize
https://images.openai.com/static-rsc-4/LxHAxT3sGwEdhgBj4q8TzcSdsmV8DJaj8mSIvcY46SDjA3yJFnTWwsk4reMnXw1sHYyZNTeOTY0p9zOR3QeCXEJDEDI1_lS_f-fqyPnw4OeX4wKLAokfqH-igLuwNw8EKakneBoFg-oSsD6eh2bBtTRqUh4sTCjM8SHRXS0peC_3gyVdpnKV0xzPGAzj7cZV?purpose=fullsize

4

In simple terms, the website’s WordPress files and database are unable to communicate properly.


What Causes the WordPress Database Connection Error?

There isn’t just one cause.

Common causes include:

  1. Incorrect database credentials
  2. Incorrect database hostname
  3. Database server outage
  4. Corrupted database
  5. Corrupted WordPress files
  6. Hosting-server problems
  7. Too many database connections
  8. Damaged database tables
  9. Incorrect wp-config.php settings
  10. Recently installed plugins or themes
  11. Server resource limitations
  12. Database being deleted or unavailable

The first step is identifying which problem is affecting your website.


1. Check Whether Your Hosting Server Is Working

Before changing WordPress files, check whether your hosting account and database server are functioning normally.

Log in to your hosting control panel and look for:

  • Server-status information
  • Database services
  • Resource usage
  • Hosting notifications
  • Maintenance notices
  • Error logs

If your hosting provider is experiencing a database outage, changing WordPress configuration files won’t solve the problem.

Contact your hosting provider if:

  • Your database server is down
  • Multiple websites on the same hosting account are affected
  • Your hosting account has reached its resource limits
  • The database service keeps restarting
  • You recently migrated your website

Ask the hosting provider whether the MySQL/MariaDB database server is running normally and whether your database is accessible.


2. Check Your WordPress Database Credentials

One of the most common causes is incorrect database information in the wp-config.php file.

The important settings are:

define( 'DB_NAME', 'database_name' );
define( 'DB_USER', 'database_user' );
define( 'DB_PASSWORD', 'database_password' );
define( 'DB_HOST', 'localhost' );

These values tell WordPress which database to use and how to connect to it.

https://images.openai.com/static-rsc-4/NoW82yk6on8SI966eFm4oqxskazgO8ntAAzRnT3MYQV7LngyzmN6cBkxbMCNcSKM2d3lddh17DHUkJ5vsPa0PQ6iyn8MBwFTS5jcHjshkLF8nq54PzAQ2fWIZyJF-AVchVH-19LfNDPXhaI7Epq_xh-qkl0Gq4Qmi6wEDM-tyrXRkb9FEaspj2HmAcbxhiAf?purpose=fullsize
https://images.openai.com/static-rsc-4/jOILwSIXdaH-OLWB8R-9x1MWpkS2IlL5CMOG5I9cn1-QCm3mKjFZlglVAmz0whjuRzq_D9OA80K2uq3AwtDcL2CReY-WKew_qvSErpa8GOzJmYvxHvsBk3Mo-Jn7ZfPnuXX81qRBoeateclWBJJuhOGvpVcHK-StXEkN_6ISdRK4bqZww-igs_s-uff0Fk6_?purpose=fullsize
https://images.openai.com/static-rsc-4/5e679m_wt5HyBwgHAqnBCzBM2JvA_hPQT8WlO0ZvsLFHEeQwGDRDAQhJL6rtFaGBV458lXwPkQHavxqy5IDIkCvYshBJ7xY0TL3gP2YCmI9u_jsoFpu2semYe_KEUq-VZ4VMWtXUt65qX6WgvaF9B0YjNSZLl01hstPJoxO1iYFjMBrsTVU_cVBzPijCvPkb?purpose=fullsize

4

Check these four values carefully:

DB_NAME
The name of your WordPress database.

DB_USER
The database username.

DB_PASSWORD
The password assigned to that database user.

DB_HOST
The database server hostname.

Even a small typo can prevent WordPress from connecting.


3. Verify the Database Name

Log in to your hosting control panel and open your database-management section.

Depending on your hosting provider, this may be:

  • MySQL Databases
  • phpMyAdmin
  • Database Manager
  • MySQL/MariaDB Management

Check whether the database specified in wp-config.php actually exists.

For example, if your configuration contains:

define( 'DB_NAME', 'mywebsite_wp' );

but your hosting account contains a database called:

mywebsite_wordpress

WordPress won’t be able to connect to the expected database.

Correct the value if necessary.


4. Check the Database Username and Password

The database username and password also need to be correct.

In your hosting panel, verify:

  • Database username
  • Database user
  • Password
  • User-to-database assignment
  • User privileges

A particularly common problem occurs after a website migration or hosting change.

For example:

Old server

Database: example_wp
User: example_admin
Password: old-password

New server

Database: example_wp
User: example_wpuser
Password: new-password

If wp-config.php still contains the old credentials, WordPress will fail to connect.


5. Check DB_HOST

Many WordPress installations use:

define( 'DB_HOST', 'localhost' );

But localhost is not universal.

Some hosting environments require a different database hostname.

Your hosting provider may specify something like:

mysql.example.com

or another server hostname.

If you’re unsure about the correct DB_HOST, contact your hosting provider.

Don’t randomly change it because the correct value depends on your hosting environment.


6. Check Whether the Database Server Is Running

If your database credentials are correct, the database server itself could be unavailable.

WordPress commonly uses MySQL or MariaDB-compatible database systems.

If the database service is down, WordPress cannot retrieve your website data.

Possible causes include:

  • Server maintenance
  • Database service failure
  • Hosting outage
  • Resource exhaustion
  • Server configuration problems

If you have managed hosting, ask the hosting company to confirm whether the database server is operational.


7. Repair the WordPress Database

Sometimes the database itself has damaged or corrupted tables.

WordPress includes a database-repair feature that can be enabled through wp-config.php.

Add:

define( 'WP_ALLOW_REPAIR', true );

Then visit:

https://yourdomain.com/wp-admin/maint/repair.php

You should see WordPress’s database repair options.

https://images.openai.com/static-rsc-4/xxTWrbhLTSfOn2Cj6BXI8AKgz1sE3idGzaXv4YM44A8FaGvQIdZSS4ZjcqFQvXsSIdDgm6-Mh0tjl0LAa2mPkYhihgzZAQyO3kxIhH0KPZhfRKu9MGiklhp1XTQHEbLoTZDfWUiMF0K2JNpIlhXYH24n5TN_eY7QSLnePHfSvJeQn4_9jVCEE_CxeH9XP6Mv?purpose=fullsize
https://images.openai.com/static-rsc-4/G2aYrQvfHSyjac7CiTT5wRKtlb-hQ-llktIrDgR_i3UadW4YaTtNg9tE_Be4AHX2VW-or95iHof4v4eQvIwW6uwj9IohERIfoZy2AxxmCltJh40-lFEBYALYnzbD7DvthmfIMDlpRQTymSNXzVDWyxU1v_Vp2ha5YtCihzliGKw3AHdeJe1SadSP-czpLvp2?purpose=fullsize
https://images.openai.com/static-rsc-4/67WDhIoZQCHPoZwuZKmw1JuWi2GwwIQ7yX2YE7c924igcjOtOjgAT_ymd2KGu2vajQPNIHPoR0CXvc2AMR5m8oN7Gl7WUs95F6AjmXpqJ-KvZUjHO1Mgu0iiLr0qoHdUl_6sb5HDG7x0PqmOiiX_VzGhP2vP9sEPiH6gS84c0fQr_JmN8jwPWGddyHKgA-bE?purpose=fullsize

4

You can use the available repair options to attempt to repair damaged database tables.

Important security note

After finishing the repair, remove the WP_ALLOW_REPAIR line from wp-config.php.

Leaving database-repair access enabled isn’t something you should do unnecessarily.


8. Repair the Database Through phpMyAdmin

Many hosting providers provide phpMyAdmin.

Open phpMyAdmin and select your WordPress database.

You’ll see tables such as:

wp_posts
wp_users
wp_options
wp_comments
wp_terms
wp_postmeta

Your table prefix may be different from wp_.

Select the tables and look for the Repair table option where supported.

https://images.openai.com/static-rsc-4/67WDhIoZQCHPoZwuZKmw1JuWi2GwwIQ7yX2YE7c924igcjOtOjgAT_ymd2KGu2vajQPNIHPoR0CXvc2AMR5m8oN7Gl7WUs95F6AjmXpqJ-KvZUjHO1Mgu0iiLr0qoHdUl_6sb5HDG7x0PqmOiiX_VzGhP2vP9sEPiH6gS84c0fQr_JmN8jwPWGddyHKgA-bE?purpose=fullsize
https://images.openai.com/static-rsc-4/NeyH2412FMtfsAQEQQvdiS2PW8MGorJVBc8nrqyWTjfhzCb9y9XCPFhdmh4ZP2UT7T0C8dTw7PYRsvaj0dSflhAlimqEBfoyngWs6FEbkQDtMr560U83Szcrw90y9e6XojZ_bzI4BS_6RleS1rw5Ch0DksNNMsk5j0nrLO2PPtdm8Ot6A7xPfoW3dgXz4Va4?purpose=fullsize
https://images.openai.com/static-rsc-4/AmbaSsxoW6isoBWy0sEimQ6vZm0ZzFlTmgF_pHAhZfP3itHNDc-atTFyzn7aO2n-6wwAghodOO5KjR5IHEpnSjUWPtkljyp5knyZoqA1SbkQY9wTL4g18LQRm02eH60E_NVsBsb0NEGBm37hRwQONCAaWUziraCJu6vHdM2Dzi3t1y1pgqVAXwf9SNo0hCqi?purpose=fullsize

5

If the database has serious corruption, however, repair may not be enough. A backup or hosting-provider assistance may be required.


9. Check Your Database User Permissions

The database user must have appropriate permissions for the WordPress database.

If the user exists but isn’t properly assigned to the database, WordPress may not be able to access it.

In your hosting control panel, check whether the database user is assigned to the correct database.

Depending on your hosting interface, you may need to enable the required privileges.

If you’re unsure which permissions are appropriate, ask your hosting provider rather than changing database privileges blindly.


10. Check Your Website’s wp-config.php

The wp-config.php file is one of the most important configuration files in WordPress.

Look for:

define( 'DB_NAME', 'database_name' );
define( 'DB_USER', 'database_user' );
define( 'DB_PASSWORD', 'database_password' );
define( 'DB_HOST', 'localhost' );

Also check for accidental syntax problems.

For example, this is incorrect:

define( 'DB_NAME' 'database_name' );

There should be a comma:

define( 'DB_NAME', 'database_name' );

A PHP syntax error can cause a different error, but configuration-file mistakes can still prevent WordPress from working correctly.


11. Check Whether Your Database Has Been Deleted

This sounds obvious, but it can happen during:

  • Hosting migrations
  • Website restoration
  • Manual cleanup
  • Account changes
  • Server moves

If the database specified in wp-config.php no longer exists, WordPress cannot connect to it.

Check your hosting database manager.

If the database is missing, look for a backup before creating a replacement database.

Creating an empty database with the same name will not restore your website content.

Your posts, pages, users, settings, and other data must be restored from the actual database backup.


12. Temporarily Disable Plugins

A plugin can sometimes contribute to database-related problems.

If you cannot access the WordPress dashboard, you can temporarily disable plugins through your hosting file manager or SFTP.

Go to:

/wp-content/

Then rename:

plugins

to something like:

plugins-disabled

Try loading the website again.

If the website starts working, a plugin may be involved.

You can then restore the folder name and troubleshoot the plugins individually.

Important

Don’t assume that a plugin is definitely responsible just because disabling it changes the behavior. Database and server problems can produce similar symptoms.


13. Switch to a Default WordPress Theme

A theme can also introduce PHP or database-related problems.

If the issue began immediately after a theme change, try switching to a default WordPress theme.

If you can’t access the dashboard, you may need to use phpMyAdmin or another supported method to change the active theme.

Before editing the database manually, make a backup.


14. Check Your Server’s Resource Limits

Sometimes the database isn’t technically broken—the server simply doesn’t have enough resources to handle requests.

Check your hosting account for:

  • CPU usage
  • RAM usage
  • Database connections
  • PHP workers
  • I/O usage
  • Disk space
  • Process limits

Shared hosting environments can impose resource limits.

If your website suddenly receives a large amount of traffic, or a plugin creates excessive database queries, the database may become unavailable or slow enough that WordPress cannot connect successfully.


15. Check if Your Database Is Full

Disk-space problems can affect database operation.

Check your hosting account for available storage.

Look for:

  • Full disk
  • Database size limits
  • Inode limits
  • Log-file growth
  • Backup storage
  • Temporary-file usage

If the server has run out of available space, your hosting provider may need to help resolve the underlying issue.


16. Check Error Logs

Server and PHP error logs can provide useful clues.

Depending on your hosting environment, check:

  • PHP error logs
  • Apache logs
  • Nginx logs
  • MySQL/MariaDB logs
  • WordPress debug logs

You can enable WordPress debugging in a controlled troubleshooting environment using:

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );

This can write errors to:

/wp-content/debug.log

Important

Don’t leave detailed debugging enabled unnecessarily on a production website, because logs can contain information you don’t want exposed publicly.


17. Restore Your Website From a Backup

If the database is badly corrupted and you have a known-good backup, restoration may be the most practical solution.

Before restoring:

  1. Identify the backup date.
  2. Make sure it predates the problem.
  3. Preserve a copy of the current site.
  4. Back up any newer content if necessary.
  5. Confirm that the backup includes the database.
  6. Restore carefully.

Remember that restoring an old backup can remove newer posts, orders, comments, user accounts, or other data created after the backup.


WordPress Database Connection Error After Migration

A database connection error is particularly common after moving a WordPress website to a new hosting provider.

Check these items first:

1. Database imported?

Make sure the WordPress database was actually imported into the new server.

2. Database name correct?

Compare the new database name with DB_NAME.

3. Username correct?

Compare the database user with DB_USER.

4. Password correct?

Make sure the database user’s current password matches DB_PASSWORD.

5. Host correct?

Confirm the required DB_HOST.

6. User assigned?

Make sure the database user has access to the database.

7. Database server running?

Ask your new hosting provider to verify the database service.


WordPress Database Error vs. “There Has Been a Critical Error”

These two errors can look similar but usually indicate different problems.

Error Establishing a Database Connection

Usually means WordPress cannot communicate with its database.

Potential causes include:

  • Incorrect credentials
  • Database-server failure
  • Database corruption
  • Hosting problems
  • Resource limits

There Has Been a Critical Error on This Website

Usually indicates a PHP/application-level problem, such as:

  • Plugin conflict
  • Theme problem
  • PHP error
  • Incompatible code
  • WordPress/PHP compatibility issue

The exact cause should be confirmed through logs rather than assuming based solely on the error message.


How to Prevent WordPress Database Connection Errors

https://images.openai.com/static-rsc-4/vuWGc3cCJ3vUOPWSktQ_0gCZ9dI4vwboErjcilnhlrvbSQkMbFih_fp3_m7nmeJtse1WU61Rs3VR5VycOSSNCfM53vpEtgx9MK_gO1h2kDLBvWgbF14ak3_-67XvEIR7lQsxriSUZdfocybM3Ao3ZoZvfeCCytoLtF7BoKnKwUG-E4RqKoq2qWQ9hrXwHK1y?purpose=fullsize
https://images.openai.com/static-rsc-4/eVIBndqOws6MnQNzMVWk2uWM8UX3jsbottV1MKM_gZyQwytSt7upwQiEPMeitLiQspYe5fWZTgqf91TibrceRlbB9iOD2hccjCgqRs_eIh2PGED-yEhgpAE99MX5eru-kDGZN34yyDFYnia0rym4dNxB09wtBeGn1DyOrCkNpcobwa1wPlcumWcYEZL2BS-4?purpose=fullsize
https://images.openai.com/static-rsc-4/2vDdy0rDjG7L4Tn97UId6YQ3IZsWWyGQtUTuF7sqOhCNhpPRPhI9dtQdnstBxmOO1j4351Jk6sLmcsd_ssw0foNcMK9Js7_eP1XXT-78WZd5IeraKA2yT4RUJVMssz_HonyN5pUMLo3gZxAy1Kvx8ImMLhpOQxYIHEwf6EzBaYeeiNvAh-BywJkhkfANffzB?purpose=fullsize

6

1. Maintain Regular Backups

Create regular backups of:

  • WordPress files
  • Database
  • Uploads
  • Configuration files

Store important backups separately from the main hosting account when possible.


2. Use Reliable WordPress Hosting

Good hosting should provide:

  • Database monitoring
  • Backups
  • Server monitoring
  • Resource monitoring
  • Support
  • Security updates

The exact features depend on the hosting provider and plan.


3. Keep WordPress Updated

Keep your:

  • WordPress core
  • Plugins
  • Themes
  • PHP version

appropriately updated.

Updates can address bugs and security vulnerabilities.


4. Avoid Unnecessary Plugins

Every additional plugin introduces another software component that needs to be maintained.

Remove plugins you no longer use.


5. Monitor Server Resources

If your website is consistently approaching CPU, RAM, storage, or database-connection limits, consider investigating the underlying cause before it turns into an outage.


6. Test Your Backups

A backup isn’t particularly useful if it can’t be restored.

Periodically test your backup and restoration process.


Quick WordPress Database Error Checklist

CheckWhat to Verify
☐Hosting server is online
☐Database server is running
☐DB_NAME is correct
☐DB_USER is correct
☐DB_PASSWORD is correct
☐DB_HOST is correct
☐Database exists
☐Database user has access
☐Database tables aren’t corrupted
☐Server has enough disk space
☐Hosting resource limits aren’t exceeded
☐Plugins aren’t causing the problem
☐Recent migration was completed correctly
☐A working backup is available
☐Error logs have been checked

Frequently Asked Questions

Why does WordPress say “Error Establishing a Database Connection”?

It means WordPress was unable to establish a working connection to the database. Common causes include incorrect database credentials, an unavailable database server, database corruption, hosting problems, or resource limitations.


Can I fix the error without technical knowledge?

Some basic problems, such as incorrect credentials, can be fixed relatively easily. More complicated database corruption or server problems may require assistance from your hosting provider or a WordPress professional.


Will restarting WordPress fix the database connection error?

Usually, no.

If the problem is caused by incorrect credentials or database corruption, restarting WordPress won’t correct the underlying issue.

If the database server itself temporarily failed, however, the hosting provider may be able to restore the database service.


Can a plugin cause a database connection error?

A plugin can contribute to database-related problems, excessive resource usage, or application errors. However, a database connection error doesn’t automatically mean that a plugin is responsible.

Investigate server and database status before disabling everything.


Does the error mean my website data is lost?

Not necessarily.

A database connection error means WordPress cannot currently access the database. Your data may still be intact.

Before making major changes, determine whether the database exists and whether it can be accessed through your hosting control panel or database-management tool.


Final Thoughts

The “Error Establishing a Database Connection” message means WordPress cannot communicate properly with the database it needs to load your website.

The most important things to check are:

  1. Hosting and database-server status
  2. wp-config.php credentials
  3. Database existence
  4. Database-user permissions
  5. Database health
  6. Server resources
  7. Recent migrations or configuration changes
  8. Error logs and backups

Don’t immediately reinstall WordPress or delete database files. Those actions can make the situation worse and may cause unnecessary data loss.

Start by identifying whether the problem is with WordPress configuration, the database itself, or the hosting server. Once the underlying cause is identified, the appropriate fix is usually much easier to apply.