How to Fix Error Establishing a Database Connection in WordPress

How to fix Error establishing a database connection in WordPress

WordPress is a free and open-source CMS written in PHP and paired with a MySQL database. When a database error occurs in a WordPress site, you will see “Error establishing a database connection“. Two most probable reasons for a database connection error are database server down and incorrect wp-config.php. In this tutorial, I will describe how to fix error establishing a database connection in WordPress step by step.

Why Do I Get Error establishing a database connection?

Error Establishing a Database Connection
Error Establishing a Database Connection

If you recently transferred your WordPress site from a hosting company to another, you may face this problem. If you are transferring your site for the first time, you will face a database connection error if you cannot transfer your site properly. Another most probable reason is an irresponsive database server. Error establishing a database connection occurs in a WordPress site for the following reasons:

  • Incorrect wp-config.php settings
  • Incorrect database name, database username and password
  • Database server down
  • Corrupted database

How To Fix Database Connection Error in WordPress

As almost every problem has a solution, it also has. Here I will describe 4 effective ways to fix error establishing a database connection.

1. Check wp-config.php settings

As the site cannot establish a database connection, you should check the database credentials. As I previously mentioned, the database problem in WordPress most probably happens at the time of transferring the site to another hosting or another cPanel account. If you recently transferred your site, then follow the following instructions:

wp-config.php in cPanel
  • Login to your cPanel
  • Go to file manager
  • Then go to your site
  • And open wp-config.php (If the site is root domain, you will find wp-config.php in public_html or if the site is on addon domain, you will find in yoursite.com)
  • Finally check the credentials according to the following box. (DB_NAME, DB_USER, DB_PASSWORD and DB_HOST)
<?php
/**
 * The base configuration for WordPress
 *
 * The wp-config.php creation script uses this file during the
 * installation. You don't have to use the web site, you can
 * copy this file to "wp-config.php" and fill in the values.
 *
 * This file contains the following configurations:
 *
 * * MySQL settings
 * * Secret keys
 * * Database table prefix
 * * ABSPATH
 *
 * @link https://wordpress.org/support/article/editing-wp-config-php/
 *
 * @package WordPress
 */

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'Your Database Name' );

/** MySQL database username */
define( 'DB_USER', 'Your Database Username' );

/** MySQL database password */
define( 'DB_PASSWORD', 'Your Database Username Password' );

/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8mb4' );

/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );

/**#@+
 * Authentication Unique Keys and Salts.
 *
 * Change these to different unique phrases!
 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define( 'AUTH_KEY',         'Key' );
define( 'SECURE_AUTH_KEY',  'Key' );
define( 'LOGGED_IN_KEY',    'Key' );
define( 'NONCE_KEY',        'Key' );
define( 'AUTH_SALT',        'Key' );
define( 'SECURE_AUTH_SALT', 'Key' );
define( 'LOGGED_IN_SALT',   'Key' );
define( 'NONCE_SALT',       'Key' );

/**#@-*/

/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give each
 * a unique prefix. Only numbers, letters, and underscores please!
 */
$table_prefix = 'wpke_';

/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * For information on other constants that can be used for debugging,
 * visit the documentation.
 *
 * @link https://wordpress.org/support/article/debugging-in-wordpress/
 */
define( 'WP_DEBUG', false );

/* That's all, stop editing! Happy publishing. */

/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
	define( 'ABSPATH', __DIR__ . '/' );
}

/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';

2. Check MySQL database name, username and password

 MySQL® Databases

If you manually transfer or restore your site from a cPanel backup, you might make a mistake. So check MySQL database name, MySQL Username and Passwords. To check these info:

Current databases
  • Log in to your cPanel
  • Go to MySQL® Databases
  • Scroll down to Current Databases
  • Check the database name and Privileged username of the affected site

If everything is okay and you are still seeing “Error establishing a database connection” then follow the next instructions:

Change mysql database user passwords
  • Again log in to your cPanel
  • Go to MySQL® Databases
  • Scroll down to Current Users
  • Change the passwords of the database users and copy the passwords. (You may see more than one user, just change the passwords of current user)
  • Finally paste the passwords in “Your Database Username Password” in your wp-config.php. (See the following box)
/** MySQL database password */
define( 'DB_PASSWORD', 'Paste Your Database Username Password Here' );

3. Check the database server is down or not

Sometimes the error occurs for database server down. Make sure that the database server is not down. You can easily check whether the database server of your cPanel is down or not.

Was this article really helpful?
YesNo

LEAVE A REPLY

Please enter your comment!
Please enter your name here