简体   繁体   中英

Tried every thing still getting ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in mac

Every time I run XAMPP and try to run $mysql -u root -p , its throwing me an error which is:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

I am using XAMPP version 1.7.3 in Mac OS-10.9. Starting Apache and MySQL is not a problem and I have confirmed Apache has started by going to activity monitor.

Few things I tried:

  1. checked if MySQL is running by typing this command in terminal: mysqladmin -u root -p status . Result was:

    mysqladmin: connect to server at 'localhost' failed error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)' Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!

  2. I checked and found mysql.sock file in /var/mysql/mysql.sock .

Things that could have triggered this error but cant say what is wrong:

  1. I recently upgraded my OS to Mavericks from Mountain Lion.
  2. I had also installed separate MySQL to work with Ruby on Rails. I have made sure that version is off.

Without fixing this problem I am not able to do any development locally and connecting MySQL in PHP script seems impossible. So any help will be appreciated.

Here is error_log file from /applications/xampp/xamppfiles/logs/error_log :

[Sat Nov 02 09:56:31 2013] [notice] caught SIGTERM, shutting down
[Sat Nov 02 09:56:39 2013] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
[Sat Nov 02 09:56:40 2013] [notice] Digest: generating secret for digest authentication ...
[Sat Nov 02 09:56:40 2013] [notice] Digest: done
[Sat Nov 02 09:56:40 2013] [notice] Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l PHP/5.3.1 mod_perl/2.0.4 Perl/v5.10.1 configured -- resuming normal operations
[Sat Nov 02 12:55:47 2013] [notice] caught SIGTERM, shutting down
[Sat Nov 02 13:40:16 2013] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
[Sat Nov 02 13:40:18 2013] [notice] Digest: generating secret for digest authentication ...
[Sat Nov 02 13:40:18 2013] [notice] Digest: done
[Sat Nov 02 13:40:18 2013] [notice] Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l PHP/5.3.1 mod_perl/2.0.4 Perl/v5.10.1 configured -- resuming normal operations
[Sat Nov 02 15:12:39 2013] [notice] caught SIGTERM, shutting down
[Sat Nov 02 20:54:41 2013] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
[Sat Nov 02 20:54:43 2013] [notice] Digest: generating secret for digest authentication ...
[Sat Nov 02 20:54:43 2013] [notice] Digest: done
[Sat Nov 02 20:54:44 2013] [notice] Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l PHP/5.3.1 mod_perl/2.0.4 Perl/v5.10.1 configured -- resuming normal operations
[Sat Nov 02 21:10:53 2013] [notice] caught SIGTERM, shutting down
[Sat Nov 02 21:10:57 2013] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
[Sat Nov 02 21:10:58 2013] [notice] Digest: generating secret for digest authentication ...
[Sat Nov 02 21:10:58 2013] [notice] Digest: done
[Sat Nov 02 21:10:58 2013] [notice] Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l PHP/5.3.1 mod_perl/2.0.4 Perl/v5.10.1 configured -- resuming normal operations
[Sat Nov 02 21:31:39 2013] [notice] caught SIGTERM, shutting down
[Sat Nov 02 21:31:44 2013] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
[Sat Nov 02 21:31:47 2013] [notice] Digest: generating secret for digest authentication ...
[Sat Nov 02 21:31:47 2013] [notice] Digest: done
[Sat Nov 02 21:31:47 2013] [notice] Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l PHP/5.3.1 mod_perl/2.0.4 Perl/v5.10.1 configured -- resuming normal operations
[Sat Nov 02 21:45:07 2013] [notice] caught SIGTERM, shutting down
[Sun Nov 03 08:50:06 2013] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
[Sun Nov 03 08:50:09 2013] [notice] Digest: generating secret for digest authentication ...
[Sun Nov 03 08:50:09 2013] [notice] Digest: done
[Sun Nov 03 08:50:10 2013] [notice] Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l PHP/5.3.1 mod_perl/2.0.4 Perl/v5.10.1 configured -- resuming normal operations  

   

Finally manage to solve it. First I was looking on wrong directory, I should have looked into /applications/xampp/xamppfiles/ .

List of things I did:

  1. there was not tmp folder under /applications/xampp/xamppfiles, so I created it (mkdir tmp).

  2. I searched for mysql.sock file and found it under /applications/xampp/xamppfiles/var/mysql/mysql.sock .

As mysql server connection was not working via socket /tmp/mysql.sock , I used following command to point my /applications/xampp/xamppfiles/var/mysql/mysql.sock to /tmp/mysql.sock file.

So this command did the trick:

ln -s /applications/xampp/xamppfiles/var/mysql/mysql.sock /tmp/mysql.sock
  1. now I restart xampp services and run: mysql -u root -p , I can get into mysql console. And I also tried connecting mysql from php script and its running ok.

After pulling hair for a day and half I finally managed to get it working hope people will find this helpful.

Not sure how you get a list of running programs on a mac but you should try and verify that mysqld is actually running. (on linux "top -bn1 | grep mysql" would normally show a line of info if it is running, or nothing if it is not.)

You could also try stopping mysqld and see if /tmp/mysql.sock vanishes. If it doesn't then try deleting it and restarting mysql to see if it appears again. (it should)

Do you have any log files that might show what is going on?

Looks like your mysql server is not started. Use the following commands:

mysqld stop
mysql.server start
ln -s /applications/xampp/xamppfiles/var/mysql/mysql.sock /tmp/mysql.sock

在High Sierra上帮了我大忙。

XAMPP mac has the default settings to store mysql.sock another location. Just change it :)

$ vim /applications/xampp/xamppfiles/etc/my.cnf

[client]
#password   = your_password
port        =3306
#socket     =/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
socket      =/tmp/mysql.sock         <---------

...

[mysqld]
user=mysql
port=3306
#socket     =/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
socket      =/tmp/mysql.sock        <---------

Restart mysql, then works.

ln -s /applications/xampp/xamppfiles/var/mysql/mysql.sock /tmp/mysql.sock 

That worked for me. I have MacOs sierra in my Mac and XAMPP 5.6.30-0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM