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

mysqli_connect(): (hy000/2002): no such file or directory
mysqli::real_connect(): (hy000/2002): no such file or directory
mysqli_real_connect(): (hy000/2002): no such file or directory
mysql connect error 2002 no such file or directory
unable to connect to zm db sqlstate(hy000) (2002) no such file or directory
dsn write not ok sqlstate(hy000) (2002) no such file or directory
new mysqli
mysqli::real_connect(): (hy000/2002): connection refused

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.

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.

Warning: mysql_connect(): [2002] No such file or directory , The script command I use at the Terminal is php scriptname.php. No such file or directory (trying to connect via unix:///tmp/mysql.sock). The php mysql_connect function uses the mysql sock file descriptor to connect to the database. At times the location of the sock file is different. You can specific the location of sock file as the input to the mysql_connect function. Solution Find out the sock file location if this is at different location then the default. To see the PHP

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.

Php, Php - Warning: Mysql_connect(): [2002] No Such File or Directory (Trying to Connect via Unix::::Tmp:Mysql.sock) in - Stack Overflow - Free download as PDF File  From the PHP documentation about connection to MySQL using PDO: PDO_MYSQL DNS The note at the very end says: Unix only: When the host name is set to "localhost", then the connection to the server is made thru a domain socket.

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.

Database connect error using PDO, even tried using php connect code from MySQL Workbench (warning not Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in $db = new PDO("mysql:host=localhost;mysql:unix_socket=/tmp/mysql.sock  I have tried everything I can think of, including those suggestions in the other two similar posts (this one: My database user exists, but I still get an (HY000/2002): No such file or directory and this one: PHP Warning: mysqli::__construct(): (HY000/2002): No such file or directory (Debian GNU/Linux 9)), and I cannot connect to MySQl with php.

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
    

Warning: mysql_connect() [function.mysql-connect]: No such file or , It helped me, thanks! Reply · How to: Warning: mysql_connect(): 2002 No such file or directory (trying to connect via unix  I spent an age trying to work out why I couldn't post a simple e-mail address to a PHP database. Everytime I tried to test submitting the form using my local server I got the following message: Warning: mysql_connect() [function.mysql-connect]: No such file or directory I was using the following connection string: mysql_connect ("localhost", "root",…

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

Using this command to create a symbol link to mysql.sock in Mac , PHP Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in Command line code on  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

Error establishing database connection – how to proceed now , i thought that during the installation process the file wp-config-sample.php gets to The “no such file or directory” message is also related to trying to connect to MySQL, Connect Error: Access denied for user 'fake_user'@'localhost' (using /tmp/mysql.sock (See PHP – MySQL connection not working: 2002 No such file  [2009-02-03 11:59 UTC] andrey@php.net Hi, it was a problem, the Unix path, in the extensions, not mysqlnd. Yes, mysqlnd uses /tmp/mysql.sock, but actually there are no configure options for mysqlnd. --with-mysql-sock is actually an option of ext/mysql .

Error establishing a database connection, There are three settings in that file for the mysql.sock location for: - pdo - mysql - mysqli Try using the IP address of your local server instead of localhost . /​warning-mysql-connect-2002-no-such-file-or-directory-trying-to-connect-vi Besides that mine fastcgi_pass unix: /var/php-nginx/15822663384347.sock/​socket;. Warning: mysql_connect() [function.mysql-connect]: Too many connections in /htdocs/index.php on line 106 Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /htdocs/index.php on line 112

Warning: mysql_connect(): [2002] No such file or directory, [2002] No such file or directory. Issue Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in  This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register. By continuing to use this site, you are consenting to our use of cookies.

Comments
  • Good answer. Fix worked for me. Do you know why it might work once and then not work again? I had that same problem.
  • This is rather hacky. I much rather recommend @luismreis' answer to work around that MySQL hidden shortcut.
  • Did not work for me... I haven't any of those files. But the luismreis' answer worked!
  • 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.
  • 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.
  • If this works, then check your hosts file, it might be missing or messed up
  • also maybe good to mention: When using MAMP like me you have to uncheck the box "Allow local access only"
  • 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.
  • @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.
  • Does this then, imply that all the grants you provided on the MySQL db to 'user'%'localhost' will break?
  • 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.
  • did you try 127.0.0.1 instead of localhost ?