How to Solve Error Establishing A Database Connection (WordPress) Part I
The above sentence is probably a nightmare for WordPress users and is one of the most common WordPress errors.
If you get this message, your website is no longer communicating with or having access to your database, which certainly causes your website down.
Most ordinary people panic when they get this message when they access their website. In fact, if you happen to get this message, you must move quickly in the process of handling because this issue can affect your traffic, sales, and analysis.
Maybe you are confused how to fix it? Maybe you do not understand why you got such a message? Do not worry! In this article, we will discuss some scenarios that usually cause this to happen and how to fix this issue.
But before we discuss how to solve it, we need to understand first what is the establishment of a database connection and why do we sometimes get that message when it comes to accessing a website, from both front-end and back-end?
What is an error establishing a database connection?
All information available on your WordPress site, such as post data, page data, meta information, plugin settings, login information, etc. stored in your MySQL Database. Some data that is not stored in the database is just media content such as pictures and themes or your plugins, as well as core files like index.php, wp-login.php, and so on.
When someone visits your website, PHP will execute code on the page and request data from the database, which will then be presented to the visitor's browser.
If PHP can not request data from the database, you will receive an "Error establishing a database connection" message. The whole page will be empty because there is no data obtained to show on the browser screen. Not only disrupt the front end of your website, it can also make you unable to access your WordPress dashboard.
When someone visits your website and gets the message, it will issue a 500 HTTP status code in your logs. This status code will appear when your website is experiencing "internal server error". This means there is something wrong on your server and the requested resource is not sent by the server. If all works well, your website will issue 200 HTTP status codes, which means everything is fine.
The cause of an error establishing a database connection
Before we discuss how to solve it, surely we need to first identify what causes the emergence of an error message establishing a database connection. Here are some of the most common causes found when someone receives this message:
The login credentials of the database are incorrect. Your WordPress website uses a separate login information to connect with MySQL Database.
The database is corrupt. With the ever-changing sections of themes, plugins, and users constantly removing and installing them, sometimes the database becomes corrupted. This can be caused by tables that are missing or corrupted individually, or maybe some information was accidentally deleted.
There is a corrupted file with WordPress installation. This problem can also occur due to hackers.
There is a problem with your database server. There are some things that are actually the fault of hosting, such as database overload from traffic being interrupted or the server becomes unresponsive because it gets too many requests. Such problems are common with websites that use shared hosts because they use multiple resources along with other users on the same server.
Before you start the troubleshooting process, we recommend that you backup. The ways that will be discussed below will manipulate the information in your database. Therefore, you should backup your site data before you fix your WordPress website. WordPress also offers backup plugins like UpdraftPlus or Duplicator to backup your files and databases.
If you want to backup files manually, you can backup files using phpMyAdmin in your cPanel. After you finish backing up your website, you just start the troubleshooting process of your database connection problem.
1. Check Database Login Credentials
The first thing you should check is that your database login credentials are correct. This is one of the most frequent reasons why an error message establishing a database connection appears on a site. Especially after someone migrates their website to a new hosting.
Your WordPress site connection details are stored in a file called wp-config.php, which is usually in the root of your WordPress site. This file contains four important information that all must be confirmed is correct for the connection to work properly.
To access the wp-config.php file, you can connect to your site via SFTP and browse the root of your site. Or if you use cPanel, you can click "File Manager", browse the root of your site, and right click to edit the file.
Select public_html on the left side of your screen then right click on wp-config.php file.
The wp-config.php file will look like below
You now need to check the current values against those on your server to make sure they are correct. You need to check your credentials database in cPanel. All you need to check first is the database name. To do this you need to login to phpMyAdmin in cPanel under the Databases section.
On the left side, you will see your database name at the very bottom. You do not need to ignore the database "information_schema" because this is something used by the host. You then want to see your server name with the name that is in DB_NAME in the wp-config.php file. If they are the same, then your website is fine. If different, you need to update your wp-config.php file.
You can also verify that this is the correct database by ensuring that the site contains the URL of your WordPress site. To do this, simply click on the database, then click on the wp_options table (this may be named something slightly different for security purposes, such as wpxx_options). At the top of the table, you'll see a value for the URL and name of your site. If this matches your current site, you can be sure that you are in the right place.
If your database name is correct and you still get an error to create a database connection message then you also want to check your username and password. To do this, you need to create a new PHP file in the root directory of your WordPress site, and input the following code. You can name it as you wish, such as checkdb.php. Just change the db_user and db_password values to those in your wp-config.php file.
Then browse to the file on your WordPress site: https://yourdomain.com/checkdb.php. If you get "MySQL Error: Access denied", you know your username or password is wrong and you need to proceed to the next step to reset your credentials.
The message you want to see is the following message - "Database connection is working properly." But of course, if you were not here. Be sure to delete this file after completion of the test.
So next you need to reset your username and password. In cPanel, click MySQL Database under the Database section.
Scroll down and create a new MySQL user. Try and choose a unique username and password so it is not easy to guess. The keyword generator tool they provide is really good. Then click "Create User." You can change the password on this screen to the current existing database user.
Then scroll down and add your new user to the database. The next screen will ask you what privileges you want to add. Select "All Privileges".
Then grab new credentials and update your wp-config.php file. You want to update DB_USER and DB_PASSWORD values. You can also run the test file again from earlier. This should then resolve your credentials issue. If not, you may still have the wrong hostname (DB_HOST). Some hosts use different values, see a list of some common DB host values. Usually this will only be localhost. But you can always contact your hosting provider or check their documentation if you are not sure. Some may also use 127.0.0.1 instead of localhost.
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
Now that is what call effort!
Excellent overview that was a joy to read. Cheers! :]
You can contact us on Discord.
[utopian-moderator]
Thanks for approved @cnts
Congratulations @abuzenk! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
You published 4 posts in one day
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP
Hey @abuzenk I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x