Database connection error - how to fix it (cPanel)

"Error establishing a database connection", "Access denied for user", "Unknown database" mean the site cannot log in to the database with the details stored in its settings.

Where the connection details are

CMSFile and settings
WordPresswp-config.php: DB_NAME, DB_USER, DB_PASSWORD, DB_HOST
Joomlaconfiguration.php: $db, $user, $password, $host
OpenCartconfig.php and admin/config.php: DB_DATABASE, DB_USERNAME, DB_PASSWORD
Laravel and others.env: DB_DATABASE, DB_USERNAME, DB_PASSWORD

Check in order

  1. The database and user names are full, with the account prefix (example_shop). Compare with "Manage My Databases".
  2. The user is added to the database. In "Current Databases" the user must be listed under "Privileged Users".
  3. The password. If unsure, set a new password for the user ("Change Password" in the user list) and put it in the settings file.
  4. The host is localhost.
Database and its user
Database and its user

If the database has no user, add one with "ALL PRIVILEGES": databases in cPanel, steps 7-10.

After moving a site from another host, the database and user names are almost always different - change them in the settings file. If something does not work, contact support from your client area (Tickets).

Questions and answers
That the database and user names are full, with the prefix, and that the user is added to the database with privileges.
On the new hosting the database name, user and password are different. Change them in the site settings file.