# php – Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in

## The Question :

246 people think this question is useful

I’m trying to connect to my MySQL DB with the Terminal on my Apple (With PHP).

Yesterday it worked fine, and now I suddenly get the error in the title.

The script works when I use my browser to run it (I have XAMPP installed), but Terminal refuses to connect to the DB.

Here is the file that I include to connect (the script works when I don’t include this, but then it doesn’t connect to the DB):

<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("FNB1C_data") or die(mysql_error());
?>



That should work, since it works with my browser.

The command I use at the Terminal is php scriptname.php.

418 people think this answer is useful

For some reason mysql on OS X gets the locations of the required socket file a bit wrong, but thankfully the solution is as simple as setting up a symbolic link.

You may have a socket (appearing as a zero length file) as /tmp/mysql.sock or /var/mysql/mysql.sock, but one or more apps is looking in the other location for it. Find out with this command:

ls -l /tmp/mysql.sock /var/mysql/mysql.sock



Rather than move the socket, edit config files, and have to remember to keep edited files local and away from servers where the paths are correct, simply create a symbolic link so your Mac finds the required socket, even when it’s looking in the wrong place!

If you have /tmp/mysql.sock but no /var/mysql/mysql.sock then…

cd /var
sudo mkdir mysql
sudo chmod 755 mysql
cd mysql
sudo ln -s /tmp/mysql.sock mysql.sock



If you have /var/mysql/mysql.sock but no /tmp/mysql.sock then…

cd /tmp
ln -s /var/mysql/mysql.sock mysql.sock



You will need permissions to create the directory and link, so just prefix the commands above with sudo if necessary.

373 people think this answer is useful

I also had this error, but could only fix it through the suggestion here.

To summarize, use:

127.0.0.1



localhost



The reason is that “localhost” is a special name for the MySQL driver making it use the UNIX socket to connect to MySQL instead of the a TCP socket.

25 people think this answer is useful

I was having the same problem and this is how I fixed it:

I had this and it didn’t work:

$con = mysql_connect('localhost', 'root', '1234');  I did this and it worked: $con = mysql_connect(':/Applications/MAMP/tmp/mysql/mysql.sock', 'root', '1234');



Instead of using the mysql server, I connected directly to the Unix Socket. Worked for me.

22 people think this answer is useful

MySQL socket is located, in general, in /tmp/mysql.sock or /var/mysql/mysql.sock, but probably PHP looks in the wrong place.

1. Check where is your socket with:

 sudo /usr/libexec/locate.updatedb


2. When the updatedb is terminated:

 locate mysql.sock



 php -i | grep php.ini



this will output something like:

 Configuration File (php.ini) Path => /opt/local/etc/php54



 sudo vim /opt/local/etc/php54/php.ini


5. Change the lines:

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



where /tmp/mysql.sock is the path to your socket.

6. Save your modifications and exit ESC + SHIFT: x

7. Restart Apache

 sudo apachectl stop
sudo apachectl start



12 people think this answer is useful

I am on XAMPP on Mac OS X, and Brian Lowe’s solution above worked with a slight modification.

The mysql.sock file is actually in “/Applications/xampp/xamppfiles/var/mysql/” folder. So had to link it up both in /tmp and /var/mysql. I haven’t checked which one is used by PHP command line, but this did the fix, so I am happy 🙂

sudo su
ln -s /Applications/xampp/xamppfiles/var/mysql/mysql.sock /tmp/mysql.sock
mkdir /var/mysql
ln -s /Applications/xampp/xamppfiles/var/mysql/mysql.sock /var/mysql/mysql.sock



10 people think this answer is useful

Mac OS X EL Capitan + MAMP Pro Do this

cd /var
sudo mkdir mysql
sudo chmod 755 mysql
cd mysql
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock mysql.sock



Then do this

cd /tmp
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock mysql.sock



Hope this saves you some time.

6 people think this answer is useful

