{"id":56153,"date":"2020-07-08T11:54:07","date_gmt":"2020-07-08T16:54:07","guid":{"rendered":"https:\/\/blog.cpanel.com\/?p=56153"},"modified":"2020-07-08T11:54:07","modified_gmt":"2020-07-08T16:54:07","slug":"how-to-fix-error-establishing-a-database-connection-in-wordpress","status":"publish","type":"post","link":"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/how-to-fix-error-establishing-a-database-connection-in-wordpress\/","title":{"rendered":"How To Fix “Error Establishing a Database Connection” in WordPress"},"content":{"rendered":"\n
The \u201cError establishing a database connection\u201d message strikes fear in a WordPress users heart, prompting many a panicked support request. You try to load a page, but all you see is a white box with a mysterious error message. WordPress is down and the \u201chelpful\u201d suggestions beneath the error are more confusing than useful.<\/p>\n\n\n\n
How can you fix a database error when you can\u2019t even open the admin dashboard to see what\u2019s wrong?<\/p>\n\n\n\n
Fortunately, \u201cError Establishing a Database Connection\u201d is straightforward to put right if you know what\u2019s going on. Let\u2019s take a look at WordPress\u2019s relationship with its database, which is called MySQL\u00ae<\/sup>, what can go wrong, and, most importantly, what you can do to put WordPress and MySQL back on speaking terms.<\/p>\n\n\n\n A WordPress site is composed of two parts: the files and the database.<\/p>\n\n\n\n When you download WordPress, there are static files and code files written in PHP, but you won\u2019t find pages and posts, usernames and passwords, comments, or configuration data \u2014 that\u2019s all stored in a MySQL database.<\/p>\n\n\n\n MySQL is a database server: it manages databases, tables, and the data stored in them. WordPress communicates with MySQL over the network, authenticating with a username and password before sending an SQL query that tells the database what to do.<\/p>\n\n\n\n For example, loading the WordPress home page executes a PHP file \u2014 usually the home.php or index.php file, depending on how the site is configured. The code connects to the database and retrieves the home page\u2019s content, slotting it into a template that generates an HTML document.<\/p>\n\n\n\n A connection error is WordPress\u2019s way of telling you that it can\u2019t get the information it needs to build the page you\u2019re trying to load. WordPress asked for data, and the database ignored it or said no. To fix the error, we have to figure out why MySQL is misbehaving.<\/p>\n\n\n\n There are several possible causes:<\/p>\n\n\n\n In summary, WordPress database errors usually mean WordPress can\u2019t connect to or authenticate with the database or the database can\u2019t respond with the right information.<\/p>\n\n\n\n It can be challenging to identify the error\u2019s cause, so the best approach is to try the most likely fixes until your WordPress site starts working again.<\/p>\n\n\n\n WordPress uses four pieces of information to talk to MySQL: <\/p>\n\n\n\n They are stored as variables in the wp-config.php file in the WordPress installation\u2019s root directory, and they look like this:<\/p>\n\n\n\n \/** MySQL database username *\/ The first step in verifying your database credentials is comparing the values listed in the wp-config.php file to those listed in the database. Open phpMyAdmin in cPanel and find the WordPress database in the left menu. Make sure the database name and the DB_NAME variable in wp-config.php match. Also, verify that the hostname matches the name in the database server section: it will almost certainly be \u201clocalhost.\u201d<\/p>\n\n\n\n Next, check the \u201cUsers\u201d section within the database entry to ensure that database\u2019s username and password match the values WordPress is using. You can edit the values in the database to make them match those in wp-config.php or edit values in wp-config.php directly using the cPanel File Manager\u2019s HTML Editor<\/a>.<\/p>\n\n\n\n If the database credentials check out but the error persists, it\u2019s time to consider file or database corruption.<\/p>\n\n\n\n Corrupt files are more likely, but minor database corruption is easier and less risky to fix so it\u2019s worth trying first. There are a couple of ways to go about WordPress database repair. The first is to select phpMyAdmin in cPanel, navigate to your database, select all the tables, and click on the \u201crepair table\u201d item in the dropdown menu.<\/p>\n\n\n\n The second method takes advantage of WordPress\u2019s built-in database repair mode, which you can activate by adding the following line to the wp-config.php file:<\/p>\n\n\n\n define(‘WP_ALLOW_REPAIR’, true);<\/strong><\/em> Next, navigate to this URL:<\/p>\n\n\n\n https:\/\/yourdomain.com\/wp-admin\/maint\/repair.php<\/p>\n\n\n\n Choose the repair option and wait while WordPress does its thing. It might take a while but should handle most common database corruption issues. Be sure to remove the line of code you added to wp-config.php when you have finished. Anyone can access the database repair URL and you don\u2019t want it triggered by malicious visitors.<\/p>\n\n\n\n WordPress\u2019s files can be corrupted by hardware problems, malware, or user errors. The goal here is to replace corrupt files with new ones freshly downloaded from WordPress.org while leaving your site\u2019s plugins and themes in place.<\/p>\n\n\n\n Download a copy of WordPress<\/a>, unzip it, and navigate to the \u201cwordpress\u201d directory. Delete the wp-content folder and the wp-config.php and wp-config-sample.php files.<\/p>\n\n\n\n Open the affected WordPress site\u2019s root folder in cPanel File Manager and upload all the remaining files and folders from your local copy of WordPress. If cPanel asks whether to overwrite existing files, say yes.<\/p>\n\n\n\n All of WordPress\u2019s core files will be replaced with pristine new files. If this doesn\u2019t resolve the database error, you may want to try replacing wp-config.php too, in which case you will have to edit the new version to add the correct database credentials, as discussed above.<\/p>\n\n\n\n If none of these methods resolves the error, it is time to restore the WordPress site\u2014both the files and the database\u2014from a recent backup that was made before the connection error occurred.<\/p>\n\n\n\n It can be frightening when you first see \u201cError Establishing a Database Connection\u201d on your WordPress site, but hopefully you now understand what the error means and the steps you can take to restore harmony between WordPress and its database. <\/p>\n\n\n\n If you have any further questions about fixing WordPress database errors with cPanel or wish to discuss anything cPanel related, please join us on our official Discord channel<\/a>, our official cPanel subreddit<\/a>, or our Support Forum<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":" The \u201cError establishing a database connection\u201d message strikes fear in a WordPress users heart, prompting many a panicked support request. You try to load a page, but all you see is a white box with a mysterious error message. WordPress is down and the \u201chelpful\u201d suggestions beneath the error are more confusing than useful. How […]<\/p>\n","protected":false},"author":77,"featured_media":65429,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[61],"tags":[],"class_list":["post-56153","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips-and-tricks"],"acf":[],"yoast_head":"\nWhy Does WordPress Need a Database?<\/strong><\/h2>\n\n\n\n
What is a WordPress Database Connection Error?<\/strong><\/h2>\n\n\n\n
How Do I Fix Database Connection Errors in WordPress?<\/strong><\/h2>\n\n\n\n
Verify Database Credentials in cPanel<\/strong><\/h3>\n\n\n\n
define(‘DB_USER’, ‘xxxxxx’);<\/em>
<\/strong><\/p>\n\n\n\nRepair the Database<\/strong><\/h3>\n\n\n\n
<\/p>\n\n\n\nReplace Corrupt WordPress Files<\/strong><\/h3>\n\n\n\n