This could be awfully frustrating specially when it happened on its own without you changing anything. This post will show you how to fix the error establishing a database connection.
“Before you make any database changes, make sure you have sufficient backups.“
Why do I get this error?
WordPress is unable to establish a database connection. Maybe your database login credentials are wrong or have been changed. It could be that your database has been corrupted.
Don’t miss Out -> How to switch back to the classic editor
Check up list
- make sure that you are getting the same error on both the front-end of the site, and the back-end of the site (wp-admin).
- error messages is the same on website pages
If you are getting a different error on the wp-admin for instance something like:
“One or more database tables are unavailable. The database may need to be repaired”
, then you need to repair your database. You can do this by adding the following line in your wp-config.php file
define(
'WP_ALLOW_REPAIR'
, true);
Once you have done that, you can see the settings by visiting this page: http://www.yoursite.com/wp-admin/maint/repair.php
Checking the WP-Config file
WP-Config.php is probably the single most important file in your entire WordPress installation. This is where you specify the details for WordPress to connect your database. If you changed your root password, or the database user password, then you will need to change this file as well. First thing you should always check is if everything in your wp-config.php file is the same.
define('DB_NAME', 'database-name'); define('DB_USER', 'database-username'); define('DB_PASSWORD', 'database-password'); define('DB_HOST', 'localhost'); |
Remember your DB_Host value might not always be localhost. Depending on the host, it will be different. For popular hosts like HostGator, BlueHost, SiteGround, it is localhost. You can find other host values here.
Some folks suggested that they fixed their problem by replacing localhost with the IP. It is common to see this sort of issue when running WordPress on a local server environment. For example on MAMP, the DB_Host value when changed to the IP may seem to work.
1 | define( 'DB_HOST' , '127.0.0.1:8889' ); |
IP’s will vary for online web hosting services.
If everything in this file is correct (make sure you check for typos), then it is fair to say that there is something wrong on the server end.
Check your Web Host (MySQL Server)
Create a new file called testconnection.php and paste the following code in it:
<?php $link = mysqli_connect('localhost', 'username', 'password'); if (!$link) { die('Could not connect: ' . mysqli_error()); } echo 'Connected successfully'; mysqli_close($link); ?> |
Source: www.wpbeginner.com
That’s it for now.
If you liked this article, then please subscribe to my YouTube Channel for video tutorials.
Great. It worked for me. Thanks
Awesome, very good news!