The reason is that php cannot find the correct path of mysql.sock.

Then, please confirm that which path is the mysql.sock located, for example /tmp/mysql.sock

then add this path string to php.ini:

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

Finally, restart Apache.

6 people think this answer is useful

When you face the following issue:

PHP throwing error “Warning: mysql_connect() http://function.mysql-connect: 2002 No such file or directory (trying to connect via unix:///tmp/mysql.sock)”

Set “mysql.default_socket” value in your /etc/php.ini to

 "mysql.default_socket = /var/mysql/mysql.sock".



Then restart web service in server admin

4 people think this answer is useful

Fix the looming 2002 socket error – which is linking where MySQL places the socket and where OSX thinks it should be, MySQL puts it in /tmp and OSX looks for it in /var/mysql the socket is a type of file that allows mysql client/server communication.

sudo mkdir /var/mysql



and then

sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock



1 people think this answer is useful

Another solution is to fix the socket location in the php.ini configuration file like this:

pdo_mysql.default_socket=/tmp/mysql.sock



Of course, the symlink works too, so its a matter of preference which one you change.

1 people think this answer is useful

When you install php53-mysql using port it returns the following message which is the solution to this problem:

To use mysqlnd with a local MySQL server, edit /opt/local/etc/php53/php.ini
and set mysql.default_socket, mysqli.default_socket and
pdo_mysql.default_socket to the path to your MySQL server's socket file.

For mysql5, use /opt/local/var/run/mysql5/mysqld.sock
For mysql51, use /opt/local/var/run/mysql51/mysqld.sock
For mysql55, use /opt/local/var/run/mysql55/mysqld.sock
For percona, use /opt/local/var/run/percona/mysqld.sock



1 people think this answer is useful

i was having the same issue

[PDOException] SQLSTATE[HY000] [2002] No such file or directory

[ErrorException] Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in …htdocs/Symfony/vendor/doctrine-dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php

So the solution is to make a symlink to the sock file thus resolving the issue. Do the following to resolve it:

$sudo mkdir /private/var/mysql/$ sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /private/var/mysql/mysql.sock

0 people think this answer is useful

I got the same errors. Mysql was running as a standalone application before I started phpMyAdmin.

I just stopped mysql Then sudo /Applications/XAMPP/xamppfiles/xampp stop sudo /Applications/XAMPP/xamppfiles/xampp start

It worked fine

0 people think this answer is useful

I just had this problem, but it only appeared when loading certain pages (other pages worked fine). It turned out that I was making calls to MySQL after I closed the connection with mysql_close(). So, as @brucenan said: make sure that MySQL is running when you call it.

0 people think this answer is useful

You can do it by simply aliasing the MAMP php on Apple terminal:

alias phpmamp='/Applications/MAMP/bin/php/php7.0.0/bin/php'



Example: > phpmamp - v

Now you can run something like: > phpmamp scriptname.php

Note: This will be applied only for the current terminal session.

0 people think this answer is useful

Since your might use MAMP, either change your Port to the default 3306 or use 127.0.0.1 in the database.php

$db['default'] = array( 'dsn' => '', 'hostname' => 'localhost',// leave it for port 3306 'username' => 'yourUserhere', 'password' => 'yourPassword', 'database' => 'yourDatabase', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE );  Or with the default settings: $db['default'] = array(
'dsn'   => '',
'hostname' => '127.0.0.1:8889',// leave it for port 8889
'database' => 'yourDatabase',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);



0 people think this answer is useful

The mySQL client by default attempts to connect through a local file called a socket instead of connecting to the loopback address (127.0.0.1) for localhost.

The default location of this socket file, at least on OSX, is /tmp/mysql.sock.

QUICK, LESS ELEGANT SOLUTION

Create a symlink to fool the OS into finding the correct socket.

ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp



PROPER SOLUTION

Change the socket path defined in the startMysql.sh file in /Applications/MAMP/bin.