Skip to content

Error establishing a database connection in cPanel WordPress

Fix the WordPress "Error establishing a database connection" page on a cPanel account by checking five things in order, from the wp-config.php credentials to the health of the tables. Each check shows the exact error it produces, so you can tell the causes apart and fix the right one.

Your WordPress site on a cPanel account shows a white page that says “Error establishing a database connection”, and nothing else. WordPress shows that same page for at least four different faults, so the page alone cannot tell you what broke. The fastest way through is to test the database login from wp-config.php yourself, because the MySQL client prints a numbered error that points at one cause. Work through five checks in order, and the first one that fails tells you what to fix.

What you’ll accomplish

  • Read the four database settings WordPress uses and test them exactly as WordPress does.
  • Turn the vague browser page into one specific error code, such as 1045 or 1044.
  • Fix the cPanel-specific causes: prefixed names, a password changed in only one place, and a user that was never added to its database.
  • Confirm whether MariaDB or MySQL is running, out of memory, or out of connections.
  • Check and repair the WordPress tables, and know when a repair is the wrong move.

Prerequisites

You need SSH access to the cPanel account, or at least the cPanel File Manager and the MySQL Databases page. Most commands below run as the cPanel user. The service checks in step 4 need root on the server, so on shared hosting you will hand that step to your host.

WP-CLI, the WordPress command-line tool, makes several checks faster. Run wp --info to see whether it is installed. Every check also has a way to do it without WP-CLI.

The examples use a cPanel account named shopco, a database shopco_wp482 and a database user shopco_wp. Replace them with your own names. Current cPanel servers run MariaDB, a drop-in replacement for MySQL, or MySQL itself; the commands work on both.

How it works

On every page load, WordPress reads four constants from wp-config.php: DB_NAME, DB_USER, DB_PASSWORD and DB_HOST. It then opens a connection to the database server and reads the wp_options table to learn its own address and settings. A failure anywhere along that path ends in the same white page.

A ladder of five checks a WordPress request passes to reach its database on cPanel. One, the credentials in wp-config.php; if wrong, the mysql test prints ERROR 1045 Access denied. Two, the cPanel database user being added to the database, which is highlighted; if not, ERROR 1044 access denied to database. Three, MariaDB running; if stopped or out of memory, ERROR 2002 Can't connect. Four, free connection slots; if full, ERROR 1040 or 1203. Five, readable tables starting with wp_options; if not, WordPress shows a page saying tables are unavailable. When all five pass, the page loads with real content and status 200.

Two details are specific to cPanel. First, cPanel adds your account name and an underscore to every database and database user you create. You type wp482 in the form and get shopco_wp482. Second, creating a user and creating a database are separate steps in cPanel. A user only gets access when you add it to the database with privileges, and forgetting that step is easy after a migration or a manual restore.

WordPress also has a second message that looks related: “One or more database tables are unavailable. The database may need to be repaired.” This one means the connection worked. WordPress logged in, could not read wp_options, and found that some other WordPress tables do exist. That points at damaged or missing tables, not at credentials.

If WP_DEBUG and WP_DEBUG_DISPLAY, WordPress’s switches for showing errors on screen, are both on, the page shows a longer version that mentions wp-config.php and names the database host. It is the same failure with more words.

Step-by-step

1. Confirm which message you have. Load the site from your terminal so a browser cache cannot fool you.

curl -s https://www.example.com/ | grep -o -E "Error establishing a database connection|One or more database tables are unavailable"

Example output:

Error establishing a database connection

If you see the first message, continue with step 2. If you see the second, jump to step 6. If you see neither, the site may be loading again, or a cache plugin or CDN (content delivery network, the edge cache in front of your site) is serving an older copy.

2. Read the settings WordPress is using. From the site folder, usually ~/public_html, print the database name, user, host and table prefix. The table prefix is the short label, wp_ by default, that starts every WordPress table name.

cd ~/public_html
wp config get DB_NAME
wp config get DB_USER
wp config get DB_HOST
wp config get table_prefix

Example output:

shopco_wp482
shopco_wp
localhost
wp_

These wp config commands read the file without connecting to the database, so they work even while the site is down. Without WP-CLI, open wp-config.php in the File Manager and find the define( 'DB_NAME', ... ) lines. Check that the name and user both start with your cPanel account name and an underscore. A value like wp482 without the prefix almost always comes from a site copied from another server.

3. Test the login exactly as WordPress does. Use the same user, host and database. Leave the password off the command line so it does not land in your shell history; the client will prompt for it. Paste the value of DB_PASSWORD from wp-config.php.

