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

17

247

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.

Ironlike answered 18/11, 2010 at 21:53 Comment(0)
T
420

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.

Tureen answered 31/8, 2011 at 23:2 Comment(12)
Good answer. Fix worked for me. Do you know why it might work once and then not work again? I had that same problem.Neotropical
This is rather hacky. I much rather recommend @luismreis' answer to work around that MySQL hidden shortcut.Megganmeggi
Did not work for me... I haven't any of those files. But the luismreis' answer worked!Nosography
@Megganmeggi luismreis'answer did not work for me... this one did.Clearance
This is not "hacky". It is the correct way to get the localhost socket to work, and it may be of importance if you don't want the extra overhead from sending packets to the router before the database is accessed.Bur
The path on my Ubuntu 10.04 was not correct. I had to do this: cd /tmp and then: sudo ln -s /var/run/mysqld/mysqld.sock mysql.sock.Greegree
Mine was first sketched situation right after a fresh install of mysql. Solution worked!Smashup
This is a php.ini configuration issue, @noun's answer is the correct approach. This answer is a fudge.Dimaggio
Thanks for this, saved me a lot of time. Why is it so broken in OSX?Dreyfus
I moved from MacPorts to Homebrew for MySQL and had the wrong symlink mysql.sock -> /opt/local/var/run/mysql5/mysqld.sock at /var/mysql. Changed it by running ln -s /tmp/mysql.sock mysql.sock as suggested here and all good now!Selfoperating
+1 This worked well for me -- thanks. I just wonder why you didn't have sudo in front of the ln -s, when you had it in front of mkdir and chmod. Seems to me the ln requires it just as much as the others.Nimble
$ sudo su - to change user to root mode.Limon
A
375

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.

Arsonist answered 12/1, 2012 at 1:25 Comment(10)
If this works, then check your hosts file, it might be missing or messed upEnglacial
also maybe good to mention: When using MAMP like me you have to uncheck the box "Allow local access only"Lucila
This solution makes extra overhead as the packet is sent by the router first. Usually not a big problem while testing, but it may become a problem with large packets of data.Bur
@Bur that's non sense. The 127.0.0.1 interface does not make packets pass thru any router at all. Just try a traceroute 127.0.0.1Discounter
@Englacial 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.Discounter
Does this then, imply that all the grants you provided on the MySQL db to 'user'%'localhost' will break?Knudsen
@Kebman, can you explain more about why the packet would be sent by the router first, and what that means (first before what)?Nimble
Please put it clearly by saying something like use "127.0.0.1" in your mysql_connect instead of "localhost". Otherwise this worked for me, +1.Expectant
Not really a goer if you use virtual hosts, or like to name your test web-pages in /etc/hosts - far better to use a solution like @noun below and edit the php.ini file to point to the correct socketChastitychasuble
This is a great answer, thanks. Using "localhost" instead of "127.0.0.1" was causing a strange problem with cli_dispatch.php of my typo3 installation (Uncaught TYPO3 Exception #1270853884: TYPO3 Fatal Error: The current username, password or host was not accepted when the connection to the database was attempted to be established!). Thanks to your answer this is fixed now.Mouth
U
25

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.

Undersheriff answered 9/6, 2012 at 2:58 Comment(2)
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.Blemish
did you try 127.0.0.1 instead of localhost ?Pretension
S
22

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
    
  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
    
Socket answered 7/6, 2013 at 21:6 Comment(3)
I don't have a php.ini; I only have a php.ini.default, so it appears that the workaround here is necessary: apple.stackexchange.com/questions/65802/… However, it did not work for me.Mending
@Mending Your installation is probably broken: rename your php.ini.default to php.ini.Socket
Apache can be restarted with a single line: sudo apachectl restartJohnathanjohnathon
V
13

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
Vanburen answered 9/7, 2012 at 6:43 Comment(1)
ty - I searched few days for a solution and this is the one that finally made things right! =) XAMPP on OS X tooChrissa
G
11

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.

Guano answered 15/9, 2015 at 0:30 Comment(1)
It's linking the mysql.dock in the /tmp that solved it. Thanks!Fir
G
6

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

Gillenwater answered 18/11, 2010 at 21:59 Comment(1)
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.sockGe
F
6

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

Please make sure that your mysql is running first.

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.

Fishman answered 25/11, 2013 at 3:17 Comment(0)
M
4

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

source: http://coolestguidesontheplanet.com/get-apache-mysql-php-phpmyadmin-working-osx-10-10-yosemite/

Matted answered 3/4, 2015 at 20:7 Comment(0)
B
1

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.

Brachy answered 8/1, 2013 at 12:4 Comment(0)
M
1

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
Mauriciomaurie answered 23/5, 2013 at 19:36 Comment(1)
I am using Mountain Lion and have installed MySQL using macports.Mauriciomaurie
D
1

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

source:http://www.reecefowell.com/2012/07/21/symfony2-cli-does-not-connect-to-mysql-while-browser-works-fine/

Distribution answered 29/9, 2015 at 18:12 Comment(1)
Post your answer here, rather than a link to an external website.Pitanga
S
0

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

Senaidasenalda answered 2/12, 2013 at 1:34 Comment(0)
C
0

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.

Cosma answered 20/7, 2014 at 23:32 Comment(0)
L
0

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.

Lotze answered 13/4, 2016 at 8:54 Comment(0)
T
0

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
        '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
    );
Theodoratheodore answered 5/6, 2017 at 21:44 Comment(0)
A
0

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.

Alary answered 6/4, 2019 at 15:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.