LochStudios  /  Help Centre  /  WordPress  /  Fix "Error Establishing a Database Connection" in WordPress

Fix "Error Establishing a Database Connection" in WordPress

Match wp-config.php to the full prefixed MariaDB name, user, password, and localhost on this hosting account, then ask us if the database is missing.

Updated

WordPress stores posts, pages, users, and settings in a MariaDB database on this account. The "Error establishing a database connection" page means PHP loaded, but WordPress could not sign in to that database. The site files are usually still there. The four values in wp-config.php (name, user, password, host) do not match what this account has, the user is not attached to the database, or the database itself is gone.

This article is for Beginner and Standard shared hosting (cPanel) in Australia. Open cPanel from the portal. Do not guess a hostname.

On a VPS you manage MariaDB yourself. Credentials and the console are on that server in the portal. See the VPS section below.

On a dedicated server, open a support ticket and we will walk you through access.

A new site should be installed with WordPress Toolkit or Installatron, not a zip from wordpress.org. Those tools create the database and write wp-config.php for you. See Install WordPress.

The site is unreachable in a different way (browser error, DNS, or a blank page)? Start with My website is down - what to check first or Fix the WordPress White Screen of Death / HTTP 500.

A step is unclear? Open a support ticket and we will help.

Open cPanel

  1. Sign in at the portal.
  2. Open the hosting service for the site.
  3. Click Log in to cPanel.

You do not need a server hostname to get in.

What usually causes it

On this hosting, the connection almost always fails for one of these reasons:

  • DB_NAME or DB_USER is the short name you typed, not the full name with the account prefix (username_shop, not shop).
  • The password in wp-config.php is not the database user password (it is not your portal password).
  • DB_HOST is the website domain, an old host name, or an IP. Sites on this account must use localhost.
  • The user exists but was never added to the database, or privileges were removed.
  • The database was deleted, or wp-config.php still points at a database from a previous host after a move.
  • wp-config.php was overwritten by a second install into the same folder.

cPanel still labels the tools MySQL. The engine is MariaDB. Every Beginner and Standard plan includes unlimited MariaDB databases. The site on this account connects with host localhost and port 3306.

Do not put the website domain in the host field. That sends the connection away from this account.

Check WordPress Toolkit or Installatron first

If Toolkit or Installatron already lists this install, open that tool before you edit files. It created the database and still knows the name.

WordPress Toolkit

  1. In cPanel, open WordPress Toolkit.
  2. Find the site.
  3. Note the database name it shows.
  4. Compare that name with MySQL Databases (steps below).

Installatron

  1. In cPanel, open Installatron.
  2. Open My Applications and choose the WordPress install.
  3. Note the database name stored for that application.

If Toolkit or Installatron does not list the site, use the file and MySQL Databases steps. You do not have to reinstall WordPress to fix a connection error.

Confirm the database still exists

  1. In the Databases section, open MySQL Databases.
  2. Under Current Databases, look for the full name (account username, then an underscore, then the short name).
  3. Under Current Users, confirm the database user is still there.

If the database is in the list, leave it. Do not create a second empty database "to be safe." An empty database will not bring the posts back.

If the database is missing, stop. Do not run a fresh WordPress install into the live folder. Hourly copies we keep include the databases on this account. Open a support ticket and ask us to restore that database. Tell us the domain, the full database name if you still have it (from wp-config.php), and the date you need. You can also export or restore a copy you already downloaded: Download and Restore a Backup.

Need a new database for a copy or a new app? Create a MySQL database and user. Use the full prefixed names in wp-config.php.

Read wp-config.php

The four defines live in wp-config.php in the site's document root, not inside wp-admin or wp-content.

  1. Open the File Manager.
  2. Go to the document root (public_html for the main domain, or the addon / subdomain folder).
  3. Select wp-config.php and click Edit (or Code Editor).
  4. If cPanel asks for a character encoding, leave utf-8.
  5. Find these lines:
define( 'DB_NAME', 'username_dbname' );
define( 'DB_USER', 'username_dbuser' );
define( 'DB_PASSWORD', 'the-password-you-set' );
define( 'DB_HOST', 'localhost' );

For a large download or a copy on your computer, use FTP. Edit in File Manager when you only need this one file.

Match the four values

Use the full names from MySQL Databases, not the short names you typed when you created them.

