How to Fix “Error Establishing a Database Connection” in WordPress

Written by

in

The “Error Establishing a Database Connection” message is one of the most common critical errors in WordPress.

When this happens, your website may become completely unavailable.

The error means WordPress cannot establish a working connection to its database.

In this tutorial, we’ll examine the most common causes and explain how to fix them safely.

What Causes This WordPress Database Error?

WordPress stores important information in a MySQL or MariaDB database.

This includes posts, pages, users, settings, and other website data.

When WordPress cannot communicate with the database, it may display the connection error.

Common causes include:

  • Incorrect database credentials
  • A stopped or unavailable database server
  • An incorrect database hostname
  • Database user permission problems
  • Too many database connections
  • Corrupted database tables
  • Server resource exhaustion

Before making changes, back up your website files and database if possible.

Step 1: Check Your wp-config.php File

WordPress stores database connection settings in wp-config.php.

This file is normally located in the WordPress installation directory.

Open it using your hosting file manager or an SSH connection.

Look for these settings:

define('DB_NAME', 'wordpress_db');
define('DB_USER', 'wordpress_user');
define('DB_PASSWORD', 'your_database_password');
define('DB_HOST', 'localhost');

Check that:

  • DB_NAME matches the actual database name.
  • DB_USER is a valid database user.
  • DB_PASSWORD matches the user’s password.
  • DB_HOST points to the correct database server.

The values above are examples. Your actual configuration will be different.

Do not share your real database password publicly.

Step 2: Check Whether MySQL Is Running

If you manage your own Linux server, check the database service.

For MySQL:

sudo systemctl status mysql

On some distributions, the service may be named mysqld.

For MariaDB:

sudo systemctl status mariadb

If the service is stopped, investigate the reason before restarting it.

You can also inspect recent service logs:

sudo journalctl -u mysql -n 50

Replace mysql with the appropriate service name.

If the database stopped because of insufficient disk space or memory, simply restarting it may not solve the underlying problem.

Step 3: Test the Database Connection

If you have command-line access, try connecting with the same database user.

For a local database:

mysql -u wordpress_user -p wordpress_db

Enter the password when prompted.

If the connection succeeds, the database server and credentials are likely working for that connection method.

If it fails, examine the error.

For example:

Access denied

The username, password, or database privileges may be incorrect.

Can’t connect to local MySQL server

The database service may be stopped, the socket path may be incorrect, or the server may be unavailable.

Unknown database

The configured database name may not exist.

Remember that localhost can use a Unix socket while 127.0.0.1 generally uses TCP. These connection methods may have different configurations.

Step 4: Check Database User Permissions

The WordPress database user needs appropriate permissions on its database.

An administrator can inspect existing privileges using a statement such as:

SHOW GRANTS FOR 'wordpress_user'@'localhost';

The hostname in this example must match the actual database account.

Do not automatically grant global privileges or use the MySQL root account for WordPress.

Follow the principle of least privilege and grant only the permissions required by the application.

Step 5: Check Disk Space and Server Resources

A database server may stop working when the server runs out of disk space.

Check available disk space:

df -h

You can also examine memory usage:

free -h

If the disk is full, identify large files and logs before deleting anything.

Avoid manually deleting database files from MySQL’s data directory.

If your server frequently runs out of memory, investigate database configuration, traffic, and other running services.

Step 6: Investigate Database Corruption

In some cases, damaged database tables can cause WordPress errors.

However, do not assume that database corruption is the cause of every connection failure.

First confirm that the database server is running and the credentials are correct.

If logs indicate table corruption, create a backup and use the repair method appropriate for your database engine.

WordPress also provides a database repair feature, but enabling it can expose a repair endpoint without normal authentication.

If you use this feature, follow the official WordPress instructions and disable it immediately afterward.

For production databases, consult your hosting provider or database administrator before attempting risky repairs.

Step 7: Contact Your Hosting Provider

If you use shared hosting and cannot access database services directly, contact your provider.

Explain:

  • When the problem started
  • Whether the error affects the entire website
  • Whether you recently changed database credentials
  • Whether your hosting account reached resource limits
  • Any relevant error messages

Your hosting provider may be able to identify database outages or account restrictions.

How to Prevent Future Database Connection Errors

Consider the following practices:

  • Keep reliable database backups.
  • Monitor available disk space.
  • Use secure database credentials.
  • Avoid unnecessary database plugins.
  • Keep WordPress and plugins updated.
  • Monitor database availability.
  • Test changes on a staging website.

For business-critical websites, consider uptime monitoring to detect failures quickly.

Frequently Asked Questions

Will I lose my WordPress posts because of this error?

Not necessarily. A database connection error does not automatically mean your data has been deleted.

Can a WordPress plugin cause a database connection error?

Plugins can contribute to resource exhaustion or database problems, although incorrect credentials and database service failures are also common causes.

Should I reinstall WordPress?

Usually not. Reinstalling WordPress should not be your first troubleshooting step because it may not address the database connection problem.

Conclusion

The WordPress database connection error is usually caused by a configuration, availability, or server resource problem.

Start by checking wp-config.php, confirming that MySQL or MariaDB is running, and testing the database credentials.

Avoid destructive database operations until you understand the cause and have a reliable backup.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *