{"id":4023,"date":"2015-06-01T04:01:00","date_gmt":"2015-06-01T04:01:00","guid":{"rendered":"https:\/\/wpopal.com\/?p=4023"},"modified":"2015-06-01T04:01:00","modified_gmt":"2015-06-01T04:01:00","slug":"how-to-fix-the-error-establishing-a-database-connection-in-wordpress","status":"publish","type":"post","link":"http:\/\/dev.wpopal.com\/wpopal\/how-to-fix-the-error-establishing-a-database-connection-in-wordpress\/","title":{"rendered":"How to Fix the Error Establishing a Database Connection in WordPress"},"content":{"rendered":"<p>Having been a website developer surely you meet once or twice the error of establishing a database connection. How do you feel when waiting 20 -30 minutes to detect and fix it? Don&#8217;t too worry, any problems have solutions, importantly you must be patient and find the best way to solve it effectively. Being a faithful follower of wpbeginner, Opal WordPress is very happy to share a great article about the above error. Hoping you can learn experience and fix it yourself with each step in that tutorial. Please spend time reading and thinking logically, surely you will see its excellence. If you feel interested, remember to share this post, once again say thanks to <a href=\"http:\/\/www.wpbeginner.com\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">wpbeginner<\/a> and the author\u00a0<a href=\"https:\/\/plus.google.com\/101623299936375408403\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Syed Balkhi<\/a>\u00a0with a great blog!<br \/>\nIf you have been surfing the web for a while, you have at least seen this error a few times. Error Establishing a Database Connection is one of those curses that could be caused by many reasons. As a WordPress beginner, this could be awfully frustrating specially when it happened on its own without you changing anything. We ran into this issue yesterday on our own site. It took a little over 20 minutes to detect and fix the problem. While doing the research to find possible causes, we realized that there was no good article that covered everything. In this article, we will show you how to fix the error establishing a database connection in WordPress by compiling a list of solutions all in one place.<br \/>\n<strong>Note: Before you make any database changes, make sure you have sufficient backups.<\/strong><\/p>\n<h4>Why do you get this error?<\/h4>\n<p>Well in short, you are getting this error because WordPress is unable to establish a database connection. Now the reason why WordPress is unable to establish a database connection can vary. It could be that your database login credentials are wrong or have been changed. It could be that your database server is unresponsive. It could be that your database has been corrupted. In our experience, majority of the times this error happens because of some sort of server error however there could be other factors as well. Lets take a look at how to go about troubleshooting this problem.<\/p>\n<h4>Does the problem occur for \/wp-admin\/ as well?<\/h4>\n<p>First thing you should do is to make sure that you are getting the same error on both the front-end of the site, and the back-end of the site (wp-admin). If the error message is the same on both pages \u201cError establishing a database connection\u201d, then proceed onto the next step. If you are getting a different error on the wp-admin for instance something like \u201cOne or more database tables are unavailable. The database may need to be repaired\u201d, then you need to repair your database.<br \/>\nYou can do this by adding the following line in your wp-config.php file:<\/p>\n<div id=\"highlighter_23877\" class=\"syntaxhighlighter  \">\n<div class=\"lines\">\n<div class=\"line alt1\"><a href=\"https:\/\/wpopal.com\/wp-content\/uploads\/2015\/06\/a1.png\" rel=\"nofollow noopener\" target=\"_blank\"><img decoding=\"async\" class=\"alignnone size-full wp-image-4025\" src=\"https:\/\/wpopal.com\/wp-content\/uploads\/2015\/06\/a1.png\" alt=\"a1\" width=\"552\" height=\"22\" title=\"\"><\/a><\/div>\n<\/div>\n<\/div>\n<p>Once you have done that, you can see the settings by visiting this page:<strong>http:\/\/www.yoursite.com\/wp-admin\/maint\/repair.php<\/strong><br \/>\n<img decoding=\"async\" src=\"http:\/\/cdn4.wpbeginner.com\/wp2-9\/dbrepair.gif\" alt=\"WordPress Database Repair\" title=\"\"><br \/>\nRemember, the user does not need to be logged in to access this functionality when this define is set. This is because its main intent is to repair a corrupted database, Users can often not login when the database is corrupt. So once you are done repairing and optimizing your database, make sure to remove this from your wp-config.php.<br \/>\nIf this repair did not fix the problem, or you are having trouble running the repair then continue reading this article as you might find another solution to work.<\/p>\n<h4>Checking the WP-Config file<\/h4>\n<p><a title=\"WPConfig.php file\" href=\"http:\/\/www.wpbeginner.com\/wp-tutorials\/useful-wordpress-configuration-tricks-that-you-may-not-know\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">WP-Config.php<\/a> is probably the single most important file in your entire WordPress installation. This is where you specify the details for WordPress to connect your database. If you changed your root password, or the database user password, then you will need to change this file as well. First thing you should always check is if everything in your wp-config.php file is the same.<\/p>\n<div id=\"highlighter_400364\" class=\"syntaxhighlighter  \">\n<div class=\"lines\">\n<div class=\"line alt1\"><a href=\"https:\/\/wpopal.com\/wp-content\/uploads\/2015\/06\/a7.png\" rel=\"nofollow noopener\" target=\"_blank\"><img decoding=\"async\" class=\"alignnone size-full wp-image-4032\" src=\"https:\/\/wpopal.com\/wp-content\/uploads\/2015\/06\/a7.png\" alt=\"a7\" width=\"557\" height=\"72\" title=\"\"><\/a><\/div>\n<\/div>\n<\/div>\n<p>Remember your DB_Host value might not always be localhost. Depending on the host, it will be different. For popular hosts like HostGator, BlueHost, Site5, it is localhost. You can find other host values here.<br \/>\nSome folks suggested that they fixed their problem by replacing localhost with the IP. It is common to see this sort of issue when running WordPress on a local server environment. For example on MAMP, the DB_Host value when changed to the IP may seem to work.<br \/>\n<a href=\"https:\/\/wpopal.com\/wp-content\/uploads\/2015\/06\/a3.png\" rel=\"nofollow noopener\" target=\"_blank\"><img decoding=\"async\" class=\"alignnone size-full wp-image-4027\" src=\"https:\/\/wpopal.com\/wp-content\/uploads\/2015\/06\/a3.png\" alt=\"a3\" width=\"557\" height=\"28\" title=\"\"><\/a><br \/>\nIP\u2019s will vary for online web hosting services.<br \/>\nIf everything in this file is correct (make sure you check for typos), then it is fair to say that there is something wrong on the server end.<\/p>\n<h4>Check your Web Host (MySQL Server)<\/h4>\n<p>Often you will notice this Error establishing database connection when your site gets swarmed with a lot of traffic. Basically, your host server just cannot handle the load (specially when you are on shared hosting). Your site will get really slow and for some users even output the error. So the best thing you should do is get on the phone or livechat with your hosting provider and ask them if your MySQL server is responsive.<br \/>\nFor those users who want to test if MySQL server is running yourself, you can do a few things. Test other sites on the same server to see if they are having the issue. If they are also getting the same error, then most definitely there is something wrong with your MySQL server. If you do not have any other site on this same hosting account simply go to your cPanel and try to access phpMyAdmin and connect the database. If you can connect, then we need to verify if your database user has sufficient permission. Create a new file called <strong>testconnection.php<\/strong> and paste the following code in it:<\/p>\n<div id=\"highlighter_454807\" class=\"syntaxhighlighter  \">\n<div class=\"lines\">\n<div class=\"line alt1\"><a href=\"https:\/\/wpopal.com\/wp-content\/uploads\/2015\/06\/a4.png\" rel=\"nofollow noopener\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4028\" src=\"https:\/\/wpopal.com\/wp-content\/uploads\/2015\/06\/a4.png\" alt=\"a4\" width=\"556\" height=\"132\" title=\"\"><\/a><\/div>\n<\/div>\n<\/div>\n<p>Make sure to replace the username and password. If the connected successfully, then it means that your user has sufficient permission, and there is something else that is wrong. Go back to your wp-config file to make sure that everything there is correct (re-scan for typos).<br \/>\nIf you cannot connect to the database by going to phpMyAdmin, then you know it is something with your server. It does not necessarily means that your MySQL server is down. It could mean that your user does not have sufficient permission.<br \/>\nIn our case, our MySQL server was running. All other sites on the servers were working fine except for WPBeginner. When we tried going to our phpMyAdmin, we ended up getting the error:<br \/>\n<strong>#1045 \u2013 Access denied for user \u2018foo\u2019@\u2019%\u2019 (using password: YES)<\/strong><br \/>\nWe got on the phone with HostGator and their support quickly found the problem. Somehow our user\u2019s permissions were reset. Not sure how that happened, but apparently that was the reason. They went back in and restore the permissions and we were able to get the site back live.<br \/>\nSo if you get the access denied error in either connecting to your phpMyAdmin or through testconnection.php results, then you should contact your host right away to get them to fix it.<\/p>\n<h4>Solutions that Worked for Others<\/h4>\n<p>It is important to note, that these may not work for you. Use at your own risk and make sure that you have sufficient backups if anything goes wrong.<br \/>\nDeepak Mittal said that his client was getting the error that database needs to be repaired. Even after repairing the database, the error did not go away. He tried various things and at the end, the issue was the site url. Apparently that was changed which caused the error to persist. He ran the SQL query by going to phpMyAdmin:<\/p>\n<div id=\"highlighter_243877\" class=\"syntaxhighlighter  \">\n<div class=\"lines\">\n<div class=\"line alt1\"><a href=\"https:\/\/wpopal.com\/wp-content\/uploads\/2015\/06\/a6.png\" rel=\"nofollow noopener\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4030\" src=\"https:\/\/wpopal.com\/wp-content\/uploads\/2015\/06\/a6.png\" alt=\"a6\" width=\"554\" height=\"38\" title=\"\"><\/a><\/div>\n<div class=\"line alt1\">Make sure to replace YOUR_SITE_URL with the actual url example: http:\/\/www.wpbeginner.com. The wp_options will be different if you have<a title=\"How to Change the WordPress Database Prefix\" href=\"http:\/\/www.wpbeginner.com\/wp-tutorials\/how-to-change-the-wordpress-database-prefix-to-improve-security\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">changed the default WordPress database prefix<\/a>.<\/div>\n<\/div>\n<\/div>\n<p>This seemed to fix the issue for him and few others that commented on his post as well.<br \/>\n<a title=\"Sachinum\" href=\"http:\/\/wordpress.org\/support\/topic\/error-establishing-a-database-connection-323#post-2613368\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Sachinum<\/a> suggested that he was able to connect the database with testconnection.php, so he changed the wp-config.php user to the root user. WordPress started to work perfectly fine. Then he reverted the settings back to the database-user, and it continued to work. He could not figure out what was wrong, but concluded that it was a typo.<br \/>\nCutewonders suggested that they removed the content of active_plugins in wp_options table and edited the contents of recently_edited. Basically that seemed to fix the problem. Please their full response here.<br \/>\nWe read on numerous sources that users simply uploaded a fresh copy of WordPress and it fixed the error.<br \/>\nThis is a really frustrating error. What have you tried that seemed to work for you? We would be happy to expand on this resource, so others do not have to waste as much time finding a solution.<br \/>\nIn nutshell, this article will be useful for wordpress beginners. Please read and think carefully then you can do it yourself well. Remember to follow our social channels such as <a class=\"blue\" href=\"https:\/\/www.facebook.com\/opalwordpress\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">Facebook<\/a>, <a class=\"blue\" href=\"https:\/\/twitter.com\/?lang=en\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">Twitter<\/a>, <a class=\"blue\" href=\"https:\/\/www.linkedin.com\/home?trk=nav_responsive_tab_home\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">Linkedin<\/a>, <a class=\"blue\" href=\"https:\/\/www.youtube.com\/user\/WPOpalTheme\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">Youtube,<\/a> <a class=\"blue\" href=\"https:\/\/www.pinterest.com\/cunconvp192\/\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">Pinterest<\/a> to get latest information!<br \/>\nThanks so much!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Having been a website developer surely you meet once or twice the error of establishing a database connection. How do you feel when waiting 20 -30 minutes to detect and fix it? Don&#8217;t too worry, any problems have solutions, importantly you must be patient and find the best way to solve it effectively. Being a [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":30904,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[27],"tags":[],"class_list":["post-4023","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"_links":{"self":[{"href":"http:\/\/dev.wpopal.com\/wpopal\/wp-json\/wp\/v2\/posts\/4023","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/dev.wpopal.com\/wpopal\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dev.wpopal.com\/wpopal\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dev.wpopal.com\/wpopal\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/dev.wpopal.com\/wpopal\/wp-json\/wp\/v2\/comments?post=4023"}],"version-history":[{"count":0,"href":"http:\/\/dev.wpopal.com\/wpopal\/wp-json\/wp\/v2\/posts\/4023\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/dev.wpopal.com\/wpopal\/wp-json\/wp\/v2\/media\/30904"}],"wp:attachment":[{"href":"http:\/\/dev.wpopal.com\/wpopal\/wp-json\/wp\/v2\/media?parent=4023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dev.wpopal.com\/wpopal\/wp-json\/wp\/v2\/categories?post=4023"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dev.wpopal.com\/wpopal\/wp-json\/wp\/v2\/tags?post=4023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}