How to Fix Error Establishing a Database Connection in WordPress
When you have finished migrating your WordPress site, unexpectedly an “Error establishing a database connection” appears. This is a fatal error that makes your site inaccessible to users.
This error appears because WordPress is unable to establish a connection to the database. If you don’t know how to fix it, you can read this article to the end.
In this article, we will show you how to fix error establishing a database connection in WordPress.
But before that, you need to know the cause of error establishing a database connection.
This problem can arise due to incorrect database information in your WordPress configuration, corrupted database, or unresponsive database server.
WordPress uses a database to store all website content and data. Then, it connects to the database every time someone visits your website.
And the information that WordPress needs to connect to the database is:
- Database name
- Database username
- Database password
- Database servers
You can find this information in the wp-config.php file in the WordPress configuration.
If any of these items are incorrect, then WordPress will fail to connect to the database server, resulting in an Error establishing a database connection.
Here are some methods you can use to fix database connection errors in WordPress.
Method 1: Check Database Login Credentials
The most common error that occurs is incorrect credentials when entering database login details into the wp-config.php file.
Now the first thing you have to do is check the database login credentials are correct or not.
You can access this wp-config.php file via cPanel hosting or using an FTP client.
If using cPanel, find and open File Manager.
Then select ‘public_html’ on the left, on the right find and open the ‘wp-config.php’ file.
Note: The location of your website root directory (public_html) may be different. So navigate according to the website directory that you have.
Next, you can find the following lines in the wp-config.php file.
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );
Verify that the information for the database name, username, password, and database host is correct.
If you are still unsure about this database information, you can view complete database information via cPanel.
In the cPanel under Databases, click MySQL Databases.
You will be directed to the database management page. From this page, you can find out the database name and username.
At the bottom of the page. You will find the database user and a link to change the password.
Upon completion confirm the database name, username and password. You can change that information in the wp-config.php file if needed.
After that, try visiting your website to see if the database connection error has disappeared.
Method 2: Repair The WordPress Database
If you see a different error in wp-admin for example “One or more database tables are unavailable. The database may need to be repaired”, then you need to repair your database.
You can resolve this error by adding the shortcode to the wp-config.php file. Be sure to add just before “that’s all, stop editing! Happy blogging”.
Once done, you can view the settings by visiting the page
Note: You do not need to login to access the Repair Database page. After finishing repairing the database, make sure you remove this code from wp-config.php.
At this point, the “Error establishing a database connection” should have been resolved and your website can be accessed again.
We hope this article helped you learn how to fix database connection errors during WordPress migration. You may also want to see our post on how to remove categories from WordPress URLs.