# Error establishing a database connection

> PHP reached the point of trying to talk to MySQL and failed — because the credentials are wrong, the hostname is wrong, or the database server refused the connection.

Source: https://cwp.sg/errors/error-establishing-a-database-connection/  
Author: Ben Johnson  
Last verified: 2026-08-02

**Also seen as:** WordPress database error, Cannot connect to MySQL, Access denied for user

## What it means

This message means your PHP code ran, got as far as connecting to the database, and was turned away. The web server is fine and PHP is fine; the failure is strictly between your application and MySQL.

There are only four things it can be, and they are worth checking in this order:

1. Wrong username or password
2. Wrong database **hostname**
3. The connection limit for your user is full
4. The database server itself is down or the database was deleted

On shared hosting, cause 2 is the most common and the most surprising, because the value is almost never `localhost`.

## Why it happens

### The hostname is set to localhost

Nearly every tutorial on the internet says `localhost`, because on a single-server setup that is correct. On shared hosting it usually is not — your database lives on a separate database server, and you must use the hostname your host assigned you, such as `sql203.example-host.com`.

If you copied `wp-config.php` from a local development machine, this is almost certainly your problem.

### Credentials changed or were never right

Database passwords get rotated, or the site was migrated and the new database user was created with a different name. On most shared hosts the database name and username are prefixed with your account ID (`u1234_wp`), and it is easy to carry over the unprefixed name from a local copy.

### You have run out of connections

Shared hosting caps concurrent MySQL connections per user — often to a handful. If your site opens connections faster than it closes them, or a long-running query holds them open, new requests are refused with `Too many connections` and WordPress reports it as this generic message.

This version of the error is intermittent: the site works, then does not, then does again. Intermittency is the tell.

### The database was deleted or is still being created

On a newly provisioned account the database server may not have been assigned yet. If you created the account minutes ago, wait and retry before debugging anything else.

## How to fix it

### 1. Verify the four values in wp-config.php

Open `wp-config.php` and check every value against your control panel — do not trust your memory of them:

```php
define('DB_NAME',     'u1234_wordpress');
define('DB_USER',     'u1234_wpuser');
define('DB_PASSWORD', 'the-actual-password');
define('DB_HOST',     'sql203.example-host.com'); // NOT localhost
```

Watch for trailing spaces inside the quotes. They are invisible and they break authentication.

### 2. Test the connection independently of WordPress

Put this in a file, load it once, then **delete it**. It tells you whether the problem is your credentials or your application:

```php
<?php
$host = 'sql203.example-host.com';
$user = 'u1234_wpuser';
$pass = 'the-actual-password';
$db   = 'u1234_wordpress';

$mysqli = @new mysqli($host, $user, $pass, $db);
if ($mysqli->connect_errno) {
    echo 'FAILED (' . $mysqli->connect_errno . '): ' . $mysqli->connect_error;
} else {
    echo 'Connected. MySQL ' . $mysqli->server_info;
}
```

`Access denied` means credentials. `Unknown MySQL server host` means the hostname. `Too many connections` means you are at your limit.

### 3. Confirm the database still exists and has tables

Open phpMyAdmin from your control panel and check that the database is listed *and* that it contains tables. An empty database connects successfully but produces a different failure mode — usually a redirect to the installer.

If the database is genuinely gone and you have no backup, the site's content is gone with it. This is the single strongest argument for taking your own backups on any free plan.

### 4. Release stuck connections

If the error is intermittent and you suspect connection exhaustion, look for queries that never finish:

```sql
SHOW PROCESSLIST;
```

Anything in `Sleep` for a long time is a connection your application opened and never closed. In PHP, that usually means a missing `$mysqli->close()` in a long-running script, or a plugin opening its own second connection on every page load.

## On CWP hosting specifically

Your database hostname is shown on the **MySQL Databases** page of your control panel and it is never `localhost` — CWP runs databases on separate servers from web nodes, so PHP has to connect over the network.

Free accounts allow 5 concurrent MySQL connections. That is enough for a normal WordPress site with caching enabled, and not enough for an uncached site under load — which is why this error and [508 Resource Limit Is Reached](/errors/508-resource-limit-is-reached/) frequently appear together.

## When this means you have outgrown free hosting

Persistent connection-limit errors on a properly cached site mean your traffic genuinely needs more concurrent database capacity than a free plan provides.

Before upgrading, make sure it really is capacity: an uncached WordPress site can exhaust 5 connections at surprisingly low traffic, and caching is free.

---

Content Website Platform (cwp.sg) operates the free hosting it writes about; see https://cwp.sg/about/ for the methodology and the commercial disclosure.
