WordPress Error Establishing a Database Connection
When the website cannot connect to the database then this error occurs. Sometimes the server is unable to load correct data and becomes unresponsive. This error is called “Error Establishing a Database Connection“.
How do I fix “Error Establishing a Database Connection”?
WordPress database errors are usually caused by database details in the configuration file being incorrect. You will probably see a notice like “Error establishing a database connection” when this occurs. The easiest way to check this is to open your FTP client, I prefer FileZilla, and connecting to your site. Navigate to your root directory and find your wp-config.php file. Change the following values in that file to reflect the settings for your database. If you are not sure about these settings you can always check with your web host.
/** MySQL database name */ define('DB_NAME', 'your database name'); /** MySQL database username */ define('DB_USER', 'your database username'); /** MySQL database password */ define ('DB_PASSWORD', 'your database password'); /** MySQL hostname */ define('DB_HOST', 'your database username');
Still, having issues?
If you are sure that your database details are correct in the configuration file then your issue may be caused by some kind of server issue or corrupt database. What you can do to check this is to go to the main page of your site and check if you get the same error if you go to /wp-admin. If you see a message along the lines of “One or more database tables are unavailable. The database may need to be repaired” then there is something you can try to get this fixed. You need to add the following line to your wp-config.php file, add the code anywhere there is an empty line.
define('WP_ALLOW_REPAIR', true);
Now reload the /wp-admin page and it should present you with the option to repair the database. Click repair and let it run until finished and check if this solves your issue. You may want to remove the above code after you are done.
The advanced way to fix this error (don’t do it if you’re a newbie):
A few years ago, I ran into this problem after I accidentally removed the URL from the settings (it happens to everyone).
I fixed the problem by logging into phpMyAdmin (search it in the cPanel). If you have more sites installed, search for your site’s database (the one with the error), and in your Database go to the table WP_OPTIONS (If you don’t know what is the name of the database for that website, go to WP-CONFIG.PHP and you’ll find the database name there).
When in here click browse, this lists all the information.
You will notice the site URL value is blank.
Click the edit button (it looks like a pencil) and re-input your URL into the Value text area and click save.