{"id":1518,"date":"2021-05-10T13:48:39","date_gmt":"2021-05-10T10:48:39","guid":{"rendered":"https:\/\/www.scalahosting.com\/kb\/?p=1518"},"modified":"2025-11-27T11:43:44","modified_gmt":"2025-11-27T09:43:44","slug":"how-to-fix-the-error-establishing-a-database-connection-in-wordpress","status":"publish","type":"post","link":"https:\/\/www.scalahosting.com\/kb\/how-to-fix-the-error-establishing-a-database-connection-in-wordpress\/","title":{"rendered":"Fix WordPress Database Connection Error"},"content":{"rendered":"\n<p><span style=\"font-weight: 400;\">As a <\/span><b>website owner or administrator<\/b><span style=\"font-weight: 400;\">, seeing an <\/span><i><span style=\"font-weight: 400;\">\u201cerror establishing a Database Connection\u201d <\/span><\/i><span style=\"font-weight: 400;\">on your website can send you into panic mode, especially if you\u2019ve never encountered this before. If you are in this situation right now, you have nothing to worry about because you can <\/span><b>fix this error without any permanent damage<\/b><span style=\"font-weight: 400;\"> to your website.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">This article will show you the <\/span><b>different ways to fix this error<\/b><span style=\"font-weight: 400;\"> whenever it occurs, but first, it is essential to understand what the problem is and why it happens.&nbsp;<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><b>What Is A Database Connection<\/b><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">Two technologies &#8211;<\/span><b>PHP and <\/b><a href=\"https:\/\/www.scalahosting.com\/kb\/how-can-i-empty-a-mysql-database\/\"><b>MySQL<\/b><\/a><span style=\"font-weight: 400;\">, control <\/span><b>WordPress<\/b><span style=\"font-weight: 400;\"> functions.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">PHP is the <\/span><b>WordPress programming language,<\/b><span style=\"font-weight: 400;\"> while MySQL is the <\/span><b>database technology. <\/b><span style=\"font-weight: 400;\">WordPress is a content management system that uses a database to store and manage data. Every time there is a successful connection and your website loads entirely, there was an excellent <\/span><b>database connection via the PHP commands.&nbsp;<\/b><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><b>What Causes A Database Connection Error<\/b><\/h2>\n\n\n\n<p><b>For your website to establish a successful database connection, it will require the following credentials :<\/b><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Database username<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Database password<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Database name<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Database server<\/span><\/li><\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">So whenever a database connection error occurs, WordPress cannot access the <\/span><b>WordPress database information <\/b><span style=\"font-weight: 400;\">cannot be accessed.&nbsp;<\/span><\/p>\n\n\n\n<p><b>This can be due to some reasons:<\/b><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li style=\"font-weight: 400;\" aria-level=\"1\"><b>Incorrect login credentials:<\/b><span style=\"font-weight: 400;\"> To access the database, WordPress requires the correct login username and password. When these <\/span><b>login credentials<\/b><span style=\"font-weight: 400;\"> are wrong, WordPress would not be able to access the database.<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><b>Corrupted database and files<\/b><span style=\"font-weight: 400;\">: WordPress files and databases can get corrupted due to a malicious attack or failure to update themes and plugins. When the database or its files are corrupted, it can be <\/span><b>challenging to establish a database connection.<\/b><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><b>Database server downtime:<\/b><span style=\"font-weight: 400;\"> Whenever the WordPress database experiences a downtime, it would not be possible to connect with it at that time.<\/span><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><b>How to Fix the Error Establishing A Database Connection<\/b><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">The first step to take before <\/span><b>fixing a database connection error<\/b><span style=\"font-weight: 400;\"> is to <\/span><a href=\"https:\/\/www.scalahosting.com\/blog\/backup-wordpress-site\/\"><b>backup your website<\/b><\/a><span style=\"font-weight: 400;\">.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">This can be done by using a simple WordPress backup plugin.&nbsp;<\/span><\/p>\n\n\n\n<p><b>The error in establishing a database connection can be fixed in a few ways:<\/b><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Check your database login credentials<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Repair your corrupted database<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Fix corrupt files<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Check if your database server is down<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Troubleshoot with your hosting provider<\/span><\/li><\/ol>\n\n\n\n<p><span style=\"font-weight: 400;\">Let\u2019s break these solutions down into actionable bits.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><b>1. Check Your Database Credentials<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">To access the database, WordPress uses a <\/span><b>specific username and password.&nbsp;<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">These connection details are stored in the <\/span><b>wp-config.php file<\/b><span style=\"font-weight: 400;\"> of your file\u2019s manager. This file cannot change on its own, so you\u2019d typically experience this whenever you change your<\/span><b> hosting provider<\/b><span style=\"font-weight: 400;\"> or made any changes to your site\u2019s file.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">To fix this error, locate the wp-config.php file<\/span><i><span style=\"font-weight: 400;\"> (this is usually found in the root directory)<\/span><\/i><span style=\"font-weight: 400;\"> using either an <\/span><b>FTP client<\/b><span style=\"font-weight: 400;\"> or by going to your file\u2019s manager.&nbsp;<\/span><\/p>\n\n\n\n<p><b>Check for the following line of codes :<\/b><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li style=\"font-weight: 400;\" aria-level=\"1\"><i><span style=\"font-weight: 400;\">define(&#8216;DB_NAME&#8217;, &#8216;some_db&#8217;)<\/span><\/i><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><i><span style=\"font-weight: 400;\">define(&#8216;DB_USER&#8217;, &#8216;some_username&#8217;)<\/span><\/i><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><i><span style=\"font-weight: 400;\">define(&#8216;DB_PASSWORD&#8217;, &#8216;some_pass&#8217;)<\/span><\/i><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><i><span style=\"font-weight: 400;\">define(&#8216;DB_HOST&#8217;, &#8216;localhost&#8217;)<\/span><\/i><\/li><\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">Make sure that the information provided for the different parameters is correct. You can confirm the details by going to the <\/span><b>MySQL section of your SPanel dashboard.&nbsp;<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">If the details don\u2019t match, you can reset them by editing the wp-config.php file.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Now, try <\/span><b>reloading your site<\/b><span style=\"font-weight: 400;\"> to see if it works.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">If it doesn\u2019t work, keep reading this article for more troubleshooting steps.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><b>2. Repair Corrupted Databases<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">If you are getting an error that says, \u201c<\/span><i><span style=\"font-weight: 400;\">One or more database tables are unavailable\u2019\u2019,<\/span><\/i><span style=\"font-weight: 400;\"> then this means that you have to fix the error by yourself.&nbsp; Repairing a<\/span><b> corrupted database<\/b><span style=\"font-weight: 400;\"> is similar to the previous step we shared above.&nbsp;<\/span><\/p>\n\n\n\n<p><b>This time, all you have to do is add the following line of code to the bottom of your wp-config.php file:<\/b><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li style=\"font-weight: 400;\" aria-level=\"1\"><i><span style=\"font-weight: 400;\">define(&#8216;WP_ALLOW_REPAIR&#8217;, true);<\/span><\/i><\/li><\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">Then go to your browser and visit this link <\/span><b>https:\/\/yourdomain.com\/wp-admin\/maint\/repair.php<\/b><span style=\"font-weight: 400;\">. You will be presented with an option to either repair the database or optimize the database.\u00a0<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Choose <\/span><b>Repair database.<\/b><span style=\"font-weight: 400;\">&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">After running this command, ensure that you remove the<\/span><b> line of code<\/b><span style=\"font-weight: 400;\"> that you initially added to the wp-config.php file.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">If you don\u2019t do this, it simply means anyone can run a repair on your database.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><b>3. Fix Corrupt Files<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Your files can get corrupted as a result of a malicious attack by a hacker or <\/span><b>during a file transfer process. <\/b><span style=\"font-weight: 400;\">This corrupted file is another reason why you get an error establishing a database connection.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">To fix this error, we are not going to touch your existing files. We are simply going to make a fresh <\/span><a href=\"https:\/\/www.scalahosting.com\/blog\/how-to-install-wordpress\/\"><b>WordPress installation.<\/b><\/a><span style=\"font-weight: 400;\"> But first of all, it is vital to backup your existing website. After the backup, visit <\/span><b>wordpress.org <\/b><span style=\"font-weight: 400;\">to download a new version.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">After downloading this file, unzip it on your computer and <\/span><b>locate the wp-content folder<\/b><span style=\"font-weight: 400;\"> as well as the<\/span> <b><i>wp-config-sample.php<\/i><\/b><b> file.&nbsp; <\/b><span style=\"font-weight: 400;\">After deleting these files, upload the remaining files via <\/span><b>SFTP to your site. <\/b><span style=\"font-weight: 400;\">This will replace all corrupted files and install the new ones.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Ensure you <\/span><b>clear your cache files <\/b><span style=\"font-weight: 400;\">and reload your website to see if it works.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><b>4. Check if your database server is down<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">A database server can <\/span><b>experience downtime<\/b><span style=\"font-weight: 400;\"> when there is an increase in website traffic.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">If this is the case, go to your SPanel dashboard to access <\/span><b>phpMyAdmin<\/b><span style=\"font-weight: 400;\"> and connect the database.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">If you can connect, then we need to make sure that you have<\/span><b> enough database user permission.&nbsp;<\/b><\/p>\n\n\n\n<p><b>To check this, create a new file and paste the following code into it:<\/b><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" width=\"475\" height=\"206\" src=\"https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2021\/05\/2021-05-10_13h49_38.png\" alt=\"Fix WordPress Database Connection Error, 4. Check if your database server is down\" class=\"wp-image-1526\" srcset=\"https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2021\/05\/2021-05-10_13h49_38.png 475w, https:\/\/www.scalahosting.com\/kb\/wp-content\/uploads\/2021\/05\/2021-05-10_13h49_38-300x130.png 300w\" sizes=\"(max-width: 361px) 660px, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 910px, 1140px\" \/><\/figure><\/div>\n\n\n\n<p><span style=\"font-weight: 400;\">Replace the username and password with the correct credentials and <\/span><b>upload the file to your website using FTP<\/b><span style=\"font-weight: 400;\"> and access your website via your browser.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">If your user has sufficient permission, the <\/span><b>script will connect successfully.&nbsp;<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">If this is the case, it simply means that something else is wrong.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Conclusion<\/b><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">If you still encounter an error after trying any of these steps, your final resort is to <\/span><b>reach out to your hosting provider<\/b><span style=\"font-weight: 400;\"> to ask for help. At <\/span><b>Scalahosting<\/b><span style=\"font-weight: 400;\">, we offer around-the-clock support services to help you out with your hosting needs.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Feel free to reach out to our support team or leave us a feedback. If any of the above-listed steps worked for you, we would also love to <\/span><b>hear your feedback.<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a website owner or administrator, seeing an \u201cerror establishing a Database Connection\u201d on your website can send you into panic mode, especially if you\u2019ve never encountered this before. If you are in this situation right now, you have nothing to worry about because you can fix this error without any permanent damage to your [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_seopress_robots_primary_cat":"none","_seopress_titles_title":"Fix WordPress Database Connection Error | ScalaHosting KB","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":""},"categories":[27,10],"tags":[],"class_list":["post-1518","post","type-post","status-publish","format-standard","hentry","category-vps-hosting","category-billing-and-sales"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts\/1518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/comments?post=1518"}],"version-history":[{"count":10,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts\/1518\/revisions"}],"predecessor-version":[{"id":5840,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts\/1518\/revisions\/5840"}],"wp:attachment":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/media?parent=1518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/categories?post=1518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/tags?post=1518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}