php 无法在 Mac 上连接到 MySQL -- 缺少 mysql.sock 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5784791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:29:14  来源:igfitidea点击:

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

phpmysqlmacosmysql-error-2002

提问by tchaymore

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.

大约一周前,我在 Mac OS 10.6 上安装了 MySQL,并且在玩了一段时间之后,让它工作得很好。它与 python MySQLdb 集成,我还使用 Sequel Pro 连接到数据库。但是,php 不会访问服务器。即使在我将 php.ini 文件添加到 /etc/ 并将其定向到 Sequel Pro 使用的同一个袜子之后:/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.

但是现在我根本无法访问本地服务器。据我所知,我的计算机上的任何地方都没有 mysql.sock 文件,不在 /tmp/ 或其他任何地方。

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

我可以从终端启动 mysql 服务器,但一分钟后它会自动将我注销:

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):

如果我尝试从命令行调用“mysql”(今天早些时候工作得很好):

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

The PHP error is of course similar:

PHP错误当然是类似的:

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.

另外,我的mysql安装目录:/usr/local/mysql中没有“my.cnf”文件。XAMPP 附带了用于 mysql 安装的 my.cnf 文件。那些也将默认套接字列为“/tmp/mysql.sock”,但我必须手动更改它们。

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

任何想法发生了什么?为什么修改 php.ini 文件也会对 Sequel Pro 产生变化?

回答by Riba

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/

首先,我建议您使用自制软件在您的 mac 上安装任何第三方库或工具。看看: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.

否则,对于您的问题,您可以搜索 mac 上的 mysql 套接字在哪里,然后将其符号链接到 /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.

你也可以编辑你的 php.ini 文件来为 mysql 套接字设置正确的路径。

Hope this help.

希望这有帮助。

回答by Duda Nogueira

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).

我遇到了一些类似的错误,最后到了这里。我使用的是 OSX 10.9.5。这解决了我的问题(希望它可以帮助某人)。

1) sudo mkdir /var/mysql

1) 须藤 mkdir /var/mysql

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

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

更多信息:http: //glidingphenomena.blogspot.com.br/2010/03/fixing-warning-mysqlconnect-cant.html

Thanks!

谢谢!

回答by Spence-

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!

好的,我只是花了几个小时来解决同样的问题。我已经在装有 OSX 10.7.2 的 iMac 上为 osx 10.6 安装了 MySql 5.5.20(64 位)的 dmg 文件 - 并且 /tmp/mysql.sock 丢失了!

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).

答案原来是简单地安装 dmg 文件附带的 MySQLStartupItem.pkg 并重新启动系统。这会在启动时启动守护进程(我希望这是可选的)。

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

神奇的是,我的 /tmp/mysql.sock 文件出现了,一切都很好!

回答by Ted Wichman

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

我在全新安装的 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:

根据上面的建议(谢谢),我使用我的旧标准“轻松查找”来定位最新版本的 my.cnf。在编辑器中打开文件后,我发现套接字文件指向:

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

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

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

我更新了 Navicat 的高级属性/使用套接字文件到上面的路径和宾果游戏。

Hope this helps someone.

希望这可以帮助某人。

回答by Patrick

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.

我要在这里投入我的两分钱,因为我也有这个问题,但我的情况对大多数人来说都是不正常的。我的系统混合安装了 Zend Server CE 和 XAMPP。我正在运行 Mac OS X。我决定从我的系统中删除 Zend,因为它让我很痛苦。

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.

删除 Zend 后,由于两者都使用相同的数据库并且 Zend 拥有它,我的系统变得疯狂。卸载并重新安装 XAMPP 没有帮助。经过几个小时(我刚刚完成)故障排除后,我发现我需要在我的系统中搜索 my.cnf 的所有副本。原因是 mysql 仍然不断尝试寻找 Zend mysql 路径,但失败了。

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.

所以我使用 locate 来找到它。我必须启动定位(因为我以前从未使用过它),以便它可以在我的系统上构建一个文件数据库。然后我运行“locate my.cnf”并在“/private/etc”中找到了一个。所以我只是将文件重命名为 my.cnf.old 并尝试再次启动 MySQL。砰!它现在有效。

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!

我会这样说,我不得不让一位同事帮助我,因为我只是在这个轮子上旋转。我真的希望这可以帮助那里的人。谢谢!

回答by Martian.titan

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"

找到解决方案:
我遇到了同样的问题,我进行了大量搜索,但直到查看 MySql 配置文件后才解决问题。在我的配置文件中,socket 在第 21 行,路径是
“/Applications/AMPPS/var/mysql.sock”

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

在 Ampps 应用程序中,单击 MySql 选项卡,然后单击配置按钮。

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

您还可以在“/Applications/AMPPS/mysql/etc”文件夹中找到配置文件

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.

所以我简单地添加了
'unix_socket' => '/Applications/AMPPS/var/mysql.sock',
到我的 database.php 在 mysql 数组中,它对我有用。
主机线后。我已经用 localhost 和 127.0.0.1 进行了测试,两者都有效,
希望它对你也有效。

回答by Simon

If you have installed mysql through homebrew, simple brew services restart mysqlmay help.

如果您已经通过自制软件安装了 mysql,那么简单brew services restart mysql可能会有所帮助。