Fix Error Establishing a Database Connection in WordPress

Recently i was trying to install WordPress on my friend’s localhost but an error occurred which said – “Error establishing a database connection in WordPress“.

I’ll not go in detail, lets just solve the error right away. In order to fix – Error establishing a database connection, you’ll need to follow two steps.

  1. Creating a new Database
  2. Creating Database User
  3. Editing wp-config file of your WordPress package.

Where to Find wp-config file?

If you plan to install WordPress on Local Host using WAMP server then wp-config file has to be inside www folder (C:wampwww). If you plan to install WordPress on Web Host then you can access wp-config file using FTP Client like FileZilla.

You need to know the right location of wp-config file in order to proceed. If at all you have any difficulty to find wp-config, just leave a comment below and i’ll surely help you out.

Step 1 – Creating Database

Step 2 – Creating Database User

  • If you are installing WordPress on Local Host then just go to step 3
  • If you are installing WordPress on Web Host then read THIS article.

Step 3 – Editing wp-config file

When you open wp-config file, you’ll see something as shown below.

[php]

// ** 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’, ‘root’);

/** MySQL database password */
define(‘DB_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’, ”);

[/php]

WP Local Host Installation

If you have created database named XYZ then make the following changes in your wp-config file –

[php]

// ** MySQL settings – You can get this info from your web host ** //
/** The name of the database for WordPress */
define(‘DB_NAME’, ‘XYZ’);

/** MySQL database username */
define(‘DB_USER’, ‘root’);

/** MySQL database password */
define(‘DB_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’, ”);

[/php]

WP Web Host Installation

If you have created database named ABC and created a user with username: admin and password: hello then just make the following changes in your wp-config file (which is on the server).

[php]

// ** MySQL settings – You can get this info from your web host ** //
/** The name of the database for WordPress */
define(‘DB_NAME’, ‘ABC’);

/** MySQL database username */
define(‘DB_USER’, ‘admin’);

/** MySQL database password */
define(‘DB_PASSWORD’, ‘hello’);

/** 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’, ”);

[/php]

Long Story Short

You need to create database and database user. The database user will have username and password. This error usually occurs because of mismatch in configuration. So to fix it, open wp-config file and enter the right database name, database username and database user password. Save the file where it is supposed to be. And problem of “Error establishing a database connection in WordPress” will be solved.

Thanks for reading and do subscribe to blog feed for more amazing posts. You can find the subscription box below.

Leave a Reply

Your email address will not be published. Required fields are marked *