Troubleshooting ‘Error establishing a database connection’ in WordPress

Solution 1:

The “Error establishing a database connection” usually means that your database credentials are incorrect. Check your:

  • Database name
  • Username
  • Password
  • Host

Refer to the WordPress Codex: Common WordPress Errors – Error Establishing Database Connection

Tips:

  • Ensure localhost is the correct host. If you are using MAMP or WAMP, the host may differ.
  • Use Adminer (http://www.adminer.org/
    ) to find your database credentials and manage the database.
  • If you have shell access, try:
mysql> SHOW DATABASES;

This lists all databases and helps verify the database name.

To get server and port information:

mysqlserverinfo --server=root:pass@localhost -d --format=vertical

Solution 2:

I was using MAMP for the MySQL server and used its phpMyAdmin interface to create the WordPress database and assign a user with a password.

Initially, the new database user had % in the “Host” column of phpMyAdmin, which is meant to allow connections from any host. However, this caused issues during WordPress setup.

I resolved it by going to Login Information under the “User” tab in phpMyAdmin and changing the host from % to localhost.

After this change, WordPress was able to complete the installation successfully.

Solution 3:

I tried many different approaches without success. I was running MySQL v8.0.12, and noticed that under Users and Privileges, the user I created had the password validation type set to a sha2 option.

After numerous attempts, I resolved the issue by uninstalling and reinstalling MySQL, making sure to select the option to maintain compatibility with previous MySQL versions during setup.

This ensured that passwords were stored in a format compatible with WordPress, allowing it to successfully connect to the MySQL database.