“Error establishing a database connection” in WordPress. Let’s fix it

By Tech-Assured     
January 20, 2021  2363 Views

Error-Establishing-a-Database-connection-in-WordPress-Let's-fix-it

Webmasters often see a notice – ‘Error establishing a database connection’ on WordPress website. It’s an error that makes your WordPress website inaccessible to the users.

When WordPress is unable to make a connection to the database, a number of things can affect your WordPress database connection.

Let’s see the causes and fixes to it…

The ‘Error establishing a database connection’ error can be caused by incorrect database information in your WordPress settings, database server down or corrupt database.

For a connecting to the database, WordPress requires:

  • Database name
  • Database username
  • Database password
  • Database server

Solution


1.  Check Your WordPress Database Credentials

Your WordPress database credentials are stored in the wp-config.php file.

You’ll be looking for the following lines in the wp-config.php file.

// ** MySQL settings – You can get this info from your web host ** //
/** The name of the database for WordPress */
define( ‘DB_NAME’, ‘database_name_here’ );
/** MySQL database username */
define( ‘DB_USER’, ‘username_here’ );
/** MySQL database password */
define( ‘DB_PASSWORD’, ‘password_here’ );
/** MySQL hostname */
define( ‘DB_HOST’, ‘localhost’ );

Note: Database name, username, password, and database host has to be correct.

If you are not sure, you may log in to your WordPress hosting account and click on MySQL databases under the database section.

This will take you to the database management page in your hosting dashboard. From here, you can find out your database name and the username.

You can also change the user password here.

After confirming your database name, username, and password, you may later change that information in your wp-config.php file.

Now try visiting your website again to check for error.

If you still see the error…

2. Check Your Database Host Information

WordPress hosting companies use localhost or separate servers to host databases.

In that case, you need to contact your WordPress hosting company to confirm your database host information.

3. Repair WordPress Database

On the wp-admin, if you get a different error…

For example:  “One or more database tables are unavailable. The database may need to be repaired”

In this case you need to repair your database by adding the following line in your wp-config.php file.

1      define(‘WP_ALLOW_REPAIR’, true);

4. Check if Your Database Server is Down

Once you make sure that everything is correct, and WordPress still cannot connect to the database, then your database server may be down.

Sometimes, due to heavy traffic on a server your host server cannot handle the load.

Now check whether other websites are running on the same server, So that you can figure out whether your SQL server is down or no.

If you do not have any other site on the same hosting account, then go to your hosting dashboard and try to access phpMyAdmin and connect the database.

If you can connect, then we need to verify if your database user has sufficient permission. Create a new file called testconnection.php and paste the following code in it:

<?php

$link = mysqli_connect(‘localhost’, ‘username’, ‘password’);

if (!$link) {

die(‘Could not connect: ‘ . mysqli_error());

}

echo ‘Connected successfully’;

mysqli_close($link);

?>

Now replace the username and password and upload this file to your website and access it via web browser.

If the script connected successfully, then it means that your user has sufficient permission, and something else is wrong.

Ask for help –
 Our Support Engineers will assist you with all sorts of database related issues.

Tech-Assured can help you deploy best IT practices and mitigate risks with a fully compliant IT framework.


Enlink Clients