How to Fix Error Establishing a Database Connection

What Is the “Error Establishing a Database Connection” Error?

This error means WordPress cannot communicate with its database (MySQL or MariaDB). You’ll see a message like:

  • “Error establishing a database connection”
  • “Unable to connect to the database server”
  • “Can’t connect to MySQL server”

When this happens, your entire site goes down. WordPress can’t retrieve any content from the database, so nothing loads. Unlike other errors, this one isn’t caused by WordPress itself but by connectivity issues between WordPress and the database server.

This is one of the most common hosting-related errors and almost always has a straightforward fix.


Common Causes of Database Connection Errors

  • Incorrect database credentials — Wrong username, password, or database name in wp-config.php
  • Database server is down — MySQL service crashed or isn’t running
  • Database user account issues — User permissions removed or account locked
  • Hostname mismatch — Using wrong database server address
  • Database exceeded resource limits — Too many connections or storage limit exceeded
  • Network connectivity issue — Firewall blocking database connections
  • Corrupted wp-config.php — File got damaged or edited incorrectly
  • Hosting account limitations — Reached database limits on shared hosting

Solution 1: Verify Your Database Credentials

The most common cause is simply wrong login information.

Find your correct credentials:

  1. Log in to your hosting control panel (cPanel, Plesk, etc.)
  2. Find “MySQL Databases” or “Databases” section
  3. Look for your database name, username, and host

Update wp-config.php:

  1. Connect via FTP or File Manager
  2. Open wp-config.php in your root directory
  3. Find these lines:
define('DB_NAME', 'database_name');
define('DB_USER', 'database_user');
define('DB_PASSWORD', 'database_password');
define('DB_HOST', 'localhost');
  1. Replace with your actual credentials from the control panel

  2. Common DB_HOST values:

    • localhost (most common on shared hosting)
    • 127.0.0.1
    • mysql.yourdomain.com
    • Your specific server address (check your hosting docs)
  3. Save the file


Solution 2: Check Database User Permissions

The database user might exist but lack proper permissions.

Via cPanel:

  1. Go to “MySQL Databases”
  2. Find your database user
  3. Check the “User Privileges” section
  4. Ensure all required permissions are checked (SELECT, INSERT, UPDATE, DELETE, etc.)
  5. If missing, add them back

Via phpMyAdmin (if accessible):

  1. Click on “User accounts” at the top
  2. Find your database user
  3. Click “Edit privileges”
  4. Under “Database-specific privileges,” select your database
  5. Check all global privileges
  6. Click “Go”

Solution 3: Reset the Database User Password

A corrupted or forgotten password causes connection failures.

Via cPanel:

  1. Go to “MySQL Databases”
  2. Under “MySQL Users,” find your database user
  3. Click the password reset icon (usually a lock or key)
  4. Set a new password (make it strong: mix uppercase, lowercase, numbers, symbols)
  5. Click “Change Password”
  6. Update wp-config.php with the new password
  7. Test your site

Via phpMyAdmin:

  1. Click “User accounts”
  2. Right-click your user → “Change password”
  3. Enter new password twice
  4. Click “Go”

Solution 4: Check if MySQL/Database Server Is Running

The database service might be down.

Via cPanel:

  1. Log in to cPanel
  2. Look for “MySQL Processes” or “Services”
  3. Check if MySQL is running
  4. If not, click the play/restart button

Via SSH (if you have access):

systemctl status mysql

If it says “inactive” or “stopped,” restart it:

sudo systemctl restart mysql

For MariaDB:

sudo systemctl restart mariadb

Contact your host if you can’t restart MySQL from your panel. They have access to server controls.


Solution 5: Check Database Host/Server Address

Using the wrong server address prevents connections.

Find the correct address:

  1. Log in to hosting control panel
  2. Look for “MySQL Databases” or “Database Info”
  3. Check the “Host” or “Server” field

Common variations:

  • If you have multiple servers: mysql1.yourhostname.com (not just localhost)
  • Some hosts require you to add your IP to whitelist
  • Managed hosts sometimes use specific database server URLs

Update wp-config.php:

define('DB_HOST', 'mysql1.yourhostname.com');

Solution 6: Verify MySQL Port Settings

Non-standard port numbers can cause connection failures.

Check your hosting documentation for the MySQL port (usually 3306).

If your host uses a different port, update wp-config.php:

define('DB_HOST', 'localhost:3306');

Or for a remote server:

define('DB_HOST', 'mysql.yourdomain.com:3306');

Solution 7: Check Database Exists

The database itself might have been deleted or never created.

Via cPanel:

  1. Go to “MySQL Databases”

  2. Check if your database is listed

  3. If not, create a new one:

    • Click “Create New Database”
    • Enter name (usually username_dbname)
    • Click “Create Database”
  4. Then create a user and assign them to the database with all privileges

Via phpMyAdmin:

  1. Click “Databases” tab
  2. Check if your database is listed
  3. If not, create one manually

Solution 8: Check Server Resource Limits

Shared hosting plans have connection limits.

  1. Log in to cPanel
  2. Check “Concurrent MySQL Connections” (usually around 5-10)
  3. If you’re hitting the limit, either reduce active connections or upgrade hosting

Also check:

  • Database storage used vs. limit
  • Number of databases allowed

If limits are exceeded, contact hosting support for an upgrade.


Solution 9: Disable WordPress Plugins

A misbehaving database plugin can interfere with normal connections.

  1. Via FTP, rename /wp-content/plugins/ to /wp-content/plugins-disabled/
  2. Test your site
  3. If it works, rename back to plugins
  4. Disable plugins one by one to identify the culprit

Some caching or database optimization plugins can cause issues if misconfigured.


Solution 10: Test Database Connection Manually

Create a test file to verify connectivity:

  1. Create a new file called test-db.php via FTP
  2. Add this code:
<?php
$host = 'localhost';
$username = 'your_db_user';
$password = 'your_db_password';
$database = 'your_db_name';

$connection = mysqli_connect($host, $username, $password, $database);

if (!$connection) {
    echo "Connection failed: " . mysqli_connect_error();
} else {
    echo "Connected successfully to database!";
}

mysqli_close($connection);
?>
  1. Visit yourdomain.com/test-db.php
  2. If it shows “Connection failed,” the error message tells you exactly what’s wrong
  3. Delete test-db.php after testing

Solution 11: Restore from Backup

If credentials are lost and can’t be recovered:

  1. Access your hosting backup system
  2. Restore to a point before the error started
  3. The database will be restored with original credentials
  4. Check the backup’s wp-config.php for the correct credentials

Prevention Tips

  • Save your database credentials in a secure location (password manager)
  • Document your database host/server address separately
  • Don’t manually edit wp-config.php unless you’re confident
  • Keep database user permissions consistent
  • Monitor MySQL processes in your hosting panel
  • Maintain regular backups (at least weekly)
  • Test backups periodically to ensure they work

Real-world tip: I keep a backup of wp-config.php with database credentials stored securely outside my hosting account. This saves time if something goes wrong and I need to quickly verify correct settings.

Related: After fixing the database connection, make sure other critical systems are working. Check How to Fix WordPress 500 Internal Server Error or How to Fix WordPress White Screen of Death if you encounter other issues.


Conclusion

The “Error Establishing a Database Connection” looks scary but is usually fixable in minutes. Nine times out of ten, it’s incorrect credentials in wp-config.php or a MySQL user that lost permissions. Start by verifying your database information matches what’s in your hosting control panel, then work through the other solutions systematically. If your hosting provider is responsive, they can also help diagnose database server issues quickly.

Leave a Comment