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
$connectionObject=mysqli_connect("localhost","username","password","database"); if (mysqli_connect_errno($connectionObject)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
Solution:
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 127.0.0.1 to point to the local machine.
Most of the time 127.0.0.1 and localhost are considered to be the same. But in this case, it seemed different.