How To Fix Wordpress Error Establishing A Database Connection


As a WordPress site owner, there are a few error messages that you dread. The “error establishing database connection” is among the most feared, but rest assured that it’s common và can be resolved in a few ways.

Bạn đang xem: How to fix wordpress error establishing a database connection


In this post, we’ll take a closer look at what exactly this error is and then walk through the different steps you can take to lớn fix it.


What does “Error Establishing a Database Connection” mean?

You can figure out that WordPress had trouble connecting to a database from this error message, but what exactly does that mean?

To fully understand this error, we need to look behind the scenes at how WordPress displays web pages. This complexity is hidden from the visitor.

You’ll remember from installing your WordPress site that you had khổng lồ create a database. That’s because every site needs a centralized database that stores information in order lớn run. Your posts and pages, categories và tags, user data and comments, plugin and theme-related data, và site-wide settings are all stored there.

To access that treasure trove of data, WordPress uses a programming language called PHP.

Whenever someone enters a URL into their browser, WordPress uses PHP to lớn ask for information from the database. It then brings back the necessary data to display that website page lớn the visitor on their browser. When WordPress is unable to establish a database connection, it will display an error message instead.



See how the page is mostly blank? That’s because no data can be retrieved lớn render the page since the database connection isn’t working. Such an error will not only prevent your visitors from accessing the frontend of your site, but it will also prevent you from accessing your WordPress dashboard.

To get your site running as smoothly as possible again, let’s look at a few different solutions for this issue.

How to lớn Fix Error Establishing a Database Connection

Repair & optimize your database. Verify your database login credentials. Restore default files. Liên hệ your hosting provider.

Though the “error establishing a database connection” message might seem ominous, we know that one common cause is as simple as updating your credentials. While some of these steps are more difficult & require a bit of coding, they are all workable solutions for users at any level. Let’s look at these solutions more closely below.

Step 1: Repair and optimize your database

Your first step? Go to lớn your WordPress dashboard and see what error message appears. If you see the same “error establishing a database connection,” then move onto the next step.

However, if you see another message, notice the suggestions that WordPress offers. One such message might begin, “One or more database tables are unavailable...” This means you need to lớn repair and optimize your database before accessing it.

WordPress simplifies the process for you. Just log in to lớn your control panel, xuất hiện the tệp tin Manager, & navigate to lớn the wp-config.php file. At the end of the file, right before the line that reads /* That"s all, stop editing! Happy */ địa chỉ the following line of code.

define("WP_ALLOW_REPAIR", true);

Save your changes and go lớn You should see the following message.



You can click either button, but the latter is recommended to lớn optimize your site’s performance. When the repair is finished, go back to your site and kiểm tra if the error is still there.

Xem thêm: Hướng Dẫn Cách Copy Ảnh Từ Word Ra Ngoài, Cách Lấy Ảnh Từ File Word Cho Chất Lượng Tốt Nhất

If there’s no error, that’s great — but you’re not quite finished. You need to go back into your wp-config.php file, remove that line of code you added, and save it. If the error still appears when you visit your site, you’ll have to lớn move on to the next step.

Step 2: Verify your database login credentials

You know you need to remember your login credentials lớn access your database. That’s a no brainer. But there may be incorrect details stored in your WordPress configuration tệp tin (wp-config.php) impacting your connection. This can happen when your tệp tin is not updated properly after migrating to a new hosting provider.

To verify that your information is correct, mở cửa the wp-config.php file via your cPanel & look at the following lines of code.

define("DB_NAME", "database-name");

define("DB_USER", "database-username");

define("DB_PASSWORD", "database-password");

define("DB_HOST", "localhost");

You can compare these values to lớn those stored in your database to lớn ensure they match. You can find that info by logging on lớn your cPanel và going to lớn the Database section.

If the values vày not match, edit the wp-config.php file và save your changes. Then navigate lớn If you get a “connected successfully” message then your login details are now correct & the database connection error should be resolved.

Step 3: Restore mặc định files

It’s rare but chip core WordPress files can be corrupted. A hacking attempt on your site or a rogue plugin may modify these files.

Whatever the reason, you can restore the default files. The process is simple, but the stakes are high. One mistake can wipe out your site. That’s why you need khổng lồ back up your site before proceeding.

Now that you have a backup, tải về a new version of WordPress. Unzip the file, then find & delete the wp-config.php & wp-contents folder. This will ensure you don’t override your existing wp-config.php tệp tin or thua any themes, plugins, and media stored in the wp-content folder once you restore WordPress.



Next, xuất hiện File Manager on your cPanel và upload your new WordPress files. This will override all the previous files (except for wp-config.php & wp-contents folder).

When the upload is complete, go back to your site khổng lồ see if the error is resolved. If you’re still seeing an error message, it’s time to tương tác your hosting provider.

Step 4: contact your hosting provider

If none of the steps above resolve your issue, then it could be an issue with your database server. A spike in traffic on the server, for example, could be the cause since website hosts like & Bluehost limit how many database connections are allowed at once.

If you let them know you’re experiencing an error establishing database connection on your site, they can let you know what the exact cause is. Best case scenario: they might even fix it for you.

There’s no foolproof method for preventing every error establishing a database connection, but there are steps you can take to resolve them. By fixing this error, you’ll ensure that visitors continue khổng lồ have seamless experiences on your site.

hotlive |