search

Home  >  Q&A  >  body text

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

<p>I'm trying to install a regular forum on my Mac, for which I just created a database and a user from the MySQL command line: </p> <pre class="brush:php;toolbar:false;">mysql> CREATE DATABASE vanilla; Query OK, 1 row affected (0.00 sec) mysql> create user 'vanilla_user3'@'localhost' IDENTIFIED BY 'vanilla_password'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT ALL PRIVILEGES ON * . * TO 'vanilla_user3'@'localhost'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)</pre> <p>So I tried to connect using the following code: </p> <pre class="brush:php;toolbar:false;">$con=mysqli_connect("localhost","vanilla_user3","vanilla_password","vanilla"); if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }</pre> <p>But unfortunately, I got an error message</p> <blockquote> <p>Warning: mysqli_connect(): (HY000/2002): No such file or directory in /Users/kramer65/Sites/vanilla/info.php on line 3 Unable to connect to MySQL: No such file or directory</p> </blockquote> <p>Do you know where I went wrong? </p>
P粉693126115P粉693126115462 days ago526

reply all(2)I'll reply

  • P粉133321839

    P粉1333218392023-08-28 16:05:31

    I encountered the same problem, but the problem was related to the php.ini file.

    I had to edit these two lines in /etc/php.ini (or wherever your php.ini is):

    mysql.default_socket = /tmp/mysql.sock
    mysqli.default_socket = /tmp/mysql.sock

    Restart the apache server to ensure changes are reflected.

    sudo apachectl restart

    reply
    0
  • P粉320361201

    P粉3203612012023-08-28 13:51:31

    Okay, I just found the solution. It turns out that the problem is that the host should not be localhost, but 127.0.0.1. I always thought localhost and 127.0.0.1 were the same, but the results are different.

    So, maybe as a tip to future users, always use IP when in doubt.

    reply
    0
  • Cancelreply