I am having a hard time understanding why PHP cannot connect with localhost as a host. I can connect by doing:

mysql -uroot -p

And also, through Sequel Pro I can connect with localhost as a host. But with PHP, I get:

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

I am sure it's because of the sockets, because If I try to change to it works.

Why doesn't host work?

  • 1
    It might help if you showed exactly what you are doing, and not just the error message.  Please do not respond in comments; edit your question to make it clearer and more complete.
    – Scott
    Apr 21 '16 at 21:40

PHP is trying to use different socket then the one on which MySQL is listening.

To see on which socket is MySQL listening, run:

 mysqld --verbose --help|grep ^socket

socket /var/run/mysqld/mysqld.sock

To see what socket PHP tries to use, you can check output of script <?php phpinfo(); ?>, it is variable mysqli.default_socket. You can change it in your php.ini file (usually in /etc/php5/apache2/).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.