Most content management systems (CMS), such as WordPress, operate in two parts: 1) a set of static files 2) a database that includes all of your posts/content/and settings. If your files are unable to connect to your database, you often will find yourself with this error.
Error establishing a database connection
This happens when your website is unable to connect to the database to pull your website's content, most often in applications such as WordPress. A number of factors can cause this issue, but the following steps resolve most:
1) Check our Network Status Page
Downtime is a rarity for us at NameHero, but it's always a possibility. The above link goes to a real-time view of our network that ties into our internal monitoring system to quickly identify issues. If you're seeing the above error on your website, give that page a look to make sure we're not having a degrade in service. If so, our team is already on it and will post updates until things are resolved. Historically we have such issues mitigated in under 30 minutes (trust me it's all hands on deck when a service goes out).
2) Check your website's configuration file (i.e. wp-config.php)
If you're using an application such as WordPress for your website, you should navigate to the directory where it is installed and open up the configuration file that connects it to the database (i.e. wp-config.php).
To do this, go to cPanel -> File Manager and then the directory of WordPress, most often public_html:
If you have trouble locating it, you can also use the search box in the top right to search all your folders for it.
Once located, right click and edit:
This will spawn another dialog, just click edit on it to open the file.
You want to inspect the MySQL settings that include the database name, user, and host which should look something like this:
First, make sure the mySQL hostname says localhost as above.
If that's correct, go back to cPanel in another window and click on mySQL databases:
This will bring you to a dashboard with all of your databases.
You want to make sure the database listed beside DB_NAME in your wp-config.php file exists.
In my above example my database is webloggy_wp59, so that's what I'm looking for:
In this example, the database does exist, so I confirm everything is spelled correctly, which it is.
If the database is spelled differently, you would want to modify the section beside DB_NAME in your wp-config.php file.
Since I've verified the hostname is localhost, the database is webloggy_wp59, and it's spelled correctly in my config file, I want to inspect the DB_USER.
In my wp-config.php file, this is webloggy_wp59:
Next, I want to compare this to the Privileged Users column next to my database in cPanel:
Bingo! I found the problem; the database user in my wp-config.php file doesn't match the one inside of cPanel!
Now if there wasn't a user in the Privileged User's column, I would want to scroll down the page where I could create a new one, and then add it to the database.
Therefore, I want to go back into my wp-config.php file and input the correct DB_USER like this:
Notice I entered this in between the quotation marks; I don't want to remove those.
Since the database user was incorrect, I have to assume it's also using the incorrect database password. Therefore I need to return to the mySQL dashboard in cPanel and reset it. If I scroll down the page from where my databases are listed, I'll see Current Users. Next to the correct user, I will click Change Password:
This will bring me to a page where I can add the correct password. While I could simply input the password from my wp-config.php file beside DB_PASSWORD, it's best security password just to reset it. Therefore I'll use the Password Generate to create me a strong one:
As noted by the red arrows, I need to first generate it and before confirming I've copied it down, I need to go back to my wp-config.php file and enter it there:
Just like the database name and user, I input the password in between the quotes. Removing those would create another error.
Once I've down this, I can go back to cPanel and click Use Password. This will bring me back to the password reset screen where I can officially change the password, but I want to be sure the strength is Very Strong (100/100):
Now, I can go back to my wp-config.php file and click Save Changes:
I then refresh my website and see the dreaded error is gone and my website is back online:
If our Status Page isn't reporting a degrade in service, you're certain your configuration file is setup properly, you maybe ready to curse, but don't give up yet, there is one more thing we should check.
3) Check mySQL PHP Extensions
This is one setting that is often overlooked in troubleshooting, but can save you a lot of time and frustration. Most often, the error for this looks a bit different than above, but here is what one looks like in WordPress:
There has been a critical error on your website
If you've recently changed PHP versions, made any PHP modifications, or moved from another web host, you should go into cPanel -> Select PHP Version and inspect the PHP Extensions that include mySQL:
The above screenshot points to mysqli, mysqlnd, and pdo_mysql, which are enabled.
If you look closely below the second arrow, you'll notice nd_mysqli and nd_pdo_mysql are unchecked.
Without going all technical, depending on your application/theme/plugin(s) used, along with PHP version, you may need to change these up a bit. mySQL is dependent upon these in order to connect properly to PHP.
To quickly solve the error, you may wish to change your PHP version which will use the defaults we've provided.
In the above screenshot, we're using 7.2 which is a bit outdated, so we can try 7.3 with our default settings. We then want to make sure mysqlnd and nd_mysqli are checked:
If you uncheck nd_mysqli you will see an error.
Likewise, if you're using PHP 7.3, you may want to change over to 7.2 and then check that your application/plugins/themes are all running their latest version and see if what PHP specific extensions they require.
Most often, database connection errors stem from incorrect credentials inside of your PHP configuration file (i.e. wp-config.php). Ensuring these are entered correctly as well as the appropriate permissions are set, will resolve this quickly. These are more common for those who have recently moved from another web host, restored a backup, or accidentally removed/modified the wp-config.php file.
Having incorrect PHP mySQL extensions enabled/disabled usually occur when you've changed to a new version of PHP or are using an outdated theme/plugin.