沃梦达 / 编程问答 / php问题 / 正文

警告:mysql_connect(): [2002] 没有这样的文件或目录(试图通过 unix:///tmp/mysql.sock 连接)

Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in(警告:mysql_connect(): [2002] 没有这样的文件或目录(试图通过 unix:///tmp/mysql.sock 连接))

本文介绍了警告:mysql_connect(): [2002] 没有这样的文件或目录(试图通过 unix:///tmp/mysql.sock 连接)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Apple 上的终端(使用 PHP)连接到我的 MySQL 数据库.

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.

当我使用浏览器运行该脚本时(我安装了 XAMPP),该脚本有效,但终端拒绝连接到数据库.

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.

我在终端使用的命令是php scriptname.php.

The command I use at the Terminal is php scriptname.php.

推荐答案

由于某种原因,OS X 上的 mysql 获取所需套接字文件的位置有点错误,但幸运的是,解决方案就像设置符号链接一样简单.

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.

您可能有一个套接字(显示为零长度文件)为 /tmp/mysql.sock/var/mysql/mysql.sock,但一个或更多应用程序正在其他位置寻找它.用这个命令找出来:

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

与其移动套接字、编辑配置文件并记住将编辑后的文件保存在本地并远离路径正确的服务器,只需创建一个符号链接,以便您的 Mac 找到所需的套接字,即使它正在查找找错地方了!

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!

如果你有 /tmp/mysql.sock 但没有 /var/mysql/mysql.sock 那么...

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

如果你有 /var/mysql/mysql.sock 但没有 /tmp/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

您将需要创建目录和链接的权限,因此如果需要,只需在上面的命令前加上 sudo.

You will need permissions to create the directory and link, so just prefix the commands above with sudo if necessary.

这篇关于警告:mysql_connect(): [2002] 没有这样的文件或目录(试图通过 unix:///tmp/mysql.sock 连接)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:警告:mysql_connect(): [2002] 没有这样的文件或目录(试图通过 unix:///tmp/mysql.sock 连接)

基础教程推荐