VPS Tutorials
Error establishing a database connection in WordPress: how to fix it (2026)
Fix Error establishing a database connection in WordPress: read the /wp-admin/ message, then check wp-config.php, the database service, memory and tables.

When WordPress shows only “Error establishing a database connection”, do not reinstall and do not restore an old backup yet. Open /wp-admin/ on the same domain first: the admin side prints a longer message that tells you whether the problem is the credentials, the database name or the tables.
The message means WordPress could not reach or read its database. It does not mean your content is gone. This guide follows the WordPress 7.1.5 core code and official documentation for a regular single site. It is a procedure to follow, not the result of a test on your site.
The front end prints only the heading, and it prints the same heading for a wrong database name or unreadable tables. The admin side is specific:
| What /wp-admin/ shows | What it means | Go to |
|---|---|---|
“Error establishing a database connection” followed by a paragraph naming wp-config.php and the database server | The database server is unreachable, or it rejected the username or password | Steps 1, 2 and 3 |
| “Cannot select database” | The login worked, but the database name is wrong or this user has no rights on it | The database name in step 1 |
| “One or more database tables are unavailable. The database may need to be repaired.” | The connection works; a core table cannot be read | Step 4 |
| A custom error page | The site has a wp-content/db-error.php template that replaces the message | Work through the steps in order |
The paragraph in the first case includes the database host WordPress is trying to reach, so note it down.
The WordPress common errors page groups the causes into three: wrong details in wp-config.php, a problem at the host (the database hit its quota and was shut down, or the server is down), and a compromised site.
wp-config.php sits in the WordPress root next to wp-admin and wp-content; a few sites keep it one directory higher. Open it with your host's file manager or SFTP and download a copy before editing.
define( 'DB_NAME', 'database_name' );
define( 'DB_USER', 'database_user' );
define( 'DB_PASSWORD', 'database_password' );
define( 'DB_HOST', 'localhost' );
Compare each line with the database page of your hosting panel:
- Name and user: they must match the panel exactly. Many hosts add an account prefix, so the real name looks like
account_wp. - Password: if unsure, set a new password for that database user in the panel and paste it into
DB_PASSWORD. Letters and digits are the least error-prone; a single quote or backslash must be escaped as PHP requires. - Host: usually
localhost, but some hosts run databases on a separate server and show its address in the panel. A non-default port is written like127.0.0.1:3307; see the wp-config.php database settings. - Quotes: keep plain straight quotes. Text pasted from a chat app or word processor can bring curly quotes, and the site will fail with a different error after saving.
With Docker, the host is the name of the database service in your Compose file, not localhost. The official image receives it through WORDPRESS_DB_HOST and related variables, as the official image documentation describes.
For “Cannot select database”, look only at DB_NAME: does that database exist, is the prefix complete, and has the user been added to it with privileges in the panel?
Let the database state the reason instead of guessing.
On a VPS with SSH, log in with the same details. Put the value of DB_HOST after -h and type the password when prompted. If the mysql command is missing, use mariadb.
mysql -u database_user -p -h localhost database_name
With only a panel or shared hosting, replace the existing define( 'WP_DEBUG', false ); line in wp-config.php with the three lines below. If the line is absent, add them above the /* That's all, stop editing! line. Reload the site once, open wp-content/debug.log and find the line containing mysqli_real_connect().
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
| Number | Meaning | What to do |
|---|---|---|
1045 | The username, password or connecting host was rejected | Reset the password as in step 1; if it persists, use the MySQL 1045 guide |
2002, 2003 | No database server answered | DB_HOST is wrong or the database is not running; go to step 3 |
1040, 1203 | The connection limit is full | See the section on errors that come and go |
1044, 1049 (from the command line) | No rights on that database, or it does not exist | Check DB_NAME and the user's privileges |
MySQL explains 2002 and 2003 in its connection troubleshooting page. WordPress advises against debug mode on live sites in its debugging documentation, so restore define( 'WP_DEBUG', false ); as soon as you have the number and delete debug.log, which can contain server paths and the database username.
On shared hosting you cannot check this yourself; send the message at the end of this guide to support. On your own VPS, find the service name, then read its status and recent log:
systemctl list-unit-files | grep -Ei 'mysql|mariadb'
systemctl status mariadb --no-pager
journalctl -u mariadb -n 50 --no-pager
Replace mariadb with the name from the first command, commonly mysql, mariadb or mysqld. If you use a hosting control panel, check the database service on its services page. On a local stack such as XAMPP, confirm that MySQL is started in its control panel.
If the status is not active (running), find out why it stopped before starting it:
free -h
df -h
journalctl -k --since "2 days ago" | grep -Ei 'out of memory|killed process'
mysqldormariadbdin the output of the third command means the kernel killed the database for lack of memory. This tends to happen on small VPS plans that run PHP and the database together.- A partition at 100% in
df -hwhere the database lives stops it from writing files. Free some space first. - Otherwise, read the database's own errors in the
journalctl -uoutput.
sudo systemctl start mariadb
Reload the site after starting it. If the service will not start or stops again, continue with the MySQL and MariaDB startup checklist. Do not delete the data directory.
When the admin side reports unavailable tables, the connection works and the tables are the problem. Export the database first, as the WordPress database backup guide explains. On a VPS, take a snapshot as well.
Add one line to wp-config.php:
define( 'WP_ALLOW_REPAIR', true );
Open https://example.com/wp-admin/maint/repair.php, choose Repair Database and wait for the per-table results.
- This page works without logging in, for anyone who knows the address. Remove the line as soon as you finish; see the wp-config reference.
- It checks each table and runs
REPAIR TABLEon the ones that fail. MySQL documents that statement as working for MyISAM, ARCHIVE and CSV tables only; see REPAIR TABLE. Current MySQL and MariaDB versions create InnoDB tables by default, so if the page reports a failed repair, clicking again will not help. - After a failed repair, leave things as they are and restore the damaged table from a recent backup, or ask the host to handle it at database level.
wp-config.php moved with the files and still holds the old host's database name, user and host. Create the database and user on the new host, import the data, then replace all four values from step 1.
If the site opens the WordPress installation screen asking for a language and site title, do not continue. WordPress connected to the database but found no tables with the current prefix: either DB_NAME points to an empty database, or $table_prefix does not match the imported tables. Check in phpMyAdmin whether the tables start with wp_ or something else and make $table_prefix match.
Wrong settings fail every time. An error that appears and disappears is usually about resources: the database was killed for lack of memory and started again, or connections are exhausted by traffic, bots or slow queries.
Use the number from step 2 and the memory evidence from step 3 to decide: add swap, lower the number of PHP workers, cache pages, or move to a plan with more memory. On shared hosting, a per-user connection limit such as 1203 can only be raised by the host or a plan upgrade.
- Open the homepage and a post in a private window, then log in to the dashboard.
- Confirm that
WP_DEBUGis back tofalse,debug.logis deleted and theWP_ALLOW_REPAIRline is gone. - If you changed the database password, check that no other application still connects with the old one.
- Export a fresh database backup and note the cause and time.
- If the settings and the host are both fine, or
wp-config.phpchanged when you did not edit it, treat the site as compromised: change every password, then run a security check.
For support, you can send: “The front end shows ‘Error establishing a database connection’ and /wp-admin/ shows [full message]. I compared the database name and user in wp-config.php with the panel and reset the database user's password, and it still fails. It started at [time]; the last change was [migration/password change/update/none]. Please check whether the database server is running, whether this user can connect to this database from the web server, and whether a connection or storage quota was reached.” Include the error number. Do not send the database password.
You may need next:
Next step: compare the actual plans
Filter the listed plans by budget, specifications and region, or start with a buying guide.