DefineWhat it must be
DB_NAMEFull database name, including the account prefix
DB_USERFull database username, including the account prefix
DB_PASSWORDThe password for that database user
DB_HOSTlocalhost
  1. On MySQL Databases, copy the full database name from Current Databases.
  2. Copy the full username from Current Users.
  3. Put those strings into DB_NAME and DB_USER. Keep the quotes around each value.
  4. Set DB_HOST to localhost. Remove a domain, 127.0.0.1, or any leftover host from a previous provider.
  5. If you are not sure the password is right, set a new one on MySQL Databases for that user, copy it, and paste it into DB_PASSWORD in the same save.
  6. Click Save Changes.
  7. Reload the site in a private window.

A second user can be attached to the same database. The user in wp-config.php is the one that must match.

Need a tool on your computer, or an app on another server, to reach this database? That is remote access, and localhost will not work for that tool. The WordPress site on this account should still use localhost. See Allow Remote MySQL Access.

Attach the user and grant privileges

A user that exists but is not attached cannot open the database.

  1. On MySQL Databases, scroll to Add User To Database.
  2. Choose the user from DB_USER.
  3. Choose the database from DB_NAME.
  4. Click Add.
  5. Tick ALL PRIVILEGES.
  6. Click Make Changes.

If the user is already listed next to the database, open the privileges for that pair and confirm ALL PRIVILEGES is ticked. WordPress needs at least SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX, and DROP.

Confirm the database opens in phpMyAdmin

If phpMyAdmin can open the database, MariaDB on this account is up and the database exists. The remaining problem is almost always wp-config.php.

  1. Open phpMyAdmin from the Databases section, or use the phpMyAdmin link next to the database on MySQL Databases.
  2. In the left sidebar, click the full database name.
  3. You should see WordPress tables (often prefixed wp_: wp_options, wp_posts, wp_users).

No tables: this is an empty database, or you opened the wrong name. Do not import into it until you are sure. If the live tables are gone, open a support ticket and we will restore from the hourly copies.

phpMyAdmin signs you in through cPanel. There is no separate phpMyAdmin password. A failed WordPress connection does not mean phpMyAdmin is broken.

Do not delete wp-config.php

Do not remove wp-config.php to "run setup again." Visiting the site without that file starts the WordPress installer. Completing it against a new database gives you an empty site. Completing it against the existing database does not fix a bad password, and it can confuse a Toolkit or Installatron listing.

If the file is damaged or empty:

  1. Look in File Manager Trash in case you deleted it.
  2. If it is gone, open a support ticket and ask us to restore that file from the hourly copies. Tell us the domain and the path (public_html/wp-config.php, or the addon / subdomain path).
  3. You can also restore a copy you already downloaded. See Download and Restore a Backup.

If you just changed a password

Changing the database user password on MySQL Databases does not update wp-config.php. Change the define in the same session, save, then reload the site.

That password is not the WordPress admin login, and it is not your portal password, unless you set them the same. After a password change, update every app on this account that uses that user.

Disk and resource checks

If the four values are already correct and phpMyAdmin opens the tables, check that the account is not full. A full disk can stop writes and make the site fail in odd ways.

  1. Check disk usage.
  2. Open Resource Usage / Metrics. See Read your resource usage and metrics.

If a graph is against the plan limit, open a support ticket and we will look at the plan with you.

WordPress on a VPS

On a VPS the database is the MariaDB (or MySQL) instance you installed. Credentials and the console are on that server in the portal.

  1. Open wp-config.php on the VPS and confirm DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST.
  2. If MariaDB runs on the same VPS as the site, DB_HOST is usually localhost.
  3. Sign in with the console or SSH and confirm the database service is running and that user can connect.

Installing or securing the engine: Install MySQL or MariaDB and Secure It. Watching load: Monitor Server Resources.

Open a support ticket if you want us to walk through the service with you.

If the site still cannot connect

Work through this list once:

  1. Full prefixed DB_NAME and DB_USER, correct DB_PASSWORD, DB_HOST is localhost.
  2. The user is attached to that database with ALL PRIVILEGES.
  3. phpMyAdmin opens the same database and shows the WordPress tables.
  4. You saved wp-config.php in the document root of this site, not a copy in another folder.
  5. You reloaded in a private window.

If those are all true, open a support ticket. Include the domain and the full database name and username (not the password). We will look at the account with you.

Do not wait on an email thread.

Related


Was this article helpful?

← Back to WordPress