{"id":9186,"date":"2014-11-01T01:00:52","date_gmt":"2014-11-01T07:00:52","guid":{"rendered":"http:\/\/sanuja.com\/blog\/?p=9186"},"modified":"2020-05-29T17:57:35","modified_gmt":"2020-05-29T23:57:35","slug":"how-to-fix-wp-error-establishing-a-database-connection","status":"publish","type":"post","link":"https:\/\/sanuja.com\/blog\/how-to-fix-wp-error-establishing-a-database-connection","title":{"rendered":"How to fix WP Error Establishing a Database Connection"},"content":{"rendered":"<p>The <b>Error Establishing a Database Connection<\/b> error is a very common issue on WordPress. This issue is caused by a connection failure between the WordPress CMS and the SQL database. Here is a simple guide to fix the problem.<\/p>\n<p><figure id=\"attachment_9187\" aria-describedby=\"caption-attachment-9187\" style=\"width: 564px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" data-attachment-id=\"9187\" data-permalink=\"https:\/\/sanuja.com\/blog\/how-to-fix-wp-error-establishing-a-database-connection\/db_connect_error\" data-orig-file=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/db_connect_error.jpg\" data-orig-size=\"564,90\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"db_connect_error\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;A typical error message displayed on site.&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/db_connect_error-300x47.jpg\" data-large-file=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/db_connect_error.jpg\" src=\"http:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/db_connect_error.jpg\" alt=\"A typical error message displayed on site.\" width=\"564\" height=\"90\" class=\"size-full wp-image-9187\" srcset=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/db_connect_error.jpg 564w, https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/db_connect_error-300x47.jpg 300w\" sizes=\"(max-width: 564px) 100vw, 564px\" \/><figcaption id=\"caption-attachment-9187\" class=\"wp-caption-text\">A typical error message displayed on site.<\/figcaption><\/figure><!--more--><\/p>\n<p><b>Finding the cause<\/b><\/p>\n<p>This can be caused by several issues. If you are getting the same error, &#8220;Error Establishing a Database Connection&#8221;, on the main front page as well as the wp-admin section, then the problem is most likely the credentials. This is the most common reason for this issue. However, if the error message on the wp-admin section is different from that of the front page, then it is most likely caused by a <a href=\"#rp\">database corruption<\/a>. I will explain several methods to fix the problem.<\/p>\n<p><b>Fix wp-config file<\/b><\/p>\n<p>From the back-end of your server, open the wp-config.php file in an editor. If you are using cPanel, then you can do this from file manager. If you have your own server, you can access the file either using FTP or remote desktop connection. Check if the following parameters are setup properly in wp-config.php.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">define('DB_NAME', 'database_name');\r\ndefine('DB_USER', 'database_username');\r\ndefine('DB_PASSWORD', 'database_password');\r\ndefine('DB_HOST', 'localhost');<\/pre>\n<p>The information in  wp-config.php file should match the corresponding current database user account. These accounts should be listed under under MySQL Databases section of your server. On cPanel it is located under Databases sub section.<\/p>\n<figure id=\"attachment_9190\" aria-describedby=\"caption-attachment-9190\" style=\"width: 332px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" data-attachment-id=\"9190\" data-permalink=\"https:\/\/sanuja.com\/blog\/how-to-fix-wp-error-establishing-a-database-connection\/mysql_dbs\" data-orig-file=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/MySQL_dbs.jpg\" data-orig-size=\"332,113\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"MySQL_dbs\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;MySQL in cPanel&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/MySQL_dbs-300x102.jpg\" data-large-file=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/MySQL_dbs.jpg\" src=\"http:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/MySQL_dbs.jpg\" alt=\"MySQL in cPanel\" width=\"332\" height=\"113\" class=\"size-full wp-image-9190\" srcset=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/MySQL_dbs.jpg 332w, https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/MySQL_dbs-300x102.jpg 300w\" sizes=\"(max-width: 332px) 100vw, 332px\" \/><figcaption id=\"caption-attachment-9190\" class=\"wp-caption-text\">MySQL in cPanel<\/figcaption><\/figure>\n<p>Typically the database name is composed of your username_databaseName. All the current databases and users will be listed within the MySQL admin page (example shown below).<\/p>\n<figure id=\"attachment_9192\" aria-describedby=\"caption-attachment-9192\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" data-attachment-id=\"9192\" data-permalink=\"https:\/\/sanuja.com\/blog\/how-to-fix-wp-error-establishing-a-database-connection\/mysql_dbs_list\" data-orig-file=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/MySQL_dbs_list.jpg\" data-orig-size=\"600,404\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"MySQL_dbs_list\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;List of current databases and users.&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/MySQL_dbs_list-300x202.jpg\" data-large-file=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/MySQL_dbs_list.jpg\" src=\"http:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/MySQL_dbs_list.jpg\" alt=\"List of current databases and users.\" width=\"600\" height=\"404\" class=\"size-full wp-image-9192\" srcset=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/MySQL_dbs_list.jpg 600w, https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/MySQL_dbs_list-300x202.jpg 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><figcaption id=\"caption-attachment-9192\" class=\"wp-caption-text\">List of current databases and users.<\/figcaption><\/figure>\n<p>If the exact same username appears in the MySQL current list as it is in the wp-config, then it may be the password. You cannot check what password is used for each user account. But you can change the current password. To do that, under MySQL Users &#8211;> Add New User, enter the current username, Then enter a password you prefer. This will update the existing account.<\/p>\n<p><figure id=\"attachment_9193\" aria-describedby=\"caption-attachment-9193\" style=\"width: 438px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" data-attachment-id=\"9193\" data-permalink=\"https:\/\/sanuja.com\/blog\/how-to-fix-wp-error-establishing-a-database-connection\/mysql_dbs_user\" data-orig-file=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/MySQL_dbs_user.jpg\" data-orig-size=\"438,232\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"MySQL_dbs_user\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;Updating the password for a user.&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/MySQL_dbs_user-300x158.jpg\" data-large-file=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/MySQL_dbs_user.jpg\" src=\"http:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/MySQL_dbs_user.jpg\" alt=\"Updating the password for a user.\" width=\"438\" height=\"232\" class=\"size-full wp-image-9193\" srcset=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/MySQL_dbs_user.jpg 438w, https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/MySQL_dbs_user-300x158.jpg 300w\" sizes=\"(max-width: 438px) 100vw, 438px\" \/><figcaption id=\"caption-attachment-9193\" class=\"wp-caption-text\">Updating the password for a user.<\/figcaption><\/figure><br \/>\n<a name=\"rp\"><\/a><\/p>\n<p><b>Fix corrupted database<\/b><\/p>\n<p>As mention above, if the error message on the wp-admin section is different from &#8220;Error Establishing a Database Connection&#8221;, then it is caused by a database corruption. WordPress comes with a built in repair method in repair.php. First add the following to the top of wp-config.php file. <\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">define('WP_ALLOW_REPAIR', true);<\/pre>\n<p>Then run the following file located at, <b>http:\/\/domain.com\/wp-admin\/maint\/repair.php<\/b>. It will provide you with &#8220;Repair Database&#8221; and &#8220;Repair and Optimize Database&#8221; options. Choose either one to fix the issue. Once the repair is successful (or not), please <b>delete the WP_ALLOW_REPAIR from the wp-config.php file.<\/b> With the code in place, anyone one access the repair page without administrative account.<\/p>\n<p><b>Connection to database<\/b><\/p>\n<p>This can be tested by running a simple PHP script. Create a PHP file with the following code such as test.php.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">&lt;?php\r\n$link = mysql_connect('localhost', 'username', 'password');\r\nif (!$link) {\r\ndie('Could not connect: ' . mysql_error());\r\n}\r\necho 'Connected successfully';\r\nmysql_close($link);\r\n?&gt;<\/pre>\n<p>If it provide a feedback with a successful connection, then your database is working. If not, either the database is corrupted or the sever files are damaged. Contact your hosting company for help. If it is your own server, then your best hope is to replace the MySQL files with available backup.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Error Establishing a Database Connection error is a very common issue on WordPress. This issue is caused by a connection failure between the WordPress CMS and the SQL database. Here is a simple guide to fix the problem.<\/p>\n","protected":false},"author":2,"featured_media":9192,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,34],"tags":[7,30,10],"class_list":["post-9186","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-computer-science","category-cms","tag-server","tag-sql","tag-tech"],"jetpack_featured_media_url":"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2014\/10\/MySQL_dbs_list.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts\/9186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/comments?post=9186"}],"version-history":[{"count":0,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts\/9186\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/media\/9192"}],"wp:attachment":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/media?parent=9186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/categories?post=9186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/tags?post=9186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}