Vous êtes sur la page 1sur 5

php - Warning: mysql_connect(): [2002] No such file or direc...

http://stackoverflow.com/questions/4219970/warning-mysql-...

help

Warning: mysql_connect(): [2002] No such file or directory (trying to connect via


unix:///tmp/mysql.sock) in

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 this error (See title). I have no clue how to solve it. I've
spend all my free time trying today =(
The script works when I use my Browser to run it (I have XAMPP installed), but Terminal refuses to connect
to the DB.
Her eis the file that I include to connect (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 be right since with my Browser I can connect when I run the script.
The command I use is "php scriptname.php".
php

mysql

database

edited Nov 18 '10 at 22:12

asked Nov 18 '10 at 21:53


Nr 28
611 2 6 4

Please show some code and the command line you use to start the script Pekka Nov 18 '10 at 21:55

11 Answers
For some reason mysql on OS X gets the locations of the required socket file a bit wrong, but thanks to a
veritable wizard I know (that's you, Gavin) 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 1
or more apps is looking in the other location for it.
Rather than move the socket and have to edit config files and 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
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.
Hope this helps. It has sorted this exact issue for me on 3 macs so far.
edited Nov 29 '13 at 21:16
Jared Burrows
5,312 2 30 50

1 de 5

answered Aug 31 '11 at 23:02


Brian Lowe
2,556 1 5 9

03/12/14 20:08

php - Warning: mysql_connect(): [2002] No such file or direc...

http://stackoverflow.com/questions/4219970/warning-mysql-...

Good answer. Fix worked for me. Do you know why it might work once and then not work again? I had that
same problem. NickC Nov 28 '11 at 21:26

This is rather hacky. I much rather recommend @luismreis' answer to work around that MySQL hidden
shortcut. MattiSG Feb 16 '12 at 5:25
worked a treat for me too sapatos Apr 23 '12 at 10:48

Did not work for me... I haven't any of those files. But the luismreis' answer worked! Gregoire May 12 '12 at
9:19
@MattiSG luismreis'answer did not work for me... this one did. Paolo Stefan May 17 '12 at 5:48

show 11 more comments

I also had this error, but could only fix it through the suggestion here.
To summarize, use:
127.0.0.1
Instead of:
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.
edited Jan 14 '12 at 1:38

answered Jan 12 '12 at 1:25


luismreis
2,031 1 7 8

This was a correct answer for me. thank you very much!! webdevbyjoss Mar 19 '12 at 21:15

I'm surprised but it worked for me too. Thanks! Gregoire May 12 '12 at 9:20
If this works, then check your hosts file, it might be missing or messed up Fabrizio Jul 20 '12 at 14:06

also maybe good to mention: When using MAMP like me you have to uncheck the box "Allow local access
only" soupdiver Jan 21 '13 at 17:23

@Fabrizio No, it is not related. When you use localhost as name, mysql will use a Unix socket instead that is
handled through a file entry (mysql.sock, for example), not an Inet one, no matter if localhost is declared in
/etc/hosts or not. However, when you use an ip address instead, even 127.0.0.1, you force mysql to open an
Inet socket instead. Fran Nov 19 at 13:57

show 6 more comments

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.
edited Jun 9 '12 at 9:09
Jonathan Spooner
5,211 2 14 28

answered Jun 9 '12 at 2:58


Caio
131 1 2

this is curiously the only solution that worked for me. cheers Dominik Jun 20 '12 at 13:59
This is the only solution that worked for me also. I don't have a /var/mysql/mysql.sock. And I don't have a
/tmp/mysql.sock. JeffB6688 Oct 30 '12 at 23:22
Me too. Thanks. Gregor McKelvie Mar 27 '13 at 20:30

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:

2 de 5

03/12/14 20:08

php - Warning: mysql_connect(): [2002] No such file or direc...

http://stackoverflow.com/questions/4219970/warning-mysql-...

sudo /usr/libexec/locate.updatedb
2) When the updatedb is terminated:
locate mysql.sock
3) Then locate your php.ini:
php -i | grep php.ini
this will output something like:
Configuration File (php.ini) Path => /opt/local/etc/php54
Loaded Configuration File => /opt/local/etc/php54/php.ini
4) Edit your php.ini
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
edited Jul 3 '13 at 0:37

answered Jun 7 '13 at 21:06


noun
594 5 10

This helped for me. I had the same issue. Thanks. Kriem Aug 1 '13 at 18:32
Great answer. Thanks siddhusingh Sep 23 '13 at 6:18
Thanks! That was exactly what I was looking for... Fiftoine Nov 11 '13 at 20:37

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
answered Jul 9 '12 at 6:43
Nirav Mehta
101 1 2
ty - I searched few days for a solution and this is the one that finally made things right! =) XAMPP on OS X too
ljubiccica Apr 10 at 18:50

3 de 5

03/12/14 20:08

php - Warning: mysql_connect(): [2002] No such file or direc...

http://stackoverflow.com/questions/4219970/warning-mysql-...

Hei look here link text


ISSUE #2: PHP throwing error "Warning: mysql_connect() http://function.mysql-connect: 2002 No such file or
directory (trying to connect via unix:///tmp/mysql.sock)"
FIX FOR ISSUE #2: 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
answered Nov 18 '10 at 21:59
Octopus-Paul
4,490 17 30
This was the best answer for me - in my case I am using MAMP, I added to my php.ini: mysqli.default_socket =
/Applications/MAMP/tmp/mysql/mysql.sock mysql.default_socket = /Applications/MAMP/tmp/mysql/mysql.sock
Adamski Oct 22 at 16:12

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 mariadb, use /opt/local/var/run/mariadb/mysqld.sock
For percona, use /opt/local/var/run/percona/mysqld.sock
answered May 23 '13 at 19:36
Sheric
302 1 7
I am using Mountain Lion and have installed MySQL using macports. Sheric May 23 '13 at 19:37

Please make sure that your mysql running firstly!


The reason is that php can not find the correct path of mysql.sock
So firstly, 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
edited Nov 28 '13 at 2:04

answered Nov 25 '13 at 3:17


brucenan
170 1 11

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.
answered Jan 8 '13 at 12:04
Buddy Casino
1,028 3 13 26

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.
answered Jul 20 at 23:32
Boby_Wan
329 4 4

4 de 5

03/12/14 20:08

php - Warning: mysql_connect(): [2002] No such file or direc...

http://stackoverflow.com/questions/4219970/warning-mysql-...

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
answered Dec 2 '13 at 1:34
minhas23
501 3 5

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

5 de 5

03/12/14 20:08

Vous aimerez peut-être aussi