mysql -u shopco_wp -p -h localhost shopco_wp482 -e "SELECT 1;"

Example output when everything at this level works:

+---+
| 1 |
+---+
| 1 |
+---+

If you get that table, the credentials and the database assignment are fine, and the fault is further down the ladder. Otherwise, the error code tells you where to go.

Error you seeWhat it means on cPanelGo to
ERROR 1045 (28000): Access denied for user 'shopco_wp'@'localhost' (using password: YES)Wrong password, or the user does not existStep 4a
ERROR 1044 (42000): Access denied for user 'shopco_wp'@'localhost' to database 'shopco_wp482'The user exists but is not added to this database, or the database name is wrongStep 4b
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)The database service is not runningStep 5
ERROR 1040 (HY000): Too many connectionsThe whole server is at its connection limitStep 5
ERROR 1203 (42000): User shopco_wp already has more than 'max_user_connections' active connectionsThis one user is at its own connection limitStep 5

On cPanel, a database that does not exist often shows up as 1044 rather than “Unknown database”, because the user has no rights on a name that is not there. Step 4b covers both cases.

4a. Fix a 1045 by setting one password in both places. The usual story is a password changed in cPanel but never copied into wp-config.php, or the reverse. In cPanel, open MySQL Databases, scroll to Current Users, and choose Change Password next to shopco_wp. Use the generator, copy the new password, and save it. Then put the same value in wp-config.php. Unlike the step 3 test, this command keeps the password in your shell history, so on a shared login use the File Manager instead.

wp config set DB_PASSWORD 'paste-the-new-password-here'

Example output:

Success: Updated the constant 'DB_PASSWORD' in the 'wp-config.php' file with the value 'paste-the-new-password-here'.

Run the step 3 test again. It should now print the SELECT 1 table. If the password contains a single quote, edit wp-config.php in the File Manager instead, because quoting it safely on the command line is error-prone.

4b. Fix a 1044 by adding the user to the database. First list what cPanel thinks exists, and which users each database has. The uapi command is cPanel’s own command-line interface to the same functions the web pages use, and it runs as the cPanel user.

uapi Mysql list_databases

Example output (trimmed):

---
apiversion: 3
func: list_databases
module: Mysql
result:
  data:
    -
      database: shopco_wp482
      disk_usage: 61865984
      users: []
  errors: ~
  messages: ~
  status: 1

An empty users: [] list confirms the problem. The database exists, but no user is allowed in. In cPanel, open MySQL Databases, go to Add User To Database, pick shopco_wp and shopco_wp482, click Add, tick ALL PRIVILEGES and click Make Changes. Run uapi Mysql list_databases again and shopco_wp should now appear under users.

If the database you expected is not in the list at all, DB_NAME points at a name that does not exist on this server. Either change DB_NAME to the real prefixed name with wp config set DB_NAME shopco_wp482, or import the database backup if it was never restored.

5. Check the database service and its limits. A 2002, 1040 or 1203 error is a server-side problem. It also tends to break every database-backed site on the server at once, which is a useful clue. These commands need root. On shared hosting, send your host the exact error from step 3 and the time it started.

systemctl status mariadb

Example output after the kernel stopped the service for lack of memory:

● mariadb.service - MariaDB 10.11.9 database server
     Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; preset: disabled)
     Active: failed (Result: oom-kill) since Wed 2026-10-07 03:41:19 EDT; 6h ago

The OOM killer is the Linux kernel’s last resort when memory runs out: it stops the process using the most memory, and that is often the database. On a MySQL server the unit is usually named mysqld instead of mariadb. In WHM (WebHost Manager, the root-level control panel), the same status appears under Server Status » Service Status. Confirm the memory story before restarting, then restart through cPanel’s own script.

journalctl -k | grep -i "out of memory"
/scripts/restartsrv_mysql

If the kernel log shows lines like Out of memory: Killed process 1874 (mariadbd), the restart brings the site back, but the shortage will return. Reduce what competes for memory, or add memory, before the next traffic peak.

For 1040 or 1203, see who is holding the connections.

mysql -e "SELECT user, COUNT(*) AS conns FROM information_schema.PROCESSLIST GROUP BY user ORDER BY conns DESC;"

Example output:

+-------------+-------+
| user        | conns |
+-------------+-------+
| shopco_wp   |    30 |
| blogco_wp   |     4 |
| root        |     1 |
+-------------+-------+

