How to Fix WordPress “Error Establishing a Database Connection” (2026 Guide)

WordPress database connection error fix

If you’ve ever opened your WordPress website only to see the dreaded “Error Establishing a Database Connection” message, you know how stressful it can be. Your entire site goes blank, visitors can’t access anything, and panic sets in fast.

The good news? This error is almost always fixable in under 10 minutes. Let’s walk through every cause and the exact steps to fix it.

What Causes This Error?

This error appears when WordPress cannot connect to your MySQL database. Common causes include:

  • Incorrect database credentials in wp-config.php
  • MySQL database server is down or overloaded
  • Corrupted WordPress database
  • Recent site migration changed credentials
  • Hosting server experiencing issues

Step 1: Check wp-config.php Database Credentials

Connect via FTP or File Manager and open wp-config.php. Verify these lines match your hosting panel exactly:

define( 'DB_NAME', 'your_database_name' );
define( 'DB_USER', 'your_database_user' );
define( 'DB_PASSWORD', 'your_database_password' );
define( 'DB_HOST', 'localhost' );

Log into cPanel or Hostinger and confirm each value under Databases. One wrong character triggers this error.

Step 2: Check if Your Database Server is Running

Add this line temporarily to wp-config.php:

define( 'WP_ALLOW_REPAIR', true );

Visit https://yoursite.com/wp-admin/maint/repair.php and click Repair Database. Remove the line after repair.

Step 3: Repair a Corrupted Database

If repair confirms corruption, log into phpMyAdmin from your hosting panel and run:

REPAIR TABLE wp_posts, wp_options, wp_users;

Step 4: Try Changing DB_HOST

Some hosting environments use a different host. Try replacing localhost with:

  • 127.0.0.1
  • Your server’s actual IP address
  • A hostname provided by your host

Step 5: Deactivate All Plugins via FTP

Rename /wp-content/plugins/ to /wp-content/plugins-disabled/ via FTP. If the site loads, a plugin was the cause. Rename back and reactivate one by one.

Step 6: Contact Your Hosting Provider

If nothing works, it’s a server-side issue. Contact support with your domain, the error message, and when it started. Hosts like Hostinger resolve MySQL issues quickly.

Prevent This Error in Future

  • Set up daily automated backups with UpdraftPlus
  • Install UptimeRobot (free) for instant downtime alerts
  • Keep WordPress, themes, and plugins updated
  • Use a managed WordPress host for proactive monitoring

Need a Fast Fix?

At debrajx, we specialise in WordPress emergency repairs. Contact us and we’ll have your site back online within hours.

Step-by-Step Fix: WordPress Database Connection Error

Step 1: Double-Check wp-config.php Credentials

Open your wp-config.php file via cPanel File Manager or FTP. Verify these four values match exactly what your hosting control panel shows:

  • DB_NAME — exact database name (case-sensitive)
  • DB_USER — database username
  • DB_PASSWORD — current password (hosting migrations often reset this)
  • DB_HOST — usually localhost, but some hosts use 127.0.0.1 or a custom hostname

A single incorrect character here causes the error. If you recently migrated hosting providers, these credentials will have changed entirely.

Step 2: Use WordPress Built-In Database Repair

Add this line to wp-config.php just before “That’s all, stop editing!”:

define('WP_ALLOW_REPAIR', true);

Then visit yourdomain.com/wp-admin/maint/repair.php and click Repair Database. Remove the line afterward. This fixes corrupted tables that silently cause connection failures.

Step 3: Check Database User Privileges

Log into cPanel → MySQL Databases. Make sure your database user has ALL PRIVILEGES assigned to the database. This is the most overlooked fix. After any server migration or cPanel update, privilege assignments sometimes reset silently.

Step 4: Confirm the MySQL Server Is Actually Running

On shared hosting, the MySQL server occasionally crashes under load. Log into your hosting dashboard and check server status, or contact support directly. On Hostinger, the live chat can verify your database server status in under two minutes.

Step 5: Test With a New wp-config.php

Create a simple PHP test file called dbtest.php in your root folder:

<?php
$conn = new mysqli('localhost', 'DB_USER', 'DB_PASSWORD', 'DB_NAME');
if ($conn->connect_error) { die('Failed: ' . $conn->connect_error); }
echo 'Connected successfully';
?>

Replace values with your actual credentials and visit the file in your browser. If it shows “Connected successfully,” your credentials are correct and the issue is in WordPress itself. If it shows an error, the credentials are wrong.

Preventing This Error in Future

Once resolved, protect yourself with these three habits:

  • Daily backups with UpdraftPlus — backs up both files and database automatically
  • Uptime monitoring using UptimeRobot (free) — alerts you instantly when the site goes down
  • Quality hosting — cheap overcrowded servers are the number-one cause of MySQL outages

Still Can’t Fix It? Get Professional Help

Some database errors — severe table corruption, bad migrations, or server misconfiguration — need hands-on expert access. If you’ve tried everything above and the error persists, contact debrajx for emergency WordPress support in India. We typically resolve database connection issues within 1–2 hours.

Error Establishing Database Connection on Specific Pages Only

If the error appears only on certain pages (like the admin dashboard or the cart page) but not on the homepage, the database connection itself is fine — you’re hitting a resource limit.

This happens when a specific page makes too many database queries, exhausting your connection pool. Solutions:

  • Deactivate plugins one by one to find the one making excessive queries (use the Query Monitor plugin for diagnosis)
  • Ask your host to increase max_connections for your database — shared hosts often cap this at 15–25 simultaneous connections
  • Enable object caching (Redis/Memcached) so repeated queries are served from memory rather than hitting the database

Database Repair When You Can’t Access WP Admin

If the database error prevents you from logging in to WordPress admin, you can trigger WordPress’s built-in database repair tool directly via URL.

Add this line to your wp-config.php file:

define('WP_ALLOW_REPAIR', true);

Then navigate to: https://yourdomain.com/wp-admin/maint/repair.php

You’ll see two options: “Repair Database” and “Repair and Optimize Database”. Run the repair. Once done, remove the line from wp-config.php immediately — leaving it active is a security risk as anyone can trigger a repair without logging in.

Checking Database Status via cPanel phpMyAdmin

If WordPress’s repair tool doesn’t fix the issue, inspect the database directly:

  1. Log into your hosting cPanel
  2. Go to Databases → phpMyAdmin
  3. Select your WordPress database from the left sidebar
  4. Click “Check All” at the bottom to select all tables
  5. From the “With selected” dropdown, choose “Check table”
  6. Look for any tables marked “in use” or “crashed” in the Status column
  7. Select only the crashed tables and choose “Repair table” from the dropdown

Preventing Future Database Connection Errors

Most database connection errors are preventable with these habits:

  • Keep regular backups: A working database backup means you can restore in minutes, not hours. Use UpdraftPlus to back up daily to Google Drive.
  • Avoid cheap shared hosting: Hosts that oversell their servers (putting 500+ sites on one server) cause frequent database connection issues due to resource contention. Consider upgrading to at least a mid-tier plan.
  • Keep credentials documented: Store your database name, username, and password securely (in a password manager). You’ll need them if you ever have to manually recreate wp-config.php.
  • Monitor your site uptime: Free tools like UptimeRobot check your site every 5 minutes and email you when it goes down. This lets you catch database errors before your visitors do.

Still getting database connection errors? Contact us — we diagnose and fix WordPress database issues same day.


Related Articles

Scroll to Top