Development and databases

Fix the database connection error in WordPress

Reviewed on 7 min read wordpress mysql errores migraciones

The message Error establishing a database connection means WordPress cannot read its MySQL database. It is common after migrating the site, restoring a backup or changing the database user.

Do not reinstall WordPress or import the database over and over before identifying the cause. The error is usually in the configuration, the permissions or the state of MySQL, and overwriting data can make recovery harder.

Check whether the failure only affects WordPress

Before editing wp-config.php, try to open phpMyAdmin from cPanel:

  • If phpMyAdmin opens and shows the databases, that MySQL access works. You still need to check the user, host and permissions WordPress uses, which may be different.
  • If you cannot get in either, or several applications show errors, there may be a general incident or a problem with the MySQL account.
  • If the database appears but does not contain the WordPress tables, a different database was imported or the restore did not finish correctly.

See how to use phpMyAdmin to check the tables without changing their content.

Check the details in wp-config.php

Open wp-config.php in the WordPress directory and find:

define('DB_NAME', 'usuario_basedatos');
define('DB_USER', 'usuario_mysql');
define('DB_PASSWORD', 'contraseña_mysql');
define('DB_HOST', 'localhost');

Check each value:

Constant What it should contain
DB_NAME Full name of the destination database
DB_USER MySQL user assigned to that database
DB_PASSWORD Current password of that MySQL user
DB_HOST Server given by the provider; in cPanel it is usually localhost

In cPanel, the account name is normally added as a prefix to databases and users. If you created wordpress and usuariodb, the real names may be micuenta_wordpress and micuenta_usuariodb.

Do not confuse the cPanel, WordPress or email password with the MySQL user's password. They are separate credentials.

Assign the user to the database

Creating a database and creating a user are two separate actions. You also have to link them and assign privileges.

  1. In cPanel, go to Manage My Databases or use the MySQL Database Wizard.
  2. Check that the user given in DB_USER exists.
  3. Add it to the database given in DB_NAME.
  4. Assign the privileges the application needs; an ordinary WordPress installation uses all privileges on its own database.
  5. Save and test the website again.

This step is a common cause after a manual migration: the files and tables have been copied, but the new user has no access to the restored database.

Check that you imported the right database

From phpMyAdmin, open the configured database and check that it contains tables such as:

  • wp_options
  • wp_posts
  • wp_users
  • wp_postmeta

The prefix may be different from wp_. WordPress defines it in wp-config.php with $table_prefix:

$table_prefix = 'wp_';

If the tables use another prefix, set that variable to the existing value. Changing the table prefix without a prepared migration can leave plugins or custom queries pointing to the wrong names.

Check the MySQL user's password

If you cannot confirm the current password, first identify every application that uses that user. Then set a new one from cPanel and update their configurations, including DB_PASSWORD, in a coordinated way.

Coordinate the change: from the moment you change the password in cPanel until you save wp-config.php, WordPress will not be able to connect. Do not post the password in a ticket or reuse it for other services.

Connection errors versus damaged tables

A database with damaged tables may produce different messages or partial errors inside WordPress. If WordPress manages to connect but reports that a table needs repairing, make a copy of the database first.

Only to run the built-in repair tool, you can temporarily add:

define('WP_ALLOW_REPAIR', true);

Temporarily restrict access to that URL, open /wp-admin/maint/repair.php and run only the repair that matches the engine and the diagnosis. This tool does not repair every InnoDB problem. Remove the line from wp-config.php when you finish, because that page does not require logging in while the option is on.

Do not use the repair when the real problem is a wrong name, user, password or host: it will not be able to connect and will not fix anything.

If the error appeared after migrating WordPress

Also check:

  • That the copied wp-config.php file does not still have the previous provider's MySQL details.
  • That the SQL export finished without errors and the import was not left incomplete.
  • That the MySQL or MariaDB version is compatible with the installation.
  • That there are not two copies of wp-config.php and you are editing a different directory from the one serving the domain.
  • That the domain points to the server you are checking.

Follow the guide to migrating WordPress to another hosting to check files, database, URLs and DNS as separate parts.

What information to send to support

If the error continues, include:

  • The domain and roughly when it started.
  • Whether it appeared after a migration, restore or password change.
  • Whether phpMyAdmin shows the database and its tables.
  • The database name and MySQL user, without passwords.
  • A screenshot of the message and any related error from the PHP log.

Do not paste the full content of wp-config.php: it contains the database password and WordPress security keys.

If the log shows connection limits or a service outage, do not change the password by trial and error. Note the error and the time and check the status with support. Once the cause is fixed, check both reading and writing, not just that the message disappears from the home page.

You may also find useful

Something wrong or out of date? Tell us and we will review it.