error-establishing-database-connection-wordpress

If you’re facing an “error establishing a database connection” issue in WordPress, this guide is for you. It’s one of the common problems of WordPress in which the database causes issues because it has corrupted or you’re using incorrect information. 

Many other problems can also cause the issue, but the good part is it’s not an unsolvable problem. As I’ve been using WordPress for over a decade, I’ve fixed this issue numerous times for my clients and am well aware of the solutions. 

For your help, I’ll disclose all of these solutions below in detail so you can get rid of this problem yourself at your home without anyone’s help. Let’s get into the details and find out how to fix the error establishing database connection in WordPress. 

What Is “Error Establishing Database Connection” Issue & Its Causes

Before I go further, I want you to know about the “error establishing database connection”. What are its main causes, and where are you making mistakes? This will help us understand the problem better. 

error-establishing-database-connection-reasons

WordPress is a CMS (content management system) that allows us to post content online for the audience but have you ever wondered where it stores your content? It stores data in MySQL, a renowned database management system. 

With the help of MySQL, we can store and get access to our data anytime we want. However, sometimes, when you try to access your WordPress website, you might encounter an annoying error message that says, “Error establishing a database connection.” 

The main causes of this error can vary, but there are a few common culprits to consider, like database credentials. This means that the information needed to connect to the MySQL database, username, password, etc., may be entered incorrectly or have changed.

Another cause could be a problem with your web hosting server. The server hosting your website may be experiencing issues, such as high traffic or resource limitations, which can fail to establish a connection to the database.

Furthermore, if your MySQL database is experiencing problems, it can lead to the “Error establishing database connection” message. Issues like corruption, misconfiguration, or insufficient resources might be the reason for this issue. 

7 Ways To Fix Error Establishing Database Connection In WordPress

Now you know the main possible causes of the database connection error, let’s move toward the solutions. It’s better to follow the methods one by one; first, check the login credential solution, then web hosting; if they don’t work, get help with the third solution!

1. Check Database Login Credential 

The main reason why we face issues of database connection errors is because of the login credentials. It usually happens when we change the web hosting of the WordPress website to another server. 

It can be fixed with the wp-configure. php file. You only have to access this file in the database and check whether your login credentials match it. If not, you must edit the MySQL database settings related to user credentials. 

  • Get access to the cPanel of your hosting server. 
  • Then find “File Manager” in the options. 
file-manager-to-check-login
  • Go to the “Public.html” option. 
public-html-file-to check-database-error
  • Double-click on “wp-config.php” to get access to the file. 
wp-config-file-to-access-database
  • In the “wp-config.php” file, find the username, database name, etc. 
fix-error-establish-database-wpconfig
  • You can easily find the credentials between the code like this:
    • /** The name of the database for WordPress */
    • define( ‘DB_NAME’, ‘database_name_here‘ );
    • /** MySQL database username */
    • define( ‘DB_USER’, ‘username_here‘ );
    • /** MySQL database password */
    • define( ‘DB_PASSWORD’, ‘password_here‘ );
  • Note down the username, database name, and password. 
  • Go back to the cPanel and go to “MySQL database.” 
  • You’ll see a list of your current MySQL databases and users.
mysql-database-username
  • Find the right username that’s the same as written in the “wp-config.php” file. 
  • If they’re not similar, update the credentials in the “wp-config.php” file.
    • define(‘DB_NAME’,’fill_this_with_your_current_database_name’);
    • define(‘DB_USER’,’fill_this_with_your_current_username‘);
    • define(‘DB_HOST’,’fill_this_with_your_current_localhost‘);

Now, try accessing your WordPress website again. The issue of database connection errors should be resolved if the login credentials were the cause.

2. Contact Web Hosting Provider

If the first option doesn’t work, contact the web hosting provider. Your MySQL server might be the reason for all this mess. Sometimes servers go down because of surpassing the maximum number of clients at a time. 

It usually happens with shared hosting because all websites use the same hosting server resources. The best solution for this issue is to contact your hosting service provider and install a web caching plugin to reduce server load. 

Note: I recommend using Perfmatters, WP-Rocket, or the FlyingPress plugin for web caching.  They’ve many other features like optimizing databases etc., to make sure the load on your server isn’t too much.  

