Fix Error Establishing a Database Connection in Wordpress

How to Fix Error Establishing a Database Connection in WordPress?

WordPress is a combination of PHP and MySQL. So whenever you try to open a web page, it builds immediately. The PHP is actually a code that queries to access the MySQL database to fetch all the required information which is needed to deliver a web page to the user.

Whenever you come across a message like Error establishing a database connection in WordPress, it is simply that it is due to some reason or issues your PHP code was not able to connect with the MySQL database and was not able to fetch all the details that needed to build a page.

If you are familiar with WordPress, then at least once you would have faced this issue. It is one of the worst nightmares for WordPress users.This error may occur due to many reasons there is no single cause behind this error so it takes time to troubleshoot and solve.

Web Hosting

Before you go to the professionals, there are possible ways that you can fix the error by yourself. In this article, we will show you the possible ways to fix the database connection error.

What does error establishing a database connection mean?

To exactly understand what is error connecting to MYSQL, you must know how WordPress works. WordPress has written using two technologies- PHP and MySQL. The PHP is a coding language whereas MySQL is a database.

PHP decides how pages should display, manage user registrations, decides what happens when a user clicks a link and much more. Whereas Mysql includes the contents of your saved blog posts, user details, and data relating to plugins.

When someone visits a page on your website, WordPress will connect to your website database and fetches the contents of the page and then displays the page.

The database is a location where all your contents of your website stores. This information includes login credentials for all the users of the website. When WordPress is not able to connect to the database, then it won’t allow you access to the dashboard.

To connect to a database, WordPress needs to know the login credentials of your database. And in case if these login details are wrong or missing, then you won’t be able to connect to a database and you will have a database connection error. But there are also many reasons for this issue but login credentials is an important one.

Troubleshooting the error establishing a database connection in WordPress:

There are many reasons for this error and is simply means that WordPress cannot connect to your database to fetch and display the contents of your website. When you see this error, then one of the following can be the case:

  • The database needs repairing

  • You have set the wrong username and password in wp-config.php file

  • Mysql has stopped running

  • You have a plugin conflict

  • WordPress needs reinstalling

The database needs repairing:

If you want to check whether your database needs to be repaired, try to access the front and back ends of your website and look at the errors that display. In case if you get a different database error, then it indicates a repair issue.

Initially use FTP to access your wp-config.php file, open it in a plain text editor and then scroll towards the bottom until you see the message /*that’s all, stop editing! Happy blogging.*/ line.

Here directly above it add

define(‘WP_ALLOW_REPAIR’, true);

This will enable a unique URL that will repair your database. If you’re not sure, then find the URL you use to access the WordPress admin screen. You should be able to add yourwebsite.com/wp-admin/maint/repair.php to the end and navigate it to your browser. You will be able to see the number of messages, and at the bottom, you will see a line repairs complete.

Error Establishing a Database Connection in WordPress

Then finally delete define(‘WP_ALLOW_REPAIR’, true); from wp-config.php, and attempt to access the WordPress again. If this method does not solve the issues, then move on to the next method.

You have set wrong username and password in wp-config.php:

If you see and that there is an error in the database connection,  then it indicates that database information in wp-config.php needs to be updated. The credentials for the database is different from that you used to log into WordPress admin.

If you have not stored your login details for your database safe, then you have to reset a new username and password.To exactly find your WordPress database, open wp-config.php and look for the define(‘DB_NAME’, ‘example-database’); where example-database is the name of an active WordPress database.

You can reset your database username and password by following these steps:

Reopen wp-config.php

Look for define(‘DB_USER’, ‘example-username’);

Replace ‘example-username’ with the new database username you created

Look for define (‘DB_PASSWORD’, ‘example-password’);

Replace ‘example-password’ with the new database password you created.

Now save wp-config.php and then try to reconnect with WordPress in your browser. Then again if you see the same error:

Find the define (‘DB_HOST’, ‘localhost’); line

Replace it with define (‘DB_HOST’, $_ENV{DATABASE_SERVER});

Then save wp-config.php and then try to access the site again. If again this problem doesn’t solve, you will need to contact your host’s support and ask them for the correct database host to use in wp-config.php.

define(‘DB_NAME’, ‘example-database’);
define(‘DB_USER’, ‘example-username’);
define (‘DB_PASSWORD’, ‘example-password’);
define (‘DB_HOST’, ‘localhost’);

