Step-by-Step Guide: How to Fix - Error Establishing a Database Connection in WordPress


Reading Time: 7 minutes | Readablility: Medium

Wordpress Error establishing a database connection
Error Message:
Error establishing a database connection

This either means that the username and password information in your 
wp-config.php file is incorrect or that contact with the database server at localhost could not be established. 

This could mean your host’s database server is down.

Are you sure you have the correct username and password?
Are you sure you have typed the correct hostname?
Are you sure the database server is running?
If you are unsure what these terms mean you should probably contact your host. 

If you still need help you can always visit the WordPress Support Forums.

Step 1: Open wp-config.php and check WordPress credentials.

    The first thing that you will do is open the wp-config.php on your hosting server (GoDaddy, Hostinger, Azure, AWS, etc) and make sure that the following details are correct.

    /** The name of the database for WordPress */
    define('DB_NAME', "prod-user' );
    
    /** Database username */
    define('DB_USER', 'myuser' );
    
    /** Database password */
    define('DB_PASSWORD', 'Password123');
    
    /** Database hostname */
    define( 'DB_HOST', 'localhost' );
    1. DB_NAME: The name of your database (could be MySQL, Windows SQL Server, AWS Aroura, etc.)
    2. DB_USER: The user name to access the database.
    3. DB_PASSWORD: The database password.
    4. DB_HOST: Usually the value is localhost or 127.0.0.1, or can be an AWS like "arn:aws:rds:region:account-id:db:db-instance-name" or Azure Connection String for SQL Database like - "26f-dbserver.mysql.database.azure.com"


Step 2: Check the Database Server is running

    Make sure that the database server is up and running and you are able to connect it from the application server via telnet or ping if possible.

    Example:
    telnet prod-db 3306

Step 4: Try to Test Database Connection Programatically

    The quickest way to troubleshoot this error is by creating a simple test.php file under the WordPress root folder and see what's wrong.

    Example:
    <?php
    
    $db_link = mysqli_connect('localhost', 'your_db_username', 'your_db_password');
    if (!$db_link) {
        die('Could not connect to database: ' . mysqli_error());
    }
    echo 'Connected to the database was successful;
    mysqli_close($db_link);
    
    ?>

    Access this page via url http://<your-domain-name>/test.php


    Make sure to delete this file after testing.


Step 5: Contact your Hosting/Cloud Support

    If nothing worked, better get in touch with your Hosting (say GoDaddy, Hostinger) or your Cloud Service Provider (say Amazon AWS, Microsoft Azure or Google Cloud Storage)

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright Β© Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap