How To Fix Error Establishing A Database Connection In WordPress

Have you encountered an Error establishing a database connection in WordPress? Here are some solutions to fix this common WordPress error.

Failing to connect to the database in WordPress

The main cause of this is due to your database not being set correctly. To fix this, please follow the steps below.

  1. Start an FTP connection to your website’s server;
  2. Find your WordPress file directory, commonly under public_html;
  3. Download the wp-config.php file;
  4. Open this in a code-editor and make sure that the below parameters correlate with your database;
  5. Once updated, save the file and upload.
// ** 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_user');

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

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

Note that your DB_HOST (database host) is normally set to localhost as a default if your hosting is on the same server as your database.

Fixing a permissions error

If your database is connecting correctly and you still don’t know why you’re getting this error, it could be down to the database user’s permissions.

To fix this, make sure you grant the relevant permissions for your_database_user to access your_database_name through your hosting provider.

Check your database prefix

To check your database prefix, you should view your database tables and see how each of them start. The WordPress default installation sets wp_ as the database prefix.

To change this, it’s located in the wp-confix.php file:

$table_prefix = 'wp_';