The “Error establishing a database connection” message means the website can't access its database. The website's files are in place, but the connection details don't match: the database name, the user, the password or the server. It often appears after a move, after a partial restore or after someone changed the database user's password.
What you'll achieve
- compare the details in the website's settings with the database in Enhance;
- restore the connection, with a new password for the user;
- know what to do if the database is missing or empty.
Before you start
- Log in to Enhance at https://server.nbt-host.com, then “Websites” » your website.
- The steps are written for WordPress, where the connection details are in
wp-config.php. In other applications the file has a different name (for exampleconfiguration.phpor.env), but the idea is the same.
The steps
1. Read the details in wp-config.php
- The “Files” tab »
public_html, right-clickwp-config.php» “Edit”. - Write down these four lines:
define( 'DB_NAME', 'numele_bazei' );
define( 'DB_USER', 'utilizatorul' );
define( 'DB_PASSWORD', 'parola' );
define( 'DB_HOST', 'localhost' );- Close the file without saving anything, for now.
2. Compare with what's in Enhance
- Open the website's “Databases” tab.
- Check that:
- the database named in
DB_NAMEis in the list; - the user in
DB_USERexists on the “Users” tab, next to “Databases”, and has access to that database; - the server in
DB_HOSTislocalhost.
- the database named in
- Database and user names have a prefix added by Enhance, for example
site_abc1_magazin. Inwp-config.phpthey must be written in full, including the prefix. You can see the exact details on the database page, under “Connection info” (see Creating a MySQL database and user).
3. Set a new password for the user and enter it in wp-config.php
No one can see the old password, not even us. The safest option is to set a new one, in both places:
- In the “Users” tab, next to “Databases”, open the database user, set a new password (the key button generates a long one) and copy it.
- Back in “Files”, open
wp-config.phpwith “Edit” and enter the new password inDB_PASSWORD, between the single quotes. - If needed, also correct
DB_NAME,DB_USERandDB_HOST. - Save and reload the website.
If the user no longer exists (for example, after restoring only some databases), create it again with “Add user”, tick the database under “Database access” and enter its details in wp-config.php.
4. The database is missing or empty
- The database isn't in the list. If you moved the website, create it and import the
.sqlfile from your old hosting with “Import SQL file” (see Creating a MySQL database and user). If it was deleted by mistake, restore it from the “Backups” tab (see Backup and restore from Enhance). - The database is there, but the website asks you to install WordPress from scratch. The database is empty or the tables have a different prefix than
$table_prefixinwp-config.php(usuallywp_). Check the table prefix in the.sqlfile and set the same prefix inwp-config.php.
If something doesn't work
- The error appeared after a restore. If you restored only some databases, their users are not recreated. Restore all the databases or recreate the user, as in step 3.
- The error comes and goes. The details are correct, but the database server or the website is under heavy load. Send us the time when it appears.
- The WordPress dashboard says the database needs to be repaired. First make a copy (the “Backups” tab » “Create backup”), then write to us: we'll check the tables.
- I don't want to touch wp-config.php. Send us the domain and we'll do the check for you.
