Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mysql

mysqli

I'm trying to install vanilla forums on my Mac, and for this I just created a database and a user from the MySQL command line:

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) 

So I try to connect using the following code:

$con=mysqli_connect("localhost","vanilla_user3","vanilla_password","vanilla"); if (mysqli_connect_errno($con)) {   echo "Failed to connect to MySQL: " . mysqli_connect_error(); } 

but unfortunately, I get an error saying

Warning: mysqli_connect(): (HY000/2002): No such file or directory in /Users/kramer65/Sites/vanilla/info.php on line 3 Failed to connect to MySQL: No such file or directory

Any idea where I'm going wrong?

like image 268
kramer65 Avatar asked Nov 19 '13 13:11

kramer65


People also ask

Why is mysqli_connect () used?

The mysqli_connect() function establishes a connection with MySQL server and returns the connection as an object.

Why is mysqli_connect () Used explain with the help of an example?

The mysqli_connect() function in PHP is used to connect you to the database. In the previous version of the connection mysql_connect() was used for connection and then there comes mysqli_connect() where i means improved version of connection and is more secure than mysql_connect().

What is the return of mysqli_connect?

If successful, the mysqli_connect() will return an object representing the connection to the database, or FALSE on failure. The username and password parameters specify the username and password under which to connect to the MySQL server.


2 Answers

Alright, I just found the solution. The problem turned out to be that the host shouldn't have been localhost, but 127.0.0.1. I always thought localhost and 127.0.0.1 was the same, but it turned out to be different.

So maybe as a tip for future users, always use the ip when in doubt.

like image 191
kramer65 Avatar answered Sep 17 '22 18:09

kramer65


I had the same problem but the issue was something related to php.ini file.

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

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

Restart apache server to make sure the changes are reflected.

sudo apachectl restart 
like image 38
mohitmayank Avatar answered Sep 16 '22 18:09

mohitmayank