Likewise, I recommend buying world-class managed hosting like Cloudways so that you will give a minimum load to the server. With this superior cloud hosting, you will never face a common WordPress problem like Error Establishing Database Connection In WordPress.

3. Repair WordPress Database 

Those who’re sure that their WordPress database has corrupted, which is why you can’t log in to your website, should use the “Repair database” function. It’s designed to repair corrupt files that become the reason for errors. 

  • Login to the cPanel and select MySQL database
  • Find the option “Modify databases” setting. 
repair-wordpress-database
  • Under the “Modify databases” settings, you’ll see the option “Repair database.”
  • First, select your database, then click the “Repair database” option. 
  • It will take some time, but once it’s repaired, try login to your website.

4. Fix Corrupted WordPress Database 

Plugins and themes are crucial in the “error establishing a database connection” issue. We don’t check the theme and plugins and download them to our website without considering the consequences. 

To solve this problem, consider when you activated a new theme and plugin last time. If it was a day or a few hours ago before you saw the database connection error, this could be the problem. Go to the dashboard or activate a new theme from the library. 

If the issue is still the same as before, check the plugins. Delete the plugin you’ve activated recently; if you’re unable to access the dashboard go to the “cPanel > File manager > Public_html >  wp-content > plugin. 

Once you find the “Plugin” file in the database, double-click on it. Select the option “Rename” and change the file name. For example, you can add “disable” after the plugin name (plugin-disable) or add anything else, but ensure to remember the name. 

By renaming the file, all plugins will be disabled automatically for a specific period until you rename the file again to the right name (plugin). So, try to log in to your website using the credentials and see whether the issue is solved. 

Note: Don’t forget to change the file name to “plugin” after connecting to the website.

5. Fix Corrupt Database 

If the third option doesn’t work for you, you must repair the corrupted database. The reason for this issue is the same, using the corrupted plugin or themes. Luckily, the solution isn’t too difficult but needs your full attention. 

  • Go to the cPanel of your hosting server. 
  • Find the “File Manager” option and select it.  
  • Click on the file name “wp-config.php.”
  • Edit the “wp-config.php” file and add the code “wp_allow_repair” above the line (‘That’s all, stop editing! Happy blogging’ ).
    • define(‘WP_ALLOW_REPAIR‘, true);
  • Click on save, and run this link into your browser: http://www.your-site-domain.com/wp-admin/maint/repair.php

By running this link, you’ll get two options: “Repair database” and “Repair and optimize database.” you can pick any suitable option for your website. Afterward, try to access your WordPress website. 

6. Create New Database 

This step is best when no other solution works because it’s about creating a new database rather than repairing the last one. After following the steps, all problems related to your website’s database will be solved. 

  • Get access to the cPanel and select “MySQL Databases.”
  • Then create a new database by entering any name like paul, mydatabase etc. 
create-new-database-to-fix-error
  • After creating a database, click on “Go back.”
mysql-database
  • Scroll the page and go to the “Add new user” section. 
  • Add a username and set a password.
add-new-user-mysql-database
  • Click on “Create User.”
  • Scroll down again and find the “Add user to database” option. 
  • Add the “username” and “database” names you’ve set above. 
add-user-wordpress-database
  • On the next tab, click on “Assign all privileges”; that’s it. 

7. Reboot Your Web Server 

The next and last step is to reboot your web server, the same function we have on our smartphones, laptops, etc. After rebooting, the problem of “error establishing a database connection” will be solved. 

  • Get access to the cPanel. 
  • Find the option “System reboot” and click on it. 
reboot-web-server
  • Select “Graceful server reboot” then “Proceed.

Conclusion

WordPress has many common problems. Hopefully, you’ve got an idea about how to fix the “error establishing database connection” issue in WordPress. I’ve discussed the best seven solutions above with a comprehensive step-by-step guide with screenshots.

I want to ensure you’ve read everything and followed the steps properly. Most steps include editing the database, which could lead to a big problem if not followed properly. So be careful, and don’t forget to optimize your WordPress database regularly.

Share this post

Source: https://www.wpglossy.com/error-establishing-database-connection-wordpress/



You might also like this video

Leave a Reply