If you are seeing the WordPress Error Establishing a Database Connection notice on your website, it means that you have an issue with the database connection. This issue can prevent users from accessing your website.
Whenever WordPress cannot connect to the database, it will display an error message. Many factors can affect this connection, making it difficult for new users to evaluate the cause of the problem.
We’ll cover how the “WordPress Error Establishing a Database Connection” error occurs, how to fix it, and how to troubleshoot any problems along the way.
Table of Contents
WordPress Error Establishing a Database Connection Meaning
In order to understand what the “WordPress Error Establishing a Database Connection,” error message means, it is important to understand how WordPress works.
WordPress is a content management system built using PHP and MySQL. While PHP is the programming language used to create all the functions on your WordPress site, MySQL is the database that stores all your data like content, images, video, and media files.
When a page request is made, PHP pulls content from the database and displays it in your browser. That includes images, videos, audio files, and everything else you see on the page.
The next time you see a message(Error establishing a database connection) on your WordPress website, you will know that PHP cannot access your database for some reason and cannot display the page to the user.
Reasons for WordPress Error Establishing a Database Connection
Here’s a list of some common reasons why you might encounter a WordPress error establishing a database connection:
Incorrect login credentials
WordPress needs a login and password to access your database. If you’ve changed or corrupted the login details for your database, WordPress won’t be able to access it and fetch anything from it.
Corrupted database
In your WordPress site, there are so many elements like themes, plugins, addition, and users, that there is the scope that the database can get corrupted.
Corrupted WordPress files
If WordPress files get corrupted, your website could be at risk of crashing. This can happen if you attempt to update your WordPress core files, plugins or themes and something goes wrong that prevents the update from completing.
Database server down
Sometimes your website files are hosted on a different machine than the database. If the database is down or there’s a connection problem, you get an error.
Too much traffic
While it’s great to have so many visitors, it could be a problem if your web server can’t handle the sudden surge in traffic. If that happens, your database might become unresponsive.
We recommend that you back up your WordPress site before you start troubleshooting. Many fixes involve accessing your database, so it’s best not to make things worse.
You can use a popular plugin to backup both your files and your database.
Check Your Database Login Credentials
When you cannot log into your Facebook account, you can check whether the caps lock is on or if you have misspelled your username or password. You should click’ Forgot Password’ if this fails to resolve the problem.
One of the most common reasons WordPress database errors occur is incorrect database credentials. If you have recently moved your WordPress site to a new host, this could cause your problem.
The four primary pieces of information necessary to have a seamless connection with your database are:
MySQL database name define(‘DB_NAME’, ‘name’); MySQL database username define(‘DB_USER’, ‘username’); MySQL database password define(‘DB_PASSWORD’, ‘password’); MySQL hostname (server) define(‘DB_HOST’, ‘localhost’);
You can access the wp-config.php file either through your cPanel or by using FTP to connect to your site’s root folder and access the file from there. If you are accessing the file through cPanel, right-click on the wp-config.php file and choose to edit it.
You can check the database credentials from your WordPress configuration file. Now, we will ensure that the credentials in your wp-config.php file match those stored in your database manager.
Let’s start by confirming that you have the correct database name as specified in your wp-config.php file. You can do this by accessing phpMyAdmin from your hosting cPanel. Generally, under the database tools tab, you can locate PHPMyAdmin.
After logging into phpMyAdmin, click on the Databases tab. This will query a list of all your databases. You can see the database that appears after the DB_NAME in your wp-config.php file. Is it the same as the entry in wp-config.php? If that’s the case, this isn’t the issue.
If you cannot locate the database, there are two options. The first is to contact your hosting service provider. The second is to restore the database from a recent backup or create a new one.
Once you have confirmed that your database credentials are correct, you can test the connection by creating a new .php file and saving it in the root folder of your WordPress site. Name the file anything you like—for example, test.php.
The most straightforward method is to go to your file manager, pick the root folder, and then select New File from the menu.
Right-click on the new file and choose Edit. Add the following code:
<?php $link = mysql_connect('host', 'user', 'password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Hello World'; mysql_close($link); ?>
In your wp-config.php file, replace ‘host’, ‘user’, and ‘password’ with the database hostname, username, and password that you configured on your database settings page. Once you’ve finished, save the changes.
Now type out the URL http://yourdomain.com/test.php in your browser’s address bar. If you see a ‘Hello World’ message on the screen, it means that your login credentials aren’t the problem behind the error in establishing a database connection.
If you receive an error message such as ‘Could not connect: Access denied for user yourusername@localhost’ you know that there is an issue with the database connection.
When your login credentials are incorrect, you will need to create new ones and then update your wp-config.php file. It’s quite easy to make them.
Repair WordPress Database
If the website displays an error message, such as “One or more database tables are unavailable. The database may need to be repaired,” you should repair the database by adding the following line to your wp-config.php file:
define('WP_ALLOW_REPAIR', true);
Once you’ve done that, go to this page: http://www.yoursite.com/wp-admin/maint/repair.php to see the settings.
The user does not need to log in to access the database repair page. After repairing and optimizing your database, make sure to remove this code from your wp-config.php.
Update WordPress Site URL
To change the WordPress site URL, access phpMyAdmin from your hosting account dashboard and select your WordPress database.
To do this, click the SQL menu on the top and enter the following MySQL query:
UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'
If you have changed the WordPress table prefix, update the table name in your database and provide a link to your own website.
Rebooting Web Server
Users on dedicated servers, local servers, and VPSs can reboot their systems to restart the web and database server which may fix some temporary glitches causing the error.
Replacing Core WordPress Files
To solve this, we can reinstall the core WordPress files. Whether it is caused due to an issue while transferring the core files using FTP, a rogue plugin or theme, or your core files have become corrupted, we will replace the core WordPress version on our site by downloading fresh copies from WordPress.org. We will not be touching our plugins, themes, or media in this method. Only the core WordPress installation.
You will want to take care when using this method. A small mistake could wipe out your entire site. As mentioned early on, we highly recommend you backup your site before proceeding.
Once you’ve downloaded WordPress, unzip on your computer so that you have access to the files. This process does not overwrite your existing files and folders, which means that you retain all of your themes, plugins, and media files.
Once you’ve completed all of these steps and uploaded the remaining files to your website root folder, clear your browser cache or use a different browser for testing purposes only. Once you do so, reload the page; if the problem is solved, We’ve figured out what went wrong. if it hasn’t been fixed yet, it’s time to move on to the next troubleshooting step.
Contact Your Web Hosting Company
If the performance of your WordPress site has declined recently, or if it was previously working correctly but is now experiencing problems, there is a good chance that something went wrong with your hosting provider’s server.
Hosting providers offer quick turnaround times for customer support queries.
If you notice a problem establishing a database connection, you should contact your hosting provider right once.
Most hosting providers will be able to fix the problem remotely and with little disruption to your work.
Restore to Your Latest Backup
Another method of troubleshooting is to restore the site to a previous version by reverting to a backup. After updating the site, you can check for changes that you have made since the update.
WordPress users can back up their sites using popular plugins such as Updraft Plus, BlogVault, and many others. Most hosting service providers maintain backups of all the sites they host. You could ask yours if they have a copy of the latest backup of your website.
Fix 6 Common WordPress Errors
If you’re looking for more tips on how to fix WordPress errors, we’ve got you covered. Check out our following post on common WordPress errors and how to fix them.
- How to Fix Internal Server Error 500
- How to Fix Syntax Errors in WordPress
- How to Fix The WordPress Memory Exhausted Error
- How to Fix the WordPress White Screen of Death
- How To Fix The Image Upload Issue In WordPress
- How to Fix Briefly Unavailable for Scheduled Maintenance Errors in WordPress
Final Words: WordPress Error Establishing a Database Connection
As a WordPress website owner, you will encounter a wide range of challenges from time to time. Those challenges are all part of running a website. The information in this article should enable you to resolve the “WordPress Error Establishing a Database Connection” issue that you may encounter when using WordPress. Did you find this useful? Share this on social media.