Can't connect to MySQL on Mac — missing mysql.sock file

风流意气都作罢 提交于 2019-11-27 16:32:19

问题


I installed MySQL on my Mac OS 10.6 about a week ago, and, after some playing around, got it to work just fine. It integrated with python MySQLdb and I also got Sequel Pro to connect to the database. However, php wouldn't access the server. Even after I added a php.ini file to /etc/ and directed it toward the same sock that Sequel Pro was using: /tmp/mysql.sock.

But now I can't access the local server at all. As far as I can tell, there is no mysql.sock file anywhere on my computer, not in /tmp/ or anywhere else.

I can start the mysql server from Terminal, but it logs me out automatically after a minute:

110425 17:36:18 mysqld_safe Logging to '/usr/local/mysql/data/dn0a208bf7.sunet.err'.
110425 17:36:18 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
110425 17:37:58 mysqld_safe mysqld from pid file /usr/local/mysql/data/dn0a208bf7.sunet.pid ended

If I try to call "mysql" from the command line (which worked perfectly earlier today):

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

The PHP error is of course similar:

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

Also, there is no "my.cnf" file in my mysql installation directory: /usr/local/mysql. There are my.cnf files for the mysql installations that come along with XAMPP. Those also have the default socket listed as '/tmp/mysql.sock', but I had to change them manually.

Any ideas what's going on? Why would modifying the php.ini file have produced a change for Sequel Pro as well?


回答1:


First of all I suggest you to use homebrew to install any third part libraries or tools on your mac. Have a look to : http://mxcl.github.com/homebrew/

Otherwise for your problem you can search where is the mysql socket on your mac and then symlink it to /tmp.

In your terminal try something like :

locate mysql | grep sock

You will get something like :

/the/path/to/mysql.sock

Then do :

ln -s /the/path/to/mysql.sock /tmp/mysql.sock

This should works.

Also you can edit your php.ini file to set the correct path for the mysql socket.

Hope this help.




回答2:


I was getting some similar error and ended up here. I am using OSX 10.9.5. This solved my problems (hope it helps someone).

1) sudo mkdir /var/mysql

2) sudo ln -s /private/tmp/mysql.sock /var/mysql/mysql.sock

More informations: http://glidingphenomena.blogspot.com.br/2010/03/fixing-warning-mysqlconnect-cant.html

Thanks!




回答3:


Ok, I just spent a couple hours struggling with this same problem. I had installed the dmg file for MySql 5.5.20 (64bit) for osx 10.6 on my iMac with OSX 10.7.2 - and the /tmp/mysql.sock was missing!

The answer turned out to be simply install the MySQLStartupItem.pkg that comes with the dmg file and restart the system. This starts up the daemons upon startup (which I hoped would be optional).

Magically, my /tmp/mysql.sock file appeared and all is well!




回答4:


I was also getting this error on a fresh install of XAMPP.

For those not comfortable with the command line, there is another way.

Based on the advice above (thank you), I used my old standard "Easy Find" to locate the latest version of my.cnf. Upon opening the file in an editor I discovered that the socket file was pointing to:

socket = /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock

I updated Navicat's Advanced properties/ Use socket file to the path above and bingo.

Hope this helps someone.




回答5:


I am going to throw in my two cents here because I had this problem too but my circumstances are abnormal for most. My system had a blended install of Zend Server CE and XAMPP. I am running Mac OS X. And I decided to remove Zend from my system because it was being a pain in my butt.

Upon removing Zend, since both were using the same DB and Zend had it my system went crazy. Uninstalling and reinstalling XAMPP didn't help. After a couple hours (which I just literally got done with) of troubleshooting I found that I needed to search my system for all copies my.cnf. The reason being is that mysql still kept trying to look for the Zend mysql path and it was failing.

So I used locate to find it. I had to initiate locate (since I had never used it before) so that it could build a DB of files on my system. Then I ran "locate my.cnf" and I found one in "/private/etc". So I just renamed the file to my.cnf.old and tried to launch MySQL again. Bam! It works now.

I will say this, I had to have a co-worker help me out because I was just spinning my wheels on this one. I really hope this helps someone out there. Thanks!




回答6:


Found the solution:
I had the same issue and I did a lot of searches but couldn't solve the issue until I looked at my MySql config file. In my config file socket was in line 21 and the path was
"/Applications/AMPPS/var/mysql.sock"

In Ampps application click on MySql tab and then click on configuration button.

You can also find the config file in "/Applications/AMPPS/mysql/etc" folder

So simply I added
'unix_socket' => '/Applications/AMPPS/var/mysql.sock',
to my database.php in mysql array and it's worked for me.
After host line. I have tested with localhost and 127.0.0.1 and both worked
Hopefully it's working for you too.



来源:https://stackoverflow.com/questions/5784791/cant-connect-to-mysql-on-mac-missing-mysql-sock-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!