How to Fix “error establishing a database connection” in WordPress

How to Fix “error establishing a database connection” in WordPress

Fixing Pending Comments Not Showing (invisible) in WordPress

WordPress is a handy tool for blogging. Using WordPress you can create & manage websites with zero coding knowledge. While you work on WordPress, you might see Errors due to misconfigurations. Although there is a long list of WordPress related errors that webmasters face, this particular article is about “Error Establishing a Database Connection” Problem.

This is a common error that people encounter usually after moving their website from one host to another. In some cases, you will see this error if you messed up with WordPress files. No matter how this error appeared on your Website, I am providing you with a 100%  Working solution. Read this Article carefully and follow the given steps to Fix “error establishing a database connection” Issue. Let’s Get Started.

Fixing “Error Establishing a Database Connection” Problem in WordPress

What is this Error About?

After reading this error message you will surely understand that there is some issue with Database connection.  Let me break it down for you. Whatever files you work with, Be it WordPress files, images you upload, Posts you write, all are saved in your Web Hosting. Matter of Fact, WordPress gets installed on your Web Hosting only.

All Details of your Work i.e. Articles, Comments, Plugins Data, etc. are stored in a Database. Whenever any detail is required, the information is fetched from the database. Whenever WordPress & Database connection breaks, you will encounter “Error Establishing a Database Connection” Error.

To Fix this Problem, we need to ensure that every piece of information present in the WordPress configuration file (wp-config.php) matches with the actual configuration. If there is a slight mistake, this error will pop up.

Common Scenarios that lead to this Error in WordPress:

You moved your Website/Blog from One Hosting to Another using FTP but Didn’t move your Database: Suppose, I am hosting a Website on Godaddy.com and now I want to move my website from Godaddy to Hostgator. To do this, I purchased Hosting from Hostgator. Now, to move the files I need to backup all the data from Godaddy to my Computer or any other storage. Then using FTP, I need to upload the files to the new Hostgator Server. Also, I need to Change the nameservers. Most of the people, after performing these to steps try to open up their Websites and Face the Database Connection Error.

That’s natural because you missed a very important step i.e. moving your Database. While moving a website, moving WordPress Files is not enough, you need to move your Database too. For that, you need to backup your Database from your Old Hosting and restore it on your New hosting.

This process is tricky because before restoring the database, you need to Create a New Database on your new hosting with the same old name. And then, you need to Create User (with the same name that was in old hosting) and specify the same password. Finally, you need to give all the privileges to the newly created user for the new database. Once you do that, import tables from the old database.

Note: Providing the same information (Database name, username, password, table-prefix) as per old database is not compulsory. If you provided other names, make sure you enter those in the wp-config.php file.

The database is not moved Properly: In some cases, you will see this error if the database migration didn’t take place properly. Later in this Article, I have explained the process of backing up and restoring the database, Create User, etc. in detail. Do not miss that.

You Mistakenly Deleted or Edited wp-config.php: If you mistakenly Deleted a wp-config.php file or edited it, in that case also you will encounter this issue. To fix the problem in this case, you need to specify the correct information in the wp-config.php file.

The above-mentioned three scenarios are most common. 90% of cases where you see “Error Establishing Database Connection” Message appear due to one of these two cases (Usually it’s the First two cases)

Let me now tell you, how can you Fix this Issue within few minutes let’s get Started.

Error Establishing a Database Connection – How to Fix this Error?

Download the copy of your wp-config.php file and open it using an editor (Preferably Notepad++). Go to MySql Settings section and get the “DB_Name”, “DB_User”, “DB_Password” details. Also, scroll down a little to find out what the “table prefix” is. Here is a sample wp-config.php file for you:


<?php
/**
* The base configurations of the WordPress.
*
* This file has the following configurations: MySQL settings, Table Prefix,
* Secret Keys, WordPress Language, and ABSPATH. You can find more information
* by visiting {@link http://codex.wordpress.org/Editing_wp-config.php Editing
* wp-config.php} Codex page. You can get the MySQL settings from your web host.
*
* This file is used by the wp-config.php creation script during the
* installation. You don't have to use the web site, you can just copy this file
* to "wp-config.php" and fill in the values.
*
* @package WordPress
*/

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

/** MySQL database username */
define('DB_USER', 'Database_Username');

/** MySQL database password */
define('DB_PASSWORD', 'Database_Password');

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

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

/** 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}
*/
require('wp-salt.php');
/**
* 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!
*/

/**
* 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 = 'k3mY_';

/**
* WordPress Localized Language, defaults to English.
*
* Change this to localize WordPress. A corresponding MO file for the chosen
* language must be installed to wp-content/languages. For example, install
* de_DE.mo to wp-content/languages and set WPLANG to 'de_DE' to enable German
* language support.
*/

define('FS_METHOD','direct');
define('WPLANG', '');
define('FS_CHMOD_DIR', (0775 & ~ umask()));
define('FS_CHMOD_FILE', (0664 & ~ umask()));
/**
* 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.
*/
define('WP_DEBUG', false);

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

/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');

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

If we take a look at this file above, we get the following information :

Database Name is: ‘databasename’

Database User is: ‘Database_User’ 

Database Password is: ‘Database_Password’

Database table prefix is:’ k3mY_’

Now, we need to verify this by logging into the Cpanel. 

  • Once you are in the cpanel dashboard, go to “phpmyadmin” section.

error establishing a database connection

  • There, you need to ensure that a database ‘databasename” exists. If it exists, that’s good. If not, you need to create a new database with the name ‘databasename’.

If exists, Click on ‘databasename’ database and see if all tables name starts with prefix ‘k3mY_’ or not.

Now, you need to ensure that Database user ‘Database_User’ exists. To verify this, go to “MySQL® Databases” in Cpanel. In the current Databases Section, Make Sure ‘Database_User’ user exists on Database, ‘databasename’

If all these Details exist, you should Open up your website it should work fine now. If Still, you see this Problem, change the Database password to ‘Database Password’. 

If you don’t find details in the phpmyadmin section. you can do two things. Either you can create Database name, username, prefix as per the wp-config file or you can create these as per your choice and make changes to the wp-config.php file.

Creating New Database, Adding User, Specifying Password and Giving Privileges

  • Login to your Cpanel Account.
  • Go to MySQL Database Wizard

  • Type in the Database Name
  • Next Specify Database Username and Password

  • Click on “Create User” to proceed
  • In the Next Step, you need to Add the User to the Database. Select “All Privileges” & Click on “Next Step”

To make sure everything works fine, you need to backup your database from old hosting and restore it on your new hosting.

Backing up Database on Old Hosting

  • Login to the Cpanel of your Old Hosting and Go to phpmyadmin & Click on Database Name
  • Now, Go to the “Export” Section to Backup the files

  • Click on “Go” to Download the Backup file

Restoring the Database files on New Hosting

  • Log in to your New Hosting’s Cpanel, Go to phpmyadmin and Click on Database Name
  • Now, Go to “Import” Section

  • Now, Choose the backup file that you downloaded and Click on “Go”
  • Now, all old data will be shifted from the old database to the new one

Double Check all details on your New Hosting’s wp-config.php file. Make sure all details are correct. Now open up your Website, it should open up now. As you can see, proper configuration and specifying correct information in the wp-config.php file will lead to the resolution of this error.

That’s it. Now you will no longer stumble upon the “error establishing a database connection” Issue. I am pretty sure that this should fix your Problem. However, if you are still facing any problem, do let me know in the comments section. I will try to answer queries to the best of my knowledge.

COMMENTS

WORDPRESS: 0
DISQUS: 0