1. PrettyWP
  2. PrettyWP Guides
  3. Tutorials
  4. How to Fix WordPress Database Connection Error

How to Fix WordPress Database Connection Error

A database connection error in WordPress occurs when WordPress cannot establish a connection to the database. Here's a guide to help you fix this issue:

Verify database credentials: Double-check your database credentials in the wp-config.php file. Ensure the database name, username, password, and host are correctly specified. If you're uncertain, contact your hosting provider for the correct information.

Test database connectivity: Create a new PHP file (e.g., test-config.php) and add the following code:

$dbname = 'your_database_name';
$dbuser = 'your_username';
$dbpass = 'your_password';
$dbhost = 'localhost';

$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

if (!$conn) {
    die('Database connection failed: ' . mysqli_connect_error());
} else {
    echo 'Database connection successful!';


Replace 'your_database_name', 'your_username', 'your_password', and 'localhost' with the correct values. Upload this file to your WordPress root directory and access it through your browser. If you see "Database connection successful!" displayed, it means your database credentials are correct.

  • Check the database server: Sometimes, the database server may be down or experiencing issues. Contact your hosting provider to verify if the database server is running smoothly. They can also check if any server-related restrictions are blocking the database connection.

  • Repair the database: If the database connection error persists, you can try repairing your WordPress database. Add the following line to your wp-config.php file, just above the line that says, "That's all, stop editing! Happy blogging.":
define('WP_ALLOW_REPAIR', true);

Save the file, then access the following URL in your browser: http://www.yourdomain.com/wp-admin/maint/repair.php. Replace "yourdomain.com" with your domain name. On that page, click on the "Repair Database" button. WordPress will attempt to repair any corrupted database tables.

Restore from backup
: If none of the above steps resolves the issue, restore your website from a recent backup that includes a working database. This will ensure you have a clean version of your website's data.

Are you still in need of assistance? If so, we highly recommend reaching out to our support team.

Previous Fixing 404 Page Not Found Errors in WordPress
Next Fix WordPress Login Issues