Among the common issues in WordPress, getting an error that says “Error Establishing a Database Connection” is one of the most annoying.
If you’re dealing with this “Error Establishing a Database Connection” problem, we know it’s crucial to fix it quickly so your website can go back to normal. We’re here to help and guide you through the process with a smile.
Table of Contents
Why are you getting Error Establishing a Database Connection?
When you see an “Error Establishing a Database Connection” message, it means your website can’t get information from its storage called MySQL.
This makes your whole website go offline, and even the wp-admin part, where you control things, stops working.
There are a few reasons why you might see this error. But first, let’s talk a bit about how WordPress works. It has two important parts: one that deals with files and another with a database. Together, they make your WordPress site do its job.
Files
WordPress keeps all its important code, like the brain of the website, in a place called the file system. This includes things like themes, plugins, pictures you upload, and some settings.
You can look at these files using a web browser to go into cPanel or an FTP tool. It’s like opening a box to see what’s inside your website.
Database
WordPress keeps all the special things about your website in a place called the database. This includes posts, pages, comments, and settings for themes and plugins.
To look at this special data, you need tools like PHPMyAdmin, downloading a special file called SQL dump, or using a tool called the command-line MySQL client through SSH.
When you see a “Database Connection” error, it means something is stopping WordPress from talking to this special place. Since it can’t get the needed information from the database, it can’t show the page you want in your browser. It’s like trying to read a book, but the librarian can’t find it for you.
Why do I see the Database Connection error only on some of the pages or wp-admin?
If you set up a caching tool, you might see that some parts of your website still show up, even if there’s a problem connecting to the database.
This is because the front part of your website, what visitors see, is like having photocopies of pages. Even if the part where you control things (wp-admin) doesn’t work, the front still shows up.
Caching turns pages into copies that are ready to show. When someone asks for a page that’s already copied, it shows that copy without needing the special connection to the database.
Caching is like having ready-to-go copies of your favorite book. It makes your website work better and uses less computer power. So, it’s like a superhero helping your website run smoothly.
Top Causes of the “Error Establishing a Database Connection”
1. Incorrect MySQL Details
- WordPress needs specific information to connect to its database. This information, like the database name, username, and password, is stored in a file called wp-config.php in the WordPress folder on your computer.
- It’s important to be careful with the information in this file because it’s sensitive to uppercase and lowercase letters. Make sure it exactly matches the details given by your hosting provider for the MySQL database.
- Also, the prefix for WordPress tables (like wp_posts) should be the same in both WordPress and the MySQL database.
- Lastly, the user for MySQL must be linked to the database and have the right permissions to work correctly.
2. Corrupted MySQL Database
- If certain parts of the information in MySQL are damaged, WordPress might display an error because it can’t get important details from MySQL.
- The usual reason for this problem is when an update for the main WordPress system, the theme, or a plugin doesn’t work correctly.
- The table called wp_options is often the one that gets damaged the most.
3. The MySQL Host is Unavailable
If you see a connection error, but it fixes itself without doing anything, it could mean that the main MySQL server at the back end was just offline for a short time.
4. The Site Has Reached the MySQL Connection Limit
There is a maximum number of connections allowed for each user and for the entire server in MySQL.
5. High Traffic Levels
Too many people visiting a website, especially if it’s not set up well, can use up all the resources. When the resources are all used up, the website might show occasional errors.
6. Corrupted WordPress files
- Even though the problem is about the Database, WordPress might be damaged in the file system.
- The usual reason for WordPress issues is when an update for the main WordPress system fails or when a Theme and/or Plugin updates automatically.
How To Fix “Error Establishing a Database Connection”
Before going into detailed problem-solving steps, the first thing to do is contact the web server provider to make sure the issue is not on their side..
1. Contact Your Web Server Provider
The support team from your provider can tell you if there have been any recent problems with your service. When you talk to them, ask if the MySQL service is working okay and if there are any unusual increases in resource usage or visitor traffic on your account.
Once you confirm that your hosting account and webserver are fine, you can then proceed with detailed problem-solving or get more help from your provider if needed.
2. Double-Check the MySQL Information
Checking the wp-config.php is the first thing to do because having the wrong MySQL details is a common reason for this issue.
The wp-config.php file in your WordPress folder has all the settings WordPress needs to connect to the MySQL database. This includes the database name, username, password, table prefix, and other options.
If the credentials in MySQL don’t match those in wp-config.php, there will be a connection error.
To check this, go to the cPanel File Manager or your FTP client and find the wp-config.php file in the WordPress folder. Open it in an editor.
Look for the values for DATABASE_NAME, DB_USER & DB_PASSWORD. We need these to compare in cPanel.
Once you have the wp-config.php file open and found the values, go to the MySQL Databases page in cPanel. You’ll see a list of Databases and Database Users.
Make sure the database name and user in wp-config.php are the same as those listed on the cPanel MySQL Databases page.
We can’t see the existing password, but we can change it to match the one in wp-config.php. You can do this by clicking “Change Password” next to the Database User on the MySQL Databases page in cPanel.
Changing the password ensures that the password in wp-config.php matches the one in MySQL’s backend for that user.
3. Repair the MySQL Database
Now that we know the MySQL details in wp-config.php are right, we can try fixing the actual database.
Inside the WordPress backend MySQL database, there are many tables with important site setup data. Some of these tables, like wp_options, are really important. If these tables are damaged, the site won’t work.
To check and fix these tables, we can use the MySQL Repair tool in cPanel.
- Go to the MySQL Databases page in cPanel.
- Find the database mentioned in the wp-config.php for WordPress in the drop-down box labeled “Repair Database.” You can locate this under the “Modify Databases” section.
- Choose the Database you want to fix.
- Click the “Repair Database” button to begin the process.
The fixing process happens one table at a time. If your database is big, it might take a few minutes to complete the MySQL repair.
The fixing won’t hurt the “good” tables because the tool only tries to fix the ones that have issues.
4. Replace the WordPress Core files
The files that WordPress needs to talk to the MySQL database might be gone or not working right.
Usually, this happens when an update for the main WordPress files doesn’t finish properly. The good news is we can update these files without losing your website data or customizations.
WP CLI
The simplest way to do this is by using the WP CLI toolkit through SSH or the cPanel Terminal tool.
Go to the WordPress folder (for example, public_html).
Type: cd ~/public_html
Then, use the WP CLI tool to get the WordPress core files again, replacing the ones that are not working or missing:
Type: wp core download –force –skip-content
WordPress Archive
If you find it hard to use the cPanel Terminal or SSH, you can replace the main WordPress files by uploading a new copy of WordPress through FTP.
Go to the WordPress download page and get the latest version of WordPress: https://wordpress.org/download/#download-install
After downloading the latest WordPress, unzip the files on your computer and open this folder.
Delete the new “wp-content” folder in this folder because we don’t want to replace our current “wp-content” folder.
Open your FTP client and go to your WordPress folder (like public_html). Once in the WordPress folder on the server, upload the files from the WordPress folder on your computer (the one without the “wp-content” folder).
When asked if you want to replace existing files or folders, click Yes.
The FTP upload might take some time because there are many small files that make up the WordPress core.
5. Restore a Backup
If everything else hasn’t worked, you can bring back a backup of the site. A full backup includes the website’s files and a copy of the MySQL database.
If you don’t have a backup, you can ask your web hosting provider. Most providers automatically make backups of customer data, so they might have a copy.
Conclusion
Fixing the “Error Establishing a Database Connection” issue on the internet can be like solving a mystery. Every situation is a bit different, so there’s no one-size-fits-all solution.
While it might not be easy to have a perfect answer for everyone, many troubleshooting steps are not too hard to do on your own.
But, it’s okay to ask for help if you’re not sure what to do. If you don’t know exactly what you’re doing, it’s better to get assistance rather than risk making things worse.