【MySQL】Establishing a Database Connection

Posted by 西维蜀黍 on 2020-03-29, Last Modified on 2021-09-21

Problem

Are you seeing the ‘Error establishing a database connection’ notice on your WordPress website? It is a fatal error that makes your WordPress website inaccessible to the users.

This error occurs when WordPress is unable to make a connection to the database. A number of things can affect your WordPress database connection which makes it a bit difficult for beginners to troubleshoot.

Solution

Check Point 1 - Check Your Database Credentials

Check Point 2 - Check Your Database Host Information

Check Point 3 - Check If Your Database Hasn’t Been Corrupted

You need to make sure that the information prodived for your appplications is correct, including the database name, username, password, and database host.

To check it, we can use ssh to access the physical machine or any virtual containers that runs your MySQL instance and use the following to check whether we can connect to MySQL manually?

$ mysql -u root -p

Potential Error

$ mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)

Reference