Error establishing a database connection in WordPress: causes and fix
· The AYT team · 2 min read

"Error establishing a database connection" means WordPress could not connect to its MySQL or MariaDB database, so it has nowhere to read the pages from. Most often the connection details in wp-config.php are wrong (after a move or a password change). Next come a database server that is down or overloaded and, more rarely, corrupted tables.
This error does not delete your content. Posts, pages and orders live in the database; WordPress just can't reach them.
Error establishing a database connection: finding the cause
Open /wp-admin as well as the home page:
- the same error everywhere: the connection fails (wrong details or a server that is down);
- wp-admin says "One or more database tables are unavailable" and offers a repair: the connection works, but some tables are damaged (cause 3);
- the error comes and goes from one minute to the next: the database server is overloaded (cause 2).
Cause 1: the connection details in wp-config.php
WordPress reads four values from wp-config.php, in the site's main folder:
define( 'DB_NAME', 'database_name' );
define( 'DB_USER', 'database_user' );
define( 'DB_PASSWORD', 'password' );
define( 'DB_HOST', 'localhost' );
Compare them with what your hosting panel shows in its databases section. The usual mistakes:
- after a move,
wp-config.phpstill has the database name or password from the old server; - the database user's password was changed in the panel but not in the file;
- many hosts prefix database and user names with the account name (for example
account_wp1), and the prefix is missing from the file; DB_HOSTis notlocalhosteverywhere. Some hosts give a separate server name or a port (localhost:3307).
To see exactly what the server refuses, put a temporary test-db.php file in the site's folder with the details from wp-config.php:
<?php
try {
new mysqli('localhost', 'database_user', 'password', 'database_name');
echo 'Connection OK';
} catch (mysqli_sql_exception $e) {
echo $e->getMessage();
}
Open it in your browser (https://example.com/test-db.php) and read the message:
Access denied for user: the user or password is wrong, or the user has no privileges on that database;Unknown database: the database name is wrong or the database doesn't exist on this server;Connection refused,No such file or directoryor a message withgetaddrinfo:DB_HOSTis wrong or the database server isn't running.
Delete the file straight after the test, because it contains the password.
If the password is the problem, the safest fix is to set a new password for the database user in the panel and copy it into wp-config.php. Watch the quotes: the password sits between single quotes, and a single quote inside the password breaks the line.

Cause 2: the database server isn't responding
If the details are correct and the test above says Connection refused, the MySQL/MariaDB server is stopped or no longer accepts connections. On shared hosting you can't restart it yourself. Check your host's status page and contact support.
When the error is intermittent, usually at traffic peaks, the server is overloaded or has hit its connection limit (the message Too many connections). Typical causes are a plugin that runs many queries on every page, bots crawling the site aggressively, or no page cache. A page cache and an object cache (Redis) cut the number of queries a lot; see what Redis object cache does for WordPress.
Cause 3: corrupted tables
Tables can get damaged after a sudden server shutdown or a full disk. WordPress has a repair tool, off by default. Turn it on by adding this to wp-config.php, above the line /* That's all, stop editing! */:
define( 'WP_ALLOW_REPAIR', true );
Then open https://example.com/wp-admin/maint/repair.php and choose "Repair Database". The page works without signing in while the constant is set, so remove it from wp-config.php as soon as you are done.
If the repair fails (on InnoDB tables, which most current servers use, the repair command does little), restore the database from a recent backup.
Cause 4: the database or the user is missing
After a half-finished move, the database may exist but the import never ran, or the user exists without privileges on the database. Open phpMyAdmin and check that the database has tables (wp_posts, wp_options and the rest). If it is empty, import the export again. For a walk-through of phpMyAdmin, see phpMyAdmin for beginners.
If you have just moved the site and don't know where the process stopped, go through the guide to moving a WordPress site again.
Frequently asked questions
Have I lost my site's content?
Almost certainly not. The error only says WordPress can't reach the database. Content is lost only if the database was deleted or badly corrupted, and then you recover it from a backup.
Why does the error only show up sometimes?
When it comes and goes on its own, the cause is nearly always load on the database server: too many connections at once. The details in wp-config.php don't change from one minute to the next.
Does changing DB_HOST from localhost to 127.0.0.1 help?
Sometimes. localhost usually uses the server's local socket, and 127.0.0.1 a network connection. The change only helps if your host tells you which value its server expects.
On AYT hosting
In the AYT panel you see your databases and their users on the Databases page and open phpMyAdmin straight from the panel. You edit wp-config.php in the File Manager, which has a code editor. If the database is damaged, restore it from the panel's Backup page, with the site or on its own: backups are daily for the last 7 days, weekly for 4 weeks and monthly for 24 months. Monitoring checks the site and emails you an alert when it goes down. The plans are on the WordPress hosting page.
