MySQL connection details: host, database name, user and password

Print
  • 1

For a site hosted with TopHost the database host is localhost. The database name and the user name both start with your cPanel username followed by an underscore, and the password is the one you chose for the database user. The port is the standard 3306 and normally does not need to be entered.

The four values at a glance

ValueWhat to enterWhere to find it
Host (hostname, server)localhostAlways the same for a site on the same hosting account
Database nameFor example cpuser_shopcPanel, Manage My Databases, Current Databases
User (username)For example cpuser_shopadminSame page, Current Users
PasswordThe password of that database userNot displayed by cPanel; you can only set a new one

These are not the login details of cPanel or of the client area. A database user is a separate account that exists only for MySQL. If you do not have a database yet, create one as shown in creating a MySQL database in cPanel.

Checking names and resetting the password

  1. In cPanel, open Databases, then Manage My Databases.
  2. Under Current Databases each database is listed with its Privileged Users. If your user is not shown next to the database, it has no access yet: add it under Add User To Database.
  3. To set a new password, go to Current Users and click Change Password next to the user. Then update the password in your site's configuration file straight away, or the site will lose its connection.

Where common platforms store these values

PlatformFileSettings
WordPresswp-config.phpDB_NAME, DB_USER, DB_PASSWORD, DB_HOST
Joomlaconfiguration.php$db, $user, $password, $host
OpenCartconfig.php and admin/config.php (both)DB_DATABASE, DB_USERNAME, DB_PASSWORD, DB_HOSTNAME
PrestaShop 1.7 and newerapp/config/parameters.phpdatabase_name, database_user, database_password, database_host

Open the file with File Manager in cPanel (right-click, Edit) and save a copy before you change anything.

localhost or 127.0.0.1?

Both point to the same server. localhost connects through a local socket and is the usual choice. Use 127.0.0.1 only if an application specifically asks for an IP address.

Connecting from another computer

localhost works only for scripts that run on the hosting itself. A program on your computer or on another server needs the server's address and an allowed IP. The steps are in remote MySQL connection.

"Error establishing a database connection": what to check

  1. The database name and user include the prefix and are spelled exactly as in cPanel.
  2. The password in the file matches. When in doubt, set a new one in cPanel and copy it into the file.
  3. The user is listed as a Privileged User of the database, with all privileges.
  4. The host is localhost, not the old server's address left over from a move.
  5. The database still exists and is not empty (for example after a failed import).

If everything matches and the error remains, open a ticket with the domain and the exact error text.

Related articles

Was this answer helpful?