php 警告:mysql_connect():[2002] 中没有这样的文件或目录(尝试通过 unix:///tmp/mysql.sock 连接)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4219970/
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
Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in
提问by Nr 28
I'm trying to connect to my MySQL DB with the Terminal on my Apple (With PHP).
我正在尝试使用 Apple 上的终端(使用 PHP)连接到我的 MySQL 数据库。
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.
当我使用浏览器运行该脚本时(我安装了 XAMPP),该脚本有效,但终端拒绝连接到数据库。
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
.
我在终端使用的命令是php scriptname.php
.
回答by Brian Lowe
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.
出于某种原因,OS X 上的 mysql 获取所需套接字文件的位置有点错误,但幸运的是,解决方案就像设置符号链接一样简单。
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:
您可能有一个套接字(显示为零长度文件)为/tmp/mysql.sock
或/var/mysql/mysql.sock
,但一个或多个应用程序正在另一个位置寻找它。用这个命令找出:
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!
而不是移动套接字,编辑配置文件,并且必须记住将编辑的文件保存在本地并远离路径正确的服务器,只需创建一个符号链接,以便您的 Mac 找到所需的套接字,即使它在错误的位置查找!
If you have /tmp/mysql.sock
but no /var/mysql/mysql.sock
then...
如果你有/tmp/mysql.sock
但没有/var/mysql/mysql.sock
那么...
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...
如果你有/var/mysql/mysql.sock
但没有/tmp/mysql.sock
那么...
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.
您将需要创建目录和链接的权限,因此如有必要,只需在上面的命令前加上 sudo。
回答by luismreis
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.
原因是“localhost”是 MySQL 驱动程序的特殊名称,它使用 UNIX 套接字而不是 TCP 套接字连接到 MySQL。
回答by Caio
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.
我没有使用 mysql 服务器,而是直接连接到 Unix Socket。为我工作。
回答by noun
MySQL socket is located, in general, in /tmp/mysql.sock
or /var/mysql/mysql.sock
, but probably PHP looks in the wrong place.
MySQL 套接字通常位于/tmp/mysql.sock
或 中/var/mysql/mysql.sock
,但 PHP 可能位于错误的位置。
Check where is your socket with:
sudo /usr/libexec/locate.updatedb
When the updatedb is terminated:
locate mysql.sock
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
Edit your php.ini
sudo vim /opt/local/etc/php54/php.ini
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.
Save your modifications and exit ESC + SHIFT: x
Restart Apache
sudo apachectl stop sudo apachectl start
检查您的插座在哪里:
sudo /usr/libexec/locate.updatedb
当updatedb终止时:
locate mysql.sock
然后找到你的 php.ini:
php -i | grep php.ini
这将输出如下内容:
Configuration File (php.ini) Path => /opt/local/etc/php54 Loaded Configuration File => /opt/local/etc/php54/php.ini
编辑你的 php.ini
sudo vim /opt/local/etc/php54/php.ini
更改行:
pdo_mysql.default_socket=/tmp/mysql.sock mysql.default_socket=/tmp/mysql.sock mysqli.default_socket = /tmp/mysql.sock
其中 /tmp/mysql.sock 是您的套接字路径。
保存修改并退出 ESC + SHIFT: x
重启阿帕奇
sudo apachectl stop sudo apachectl start
回答by Nirav Mehta
I am on XAMPP on Mac OS X, and Brian Lowe's solution above worked with a slight modification.
我在 Mac OS X 上使用 XAMPP,上面的 Brian Lowe 的解决方案稍作修改。
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 :-)
mysql.sock 文件实际上在“/Applications/xampp/xamppfiles/var/mysql/”文件夹中。所以必须在/tmp 和/var/mysql 中将它链接起来。我还没有检查 PHP 命令行使用了哪一个,但这解决了问题,所以我很高兴 :-)
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
回答by Jsonras
Mac OS X EL Capitan + MAMP Pro Do this
Mac OS X EL Capitan + MAMP Pro 这样做
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.
希望这可以为您节省一些时间。
回答by cristian
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)"
PHP 抛出错误“警告:mysql_connect() http://function.mysql-connect:2002 没有这样的文件或目录(尝试通过 unix:///tmp/mysql.sock 连接)”
Set "mysql.default_socket" value in your /etc/php.ini
to
将“mysql.default_socket”值设置/etc/php.ini
为
"mysql.default_socket = /var/mysql/mysql.sock".
Then restart web service in server admin
然后在服务器管理中重新启动 web 服务
回答by brucenan
The reason is that php cannot find the correct path of mysql.sock
.
原因是 php 找不到正确的mysql.sock
.
Please make sure that your mysql is running first.
请确保您的 mysql 首先运行。
Then, please confirm that which path is the mysql.sock
located, for example /tmp/mysql.sock
然后,请确认所在的路径mysql.sock
,例如/tmp/mysql.sock
then add this path string to php.ini:
然后将此路径字符串添加到 php.ini:
- mysql.default_socket = /tmp/mysql.sock
- mysqli.default_socket = /tmp/mysql.sock
- pdo_mysql.default_socket = /tmp/mysql.sock
- mysql.default_socket = /tmp/mysql.sock
- mysqli.default_socket = /tmp/mysql.sock
- pdo_mysql.default_socket = /tmp/mysql.sock
Finally, restart Apache.
最后,重启Apache。
回答by kingus
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.
修复迫在眉睫的 2002 套接字错误——它链接 MySQL 放置套接字的位置和 OSX 认为它应该在的位置,MySQL 将它放在 /tmp 中,OSX 在 /var/mysql 中查找它,套接字是一种允许 mysql 客户端的文件/服务器通信。
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/
来源:http: //coolestguidesontheplanet.com/get-apache-mysql-php-phpmyadmin-working-osx-10-10-yosemite/
回答by tanveer ahmad dar
i was having the same issue
我遇到了同样的问题
[PDOException] SQLSTATE[HY000] [2002] No such file or directory
[PDOException] SQLSTATE[HY000] [2002] 没有那个文件或目录
[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
[ErrorException] 警告:PDO::__construct(): [2002] 在 ...htdocs/Symfony/vendor/doctrine-dbal/ 中没有这样的文件或目录(试图通过 unix:///var/mysql/mysql.sock 连接) 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:
因此,解决方案是创建指向 sock 文件的符号链接,从而解决问题。执行以下操作来解决它:
$ sudo mkdir /private/var/mysql/
$ sudo mkdir /private/var/mysql/
$ sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /private/var/mysql/mysql.sock
$ sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /private/var/mysql/mysql.sock
来源:http: //www.reecefowell.com/2012/07/21/symfony2-cli-does-not-connect-to-mysql-while-browser-works-fine/