Error Establishing a Database Connection: How to Fix It
WordPress says Error establishing a database connection? Check wp-config.php, test the login, restart MariaDB, repair tables and fix migration mistakes.

Contents
Error establishing a database connection is one of the most alarming WordPress errors, because the whole site disappears behind a single white page. The good news: the content is usually still there. WordPress simply can't reach its MySQL or MariaDB database, and the cause is almost always one of a handful of things.
This guide covers what the message means, checking the details in wp-config.php, testing the database login, a database server that has stopped, repairing damaged tables, the settings that break after a migration, connection limits and where to look in the logs.
What does the error mean?
WordPress keeps everything except media files in a database: posts, pages, users, settings and plugin data. On every page load PHP connects to the database with the details in wp-config.php. If that connection fails, WordPress stops and shows this message. On a site set to Turkish the same error reads "Veritabanı bağlantısı kurulurken hata oluştu", in German "Fehler beim Aufbau einer Datenbankverbindung".
The usual causes:
| Cause | Typical sign |
|---|---|
| Wrong details in wp-config.php | Error right after a migration or a password change |
| Database server is down | Every site on the server shows the error |
| Damaged tables | wp-admin says tables are unavailable and need repair |
| Connection or memory limits | Error comes and goes, mostly at busy times |
| Hacked site | Error appears without anyone changing anything |
Check the details in wp-config.php
Open wp-config.php in your site's root folder (usually public_html) with the control panel's file manager or over SFTP and find these lines:
define( 'DB_NAME', 'cpuser_wp123' );
define( 'DB_USER', 'cpuser_wpuser' );
define( 'DB_PASSWORD', 'strong-password' );
define( 'DB_HOST', 'localhost' );
Compare each value with the database that really exists. In cPanel and DirectAdmin, database and user names start with your account name and an underscore, such as cpuser_wp123; forgetting the prefix is a common mistake after a migration. Also check that the password has no stray space at the start or end, and that a single quote inside the password is written as \'.
Test the database login
Don't guess whether the details are right; test them. Over SSH, with the details from wp-config.php:
mysql -u cpuser_wpuser -p -h localhost cpuser_wp123
If you get the MariaDB [cpuser_wp123]> prompt, the details are correct. Access denied for user means the user name or password is wrong, Unknown database means the database name is wrong, and an error like Can't connect to local server through socket (worded slightly differently in MySQL) means the database server isn't running or is listening somewhere else.
Without SSH access, use the panel: in cPanel's MySQL Databases (MySQL Management in DirectAdmin), check that the database exists, that the user is assigned to it with all privileges, and set a new password for the user if needed. Then put the same password into wp-config.php. To see the tables, open the database in phpMyAdmin.
Is the database server running?
If every site on the server shows the error at once, the database server itself has probably stopped. On shared hosting, contact your provider; on your own VPS check it with:
sudo systemctl status mariadb
sudo journalctl -u mariadb --since "1 hour ago"
If you use MySQL instead of MariaDB, the service is called mysql on Ubuntu/Debian and mysqld on AlmaLinux. To start it again:
sudo systemctl restart mariadb
If the service doesn't start or keeps stopping, look for the reason before restarting it again and again. The two most common ones:
- The disk is full. MariaDB can't write its data and log files and stops. Check with
df -h; our Linux disk full guide shows how to free up space. - Memory ran out. When RAM runs out, the kernel's out-of-memory killer often picks the largest process, which is usually the database. You can see it in the kernel log:
sudo journalctl -k | grep -i "out of memory"
If memory runs out regularly, reduce innodb_buffer_pool_size, add swap or move to a plan with more RAM. Installing and configuring the database server is covered in our How to install MariaDB guide.
Repair damaged tables
If the login works and the server is running but WordPress still fails, or /wp-admin says "One or more database tables are unavailable", some tables are damaged. Take a backup of the database first, then add this line to wp-config.php above the line /* That's all, stop editing! */:
define( 'WP_ALLOW_REPAIR', true );
Then open https://yourdomain.com/wp-admin/maint/repair.php and choose Repair Database. This page works without logging in, so remove the line from wp-config.php as soon as the repair is done.
With SSH access you can do the same with mysqlcheck or WP-CLI:
mysqlcheck -u cpuser_wpuser -p --auto-repair cpuser_wp123
wp db repair
DB_HOST: localhost, 127.0.0.1 or a socket
DB_HOST is localhost on almost every hosting plan. The difference matters on your own server:
localhost: PHP connects over the local Unix socket.127.0.0.1: PHP connects over TCP, usually on port 3306. You can add the port:127.0.0.1:3306.- A socket path:
localhost:/run/mysqld/mysqld.sock, if the socket isn't where PHP expects it.
If localhost fails, try 127.0.0.1, and the other way round. If the database runs on another server, enter its address and check that the database user is allowed to connect from your web server's IP and that the firewall lets port 3306 through only from there.
After a migration
Most of these errors appear right after moving a site. Check these points:
- New details: the database name, user and password on the new host are almost never the same as on the old one. Update all four lines in
wp-config.php. - Was the database imported? Open the database in phpMyAdmin and make sure the tables are there.
- Table prefix:
$table_prefixinwp-config.phpmust match the prefix of the imported tables, for examplewp_orwpab_. If it doesn't, WordPress usually shows the installation screen instead of your site. - siteurl and home: if the domain changed, the old address in the
wp_optionstable sends visitors back to the old site. You can fix it temporarily inwp-config.php:
define( 'WP_HOME', 'https://yourdomain.com' );
define( 'WP_SITEURL', 'https://yourdomain.com' );
The whole process is covered step by step in our How to migrate a website guide.
Too many connections
If the error comes and goes, the database may be refusing new connections. In the logs you then see Too many connections (the server-wide max_connections limit) or has exceeded the 'max_user_connections' resource (a per-user limit, common on shared hosting). On your own server you can check the limit and the open connections:
SHOW VARIABLES LIKE 'max_connections';
SHOW FULL PROCESSLIST;
Raising the limit only treats the symptom. Usually slow queries, a plugin that opens many connections or bot traffic without caching are behind it. A page cache such as LiteSpeed Cache cuts the number of database queries sharply.
Could the site be hacked?
If nobody changed anything and the error appeared out of nowhere, consider an attack. Malware sometimes changes wp-config.php, and an attacker with panel access can change the database password or delete tables. Check when the file was changed:
ls -l --time-style=long-iso wp-config.php
If the date doesn't match any change you made, follow our Website hacked: what to do guide before simply restoring the details.
Where to look in the logs
- WordPress: set
define( 'WP_DEBUG', true );anddefine( 'WP_DEBUG_LOG', true );inwp-config.phpand errors are written towp-content/debug.log. Turn them off again afterwards. - PHP: cPanel shows the
error_logfile in the site folder and the Errors page; DirectAdmin offers the domain's error log in the panel. - MariaDB/MySQL:
journalctl -u mariadb, or/var/log/mysql/error.logon Ubuntu/Debian and/var/log/mariadb/mariadb.logon AlmaLinux, depending on the configuration.
If the browser shows a 500 error instead of this message, the problem is more likely in PHP than in the database; our HTTP error codes guide explains the difference.
WordPress databases at Nodesty
Nodesty web hosting runs WordPress sites on AMD Ryzen 9 7900 servers with the LiteSpeed web server and NVMe storage. You manage databases and users with cPanel or DirectAdmin and phpMyAdmin, and with JetBackup you can restore a database from the panel. If you want to manage MariaDB yourself, our AMD Ryzen 9 9950X VPS plans give you full root access and free daily backups kept for 7 days.
Frequently Asked Questions
What does Error establishing a database connection mean?
WordPress keeps its posts, pages, users and settings in a MySQL or MariaDB database. The message means PHP could not connect to that database, so WordPress has nothing to show. The cause is almost always wrong details in wp-config.php, a database server that is down or a database that has hit a limit.
Are my posts and pages lost?
Usually not. The error means WordPress can't reach the database, not that the data is gone. Once the connection details are correct or the database server is running again, the site comes back as it was. Still, take a backup of the database before you repair tables or change anything.
The site shows the error but wp-admin works. How is that possible?
If wp-admin says that one or more database tables are unavailable, some tables are damaged. Enable WP_ALLOW_REPAIR in wp-config.php, run the repair at /wp-admin/maint/repair.php and remove the line again when you're done.
Should DB_HOST be localhost or 127.0.0.1?
On most hosting plans it is localhost. localhost connects over the local Unix socket, 127.0.0.1 connects over TCP on port 3306. If one doesn't work, try the other; if the database is on another server, use that server's address and make sure the user is allowed to connect from your web server.
Why does the error only appear at busy times?
The database may be running out of connections or memory. Too many connections, a max_user_connections limit on the database user or MariaDB being killed by the out-of-memory killer all cause errors under load. Caching, fewer heavy plugins or more resources solve this for good.
Can a hacked site cause this error?
Yes. Malware sometimes modifies wp-config.php, or an attacker changes the database user's password or deletes tables. If you didn't change anything and the details suddenly stopped working, check when wp-config.php was last modified and scan the site for malware.
About the author
Nodesty Team · Infrastructure & Support
The Nodesty team runs our servers, network and DDoS protection day to day and answers customer support requests around the clock. These guides come from that work.
Related articles

WordPress Upload Size Limit: How to Increase It
Fix The uploaded file exceeds the upload_max_filesize directive in php.ini: raise the WordPress upload limit in cPanel, DirectAdmin, .user.ini or on a VPS.

502 Bad Gateway: Causes and How to Fix It on Nginx
Getting 502 Bad Gateway? Read the Nginx error log, restart PHP-FPM or your app, fix the upstream port or socket, raise pm.max_children, check Cloudflare.

How to Reset the WordPress Admin Password When Locked Out
Locked out of WordPress? Reset the admin password with the lost password link, WP-CLI, phpMyAdmin or MySQL, or create a new admin user from the terminal.