In short
"Error establishing a database connection" means WordPress cannot connect to the MySQL/MariaDB server using the details in wp-config.php. There are three main causes: a wrong database name, user, password or host; corrupted tables; or an overloaded or unresponsive database server. Start by comparing the wp-config values with cPanel, confirm the connection with a small PHP test script, and repair the tables with WP_ALLOW_REPAIR if needed.
In this guide
- Diagnose first: which cause are you dealing with?
- Step 1: Verify the wp-config.php details
- Step 2: Test the connection with a script
- Step 3: Repair corrupted tables
- Step 4: Identify server load and resource limits
- Common special cases
- How to keep the error from coming back
- Next step: a plan that can handle your database load
"Error establishing a database connection" (shown as "Veritabanı bağlantısı kurulurken hata" on Turkish-language installs) tells you that WordPress cannot reach the MySQL or MariaDB database where your content is stored. Your site files are intact; only the connection between PHP and the database is failing. The causes fall into three groups: wrong details in wp-config.php, corrupted tables, and a database server that is unresponsive or overloaded. Follow the order below and you can tell which one it is within a few minutes.
Diagnose first: which cause are you dealing with?
| Symptom | Most likely cause | Where to start |
|---|---|---|
| Started right after a migration, password change or new install, and it is constant | Wrong details in wp-config.php | Steps 1 and 2 |
| The front end shows the error, while /wp-admin shows a different message suggesting a repair | Corrupted or inaccessible tables | Step 3 |
| The error is intermittent; it appears during traffic or bot spikes, then clears on its own | Connection limit or resource load | Step 4 |
| Every site on the same hosting account is affected | Database service or disk quota | Step 4 and a support ticket |
Step 1: Verify the wp-config.php details
WordPress reads its database details from the wp-config.php file in your site's root directory. Open the file with cPanel File Manager or FTP and look at these four lines:
define( 'DB_NAME', 'kullanici_wpdb' );
define( 'DB_USER', 'kullanici_wpuser' );
define( 'DB_PASSWORD', 'GucluSifre123' );
define( 'DB_HOST', 'localhost' );Now compare these values with the MySQL Databases screen in cPanel:
- Prefix: In cPanel, database and user names start with your account name (
kullanici_). The most common error after a migration is a prefix still set to the old account's name. - Privileges: Under "Current Users" for the database, confirm the user is assigned to this database with ALL PRIVILEGES. If the user exists but is not assigned to the database, the connection is refused.
- Password: If you are not sure, reset the user's password in cPanel and enter the same password on the
DB_PASSWORDline. If the password contains a single quote ('), you must escape it with a backslash; the easiest option is to choose a password without quotes. - Host: On cPanel hosting the value is almost always
localhost. If you use an external database server, enter the address your provider gave you and the port if needed (for example,db.ornek.com:3306).
We explain how to create a database and user from scratch in our guide to creating a MySQL database and user in cPanel.
Step 2: Test the connection with a script
WordPress's generic message does not tell you the cause, but a small PHP script shows the actual error MySQL returns. Create a file named dbtest.php in your site's root directory and fill in the details from wp-config:
<?php
$baglanti = new mysqli('localhost', 'kullanici_wpuser', 'GucluSifre123', 'kullanici_wpdb');
if ($baglanti->connect_error) {
die('Bağlantı hatası: ' . $baglanti->connect_error);
}
echo 'Bağlantı başarılı';Open https://alanadiniz.com/dbtest.php in your browser and interpret the output using the table below (the script's own messages are in Turkish: "Bağlantı hatası" means connection error and "Bağlantı başarılı" means connection successful):
| Error message | What it means |
|---|---|
Access denied for user | Wrong username or password, or the user is not assigned to the database |
Unknown database | Wrong database name, or the database was never created |
Too many connections or max_user_connections | The concurrent connection limit is reached; a load problem |
Connection refused or No such file or directory | The database service is not running or the host value is wrong |
Bağlantı başarılı | The details are correct; the problem is in the tables or intermittent load |
When you are done testing, always delete dbtest.php; it contains your database password in plain text.
Step 3: Repair corrupted tables
If the connection succeeds but WordPress still shows the error, or the admin dashboard warns that one or more tables are unavailable, the tables may be corrupted. A sudden power loss, a full disk or an interrupted operation can cause this.
WordPress's built-in repair tool
- Add this line to
wp-config.php:define( 'WP_ALLOW_REPAIR', true ); - Open
https://alanadiniz.com/wp-admin/maint/repair.phpand run "Repair Database". - Always remove the line when you are done. This page is accessible without logging in, so leaving it enabled is a security risk.
Repairing with phpMyAdmin or WP-CLI
The "Repair Database" section on cPanel's MySQL Databases screen and the repair table option in phpMyAdmin do the same job. Keep in mind that the repair command only works on some storage engines such as MyISAM and is not supported on InnoDB tables; if InnoDB tables are corrupted, restoring from a backup is the safest route. You can find out how to use phpMyAdmin in our guide to importing and exporting databases. If you have SSH access:
cd ~/public_html
wp db check
wp db repairStep 4: Identify server load and resource limits
If the error comes and goes during the day, your details are correct; the database server simply cannot accept new connections at that moment. On shared hosting, every account has concurrent connection and CPU limits. Typical causes include:
- Bot and attack traffic: Heavy requests to wp-login.php and xmlrpc.php hit the database every time. If your access logs show hundreds of requests from the same IPs, block them.
- No caching: Every visit runs PHP and the database. On a LiteSpeed server, page caching greatly reduces this load.
- Heavy plugins: Statistics, related posts or poorly written search plugins run expensive queries on every page.
- Bloated autoloaded options: Autoloaded data in the
wp_optionstable is read on every request. - Full disk quota: When the database cannot write, connection and table errors begin.
You can measure the size of autoloaded data from the SQL tab in phpMyAdmin (change the wp_ part if your table prefix is different). Current WordPress versions use values such as on and auto in the autoload column in addition to yes, so the query covers all of them:
SELECT ROUND(SUM(LENGTH(option_value)) / 1024) AS autoload_kb
FROM wp_options
WHERE autoload IN ('yes', 'on', 'auto-on', 'auto');If the result reaches several megabytes, cleaning up options left behind by plugins you removed brings noticeable relief. If your account is hitting its CPU or process limit, the 508 Resource Limit Is Reached error often shows up too; the steps for reading limits in that article also apply here.
Common special cases
The install screen appears instead of the error
If the connection works but WordPress sends you to a fresh install screen, the database is empty or the table prefix does not match. The $table_prefix value in wp-config.php must match the start of the table names you see in phpMyAdmin (for example, wp_ or wpx7_). Do not run the install wizard in this case; fixing the prefix is enough.
After restoring an old backup
When you restore from a backup, the files may come back with an old wp-config.php while the database uses a new username. First check in phpMyAdmin which database actually contains data, then edit wp-config to point to that database. If you have more than one WordPress install on the same account, make sure each site points to its own database; two sites sharing one database also causes unexpected errors.
Only at certain times, at regular intervals
If the error appears at the same times every day, look at your scheduled tasks. Backup plugins, bulk email sends or product imports can keep the database busy for minutes. Moving these tasks to low-traffic hours or splitting them into smaller batches is often enough. If you have a staging copy that shares the same database as an earlier version of your site, check that too.
How to keep the error from coming back
- Test your wp-config details right after migrations and password changes.
- Keep page caching on and delete plugins you do not use.
- Take regular database backups; for InnoDB corruption, a backup is the only reliable fix.
- Monitor disk usage and do not let old backup files pile up in your hosting space.
- If your traffic has grown, plan to move to a plan with higher RAM and CPU limits.
Next step: a plan that can handle your database load
If the error keeps returning during traffic peaks even though your details are correct, the problem is resources, not code. Sunucu Atlas WordPress hosting plans come with NVMe storage and the LiteSpeed web server; page caching on LiteSpeed sharply cuts the number of requests that reach the database. With one-click installs through WP Toolkit, the database and user are created automatically, which removes the risk of entering the wrong details.
If you run several PHP applications or databases besides WordPress, look at our web hosting plans with LiteSpeed and NVMe storage; the number of databases, RAM and CPU limits increase step by step across plans. If you frequently hit limits on your current plan, moving to a WordPress plan with higher RAM and CPU limits is the lasting fix. If you are not sure what is causing the problem, open a ticket with the Sunucu Atlas support team and include the test script's output.
Frequently asked questions
What causes "Error establishing a database connection"?
It appears when WordPress cannot connect to MySQL with the database name, user, password or host in wp-config.php. Wrong details, corrupted tables or an overloaded database server are the most common causes.
I get this error after migrating my site. What should I do?
Make sure you created the database and user on the new host and assigned the user to the database with all privileges. Then update DB_NAME, DB_USER and DB_PASSWORD in wp-config.php with the new account's prefix.
What should the DB_HOST value be?
On hosting that uses cPanel, it is usually localhost. If the database is on a separate server, enter the address your provider gave you and the port if required.
Is WP_ALLOW_REPAIR safe?
It is safe to use during the repair, but the repair.php page is accessible without logging in. Remove the line from wp-config.php as soon as you are done.
Why does the error come and go?
This usually means the concurrent connection or CPU limit is being reached. Blocking bot traffic, turning on page caching and removing heavy plugins solves the problem in most cases.