Warning: mysqli_connect(): (HY000/2002): No such file or directory

This is the error that we got when creating a local MySQL database. The MySQL database server was running and we were to connect to the database using PHP code.

Following is the code that we tried:

Connect MySQL using PHP

if (mysqli_connect_errno($connectionObject)) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();


In the above case, we found out that the host ‘localhost’ causes this issue. It could not connect to the MySQL database using the host as ‘localhost’.

We had to use to point to the local machine.

Most of the time and localhost are considered to be the same. But in this case, it seemed different.