Once you have the details, find define(‘DB_HOST’, ‘localhost’); and replace localhost with the value they provide: save the file and check your site to see whether you can access your WordPress site. If you still face this error, then move to the next step.

Mysql has stopped running:

If the database is repaired and wp-config.php is up to date, MySQL may have stopped running on the server. To fix it, you will need to connect to the database using phpMyadmin. It is a web software like WordPress, but it is for managing MySQL databases and it comes installed on most servers.

If your hosting company offers cPanel access to administrate the account, then find phpMyadmin and log in. If you are not able to find it on your host,  then google search will give you the proper instructions.When these methods fail, you can connect to the database by using a custom PHP file.

To do so, you have to access your site through FTP, and then create an empty file called testdatabase.php in the same directory as wp-config.php. Now edit the file and add the following details replacing the example username and password with the information you already located.

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

Then finally go to http://example.com/testdatabase.php to test the MySQL server connection. If you again face the issue then contact your hosting company so that they will be able to restore MySQL access.

You have a plugin conflict:

Sometimes a plugin interrupts the database connection. To see if this is the case, open your FTP client again, navigate the wp-content folder, and rename the plugins folder to plugin-backup. Next, create a new, empty folder called plugins, then attempt to access the WordPress admin.

If you can access WordPress, a plugin is interrupting the database connection.  To identify which plugin it is, then you can reinstall and activate each plugin one at a time. This process may resolve the issue on its own.Once you have found the problem plugin, then you will be locked out of your site.

To regain the access, log in through FTP and enter the plugin folder and rename the individual plugin folder by appending -conflict to the end. And finally, you should be able to reinstall and reactivate the rest of your plugins without a problem.

WordPress needs reinstalling:

If all the method fails, and even then this error occurs, then you must reinstall the WordPress again. And there is no other way to prevent other than a reinstall. It is most important to note that, no data will be lost and everything will be fine.

If you have modified the core files, then that can break your site. There is also a possibility to empty your WordPress database by mistake. Due to this risk, reinstalling WordPress is the last option.  Make sure you have a regular backup.

Once your WordPress is re-installed, attempt to visit your site in the browser. This is the most extreme case and it will fix the connection issues that the above methods could not. And again if you are not able to connect, then definitely you have to contact your host team.

Conclusion

Error establishing a database connection in WordPress will really bring a fear to the WordPress beginners. But there are certain methods to solve this issue as we discussed above. In case any of these methods don’t work then you have contact your web host team to fix the issue.

They will clear this issue and then you can start connecting the database to your site.

Hope you liked this article. If you have any queries please feel free to comment us. You can subscribe us on Facebook and Twitter.

Prabhu Ganesan

Prabhu Ganesan has provided his Web development and SEO services independently. He is Co-founder of Webicle Media and WPBlogX. He is very much involved in WordPress as it becomes a second nature for him. Find him on twitter @gprabhucbe

6 thoughts to “How to Fix Error Establishing a Database Connection in WordPress?”

  1. Hello there,
    Am Olga and I started working with WordPress quite a few years back. I have designed numerous websites with creative ideas. People who work with WordPress would definitely come across the problem error establishing the database connection and it is quite tricky to solve this problem. even i have faced this problem many times. but your article is really helpful for the beginners who start working with WordPress. with the help of this article, they can easily rectify the problem and resolve it.

  2. Hello
    This is normal. I am a web designer. My passion is to design creative websites. WordPress is one of the best tools to create the website nowadays. it is been almost used by all the web designers. As a WordPress user, all will have come across the problem error establishing a database connection. This error is quite difficult to understand where we went wrong. But there are methods to resolve it and your post have done an excellent job of posting the information about resolving this problem. it is helpful for both the beginners as well as WordPress users. Thank you so much.

  3. My Website Is Loading, And Working Fine But I Get Error Establishing Database Connection When I Tried Wp-admin And Again I Reloaded Page 3-10 Times And My Site Is Working, Again And Again, I Get That Error Unexpectedly And My Whole Site Shows Same But Again After Some Refresh, My Site Is Working Again… Help Me Please

  4. I’m getting this error on normal sites, not mine so no blogs etc just browsing the net and when I press next I get “Error establishing a database connection” I don’t know what to do, even on Facebook, I clicked a link to read a story and it said 404 error I don’t have permission, I haven’t changed any settings.

  5. Thanks so much, all, for maintaining such a great website! You guys continue to bail me out with problems, issues & fixes for those of us who just enjoy writing & posting and not dealing with all of the WordPress programming stuff. Great work!
    Cheers!!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.