One user sitting at the per-user cap usually means slow queries piling up, often from a plugin or from a crawl hitting uncached pages. Raising the cap hides that problem for a while. Per-user connection limits on cPanel explains how to size max_user_connections once you have found the cause.

6. Check and repair the tables. Look at the tables when the step 3 test worked but the site still fails. The same applies when you saw “One or more database tables are unavailable”.

wp db check

Example output:

shopco_wp482.wp_commentmeta                        OK
shopco_wp482.wp_comments                           OK
shopco_wp482.wp_options
warning  : Table is marked as crashed
error    : Corrupt
shopco_wp482.wp_postmeta                           OK
Success: Database checked.

A table marked as crashed is a MyISAM table, an older storage engine that older sites and migrations still carry. These repair well.

wp db repair

Example output for the damaged table:

shopco_wp482.wp_options                            OK
Success: Database repaired.

InnoDB, the default storage engine today, does not support this kind of repair. For an InnoDB table, wp db repair answers The storage engine for the table doesn't support repair. A damaged InnoDB table usually means restoring that database from a backup.

Without SSH, WordPress has a built-in repair page. Add define( 'WP_ALLOW_REPAIR', true ); above the line that says “stop editing” in wp-config.php, then open https://www.example.com/wp-admin/maint/repair.php. Remove the line as soon as you are done, because that page works for anyone, without logging in.

Validate it works

Run all three checks. Each one proves a different part of the path.

mysql -u shopco_wp -p -h localhost shopco_wp482 -e "SELECT COUNT(*) FROM wp_options;"
wp option get siteurl
curl -s -o /dev/null -w "%{http_code}\n" https://www.example.com/

The first should print a number in a small table, proving the login and the table work together. The second should print your site address, which proves WordPress itself can read its settings. The third should print 200. The database error page returns a server-error status, so a 500 here means the site is still failing.

Finally, open the site and one admin page in a private browser window, and confirm you see real content.

Troubleshooting

The mysql test works, but WordPress still fails. Compare character by character. A trailing space inside the quotes, or a curly quote pasted from a document, makes the value in wp-config.php different from what you typed. Run wp config get DB_PASSWORD and compare it with what works.

DB_HOST says 127.0.0.1 or a hostname. Change it to localhost. With localhost, PHP connects through the local socket file, a special file the database listens on instead of a network port. cPanel also grants each database user access from localhost. A TCP address can fail with 1045 even when the password is right. If the server keeps its socket in a non-standard place, WordPress accepts localhost:/var/lib/mysql/mysql.sock as the host.

You see the WordPress install screen instead of an error. WordPress connected but found no tables with the expected prefix. table_prefix in wp-config.php does not match the tables in the database. Run wp db query "SHOW TABLES;" and set table_prefix to the prefix the tables really use. Do not run the installer, because it creates a second, empty site.

The error comes and goes. Intermittent failures usually mean step 5: the per-user connection cap, or a server close to its memory limit. Check the connection counts during a slow period and again during the error.

Everything broke right after a migration or restore. Expect several causes at once. Check the prefix in step 2, the assignment in step 4b and the password in step 4a, then retest.

The database stopped because the disk is full. A full partition looks exactly like a credentials problem from the browser, and step 5 shows MariaDB stopped or refusing writes. As root, run df -h / /var /home and look for any line at 100% in the Use% column. Free space first: old backups under /backup, rotated logs, or old binary logs (the files where the database records every change) if binary logging is on. Then restart the service with /scripts/restartsrv_mysql and repeat the test from step 3.

When this is the wrong approach

These steps fix one site’s connection. Some situations need something else.

  • Every site on the server shows the error. Treat it as a server incident. Start with step 5, and check free disk space with df -h, because a full disk stops the database too.
  • The site uses a remote or managed database. DB_HOST points at another server, so firewall rules and that provider’s limits matter more than cPanel’s MySQL Databases page.
  • The application is not WordPress. The same checks apply to any PHP app on cPanel, only the credentials live elsewhere: a .env file for Laravel, configuration.php for Joomla, or a config.php. Read the name, user, password and host from that file, run the mysql test from step 3 with them, and use the error table to pick the fix. Skip the wp commands.
  • An InnoDB table is damaged. Restore that database from your most recent good backup instead of repeating repairs.

The WordPress Advanced Administration handbook covers wp-config.php settings in more depth, and the wp db check reference lists its options.

Running WordPress that earns real money deserves an operator — Stack Harbor runs that stack under Enterprise WordPress; book an infrastructure consult.