php MySQL 连接不工作:2002 没有这样的文件或目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1676688/
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 03:28:17  来源:igfitidea点击:

MySQL connection not working: 2002 No such file or directory

phpmysqlconnection

提问by mk12

I'm trying to set up WordPress. I have Apache and MySQL running, and the accounts and database are all set up. I tried to make a simple connection:

我正在尝试设置 WordPress。我运行了Apache和MySQL,账户和数据库都设置好了。我试图建立一个简单的联系:

<?php
    $conn = mysql_connect('localhost', 'USER', 'PASSWORD');
    if(!$conn) {
        echo 'Error: ' . mysql_errno() . ' - ' . mysql_error();
    }
?>

And I always get this:

我总是得到这个:

Error: 2002 - No such file or directory

错误:2002 - 没有这样的文件或目录

What file or directory could it be talking about?

它可以谈论什么文件或目录?

I'm on a OS X Snow Leopard, using the built-in Apache. I installed MySQL using the x86_64 dmg.

我在 OS X Snow Leopard 上,使用内置的 Apache。我使用 x86_64 dmg 安装了 MySQL。

UPDATE: I found that the socket is at /tmp/mysql.sock, so In php.ini, I replaced all occurrences of the wrong path with that.

更新:我发现套接字位于 /tmp/mysql.sock,所以在 php.ini 中,我用它替换了所有出现的错误路径。

采纳答案by Alec Gorge

If you use Linux: the path to the mysql.sock file is wrong. This is usually because you are using (LAMPP) XAMPP and it isn't in /tmp/mysql.sock

如果您使用 Linux:mysql.sock 文件的路径是错误的。这通常是因为您使用的是 (LAMPP) XAMPP 而它不在 /tmp/mysql.sock 中

Open the php.ini file and find this line:

打开 php.ini 文件并找到这一行:

mysql.default_socket

And make it

并使它

mysql.default_socket = /path/to/mysql.sock

回答by bryan kennedy

I had a similar problem and was able to solve it by addressing my mysql with 127.0.0.1instead of localhost.

我有一个类似的问题,并且能够通过使用127.0.0.1而不是localhost.

This probably means I've got something wrong in my hosts setup, but this quick fix get's me going for right now.

这可能意味着我的主机设置有问题,但这个快速修复让我现在就去。

回答by Jacek Laskowski

This is for Mac OS Xwith the native installation of Apache HTTPand custom installation of MySQL.

这适用于Mac OS X,带有Apache HTTP本机安装MySQL 的自定义安装

The answer is based on @alec-gorge's excellent response, but since I had to google some specific changes to have it configured in my configuration, mostly Mac OS X-specific, I thought I'd add it here for the sake of completeness.

答案基于@alec-gorge 的出色回复,但由于我不得不在我的配置中搜索一些特定更改以对其进行配置,主要是 Mac OS X 特定的,我想为了完整起见我会在此处添加它。

Enable PHP5 support for Apache HTTP

为 Apache HTTP 启用 PHP5 支持

Make sure the PHP5 support is enabled in /etc/apache2/httpd.conf.

确保在/etc/apache2/httpd.conf.

Edit the file with sudo vi /etc/apache2/httpd.conf(enter the password when asked) and uncomment (remove ;from the beginning of) the line to load the php5_modulemodule.

使用sudo vi /etc/apache2/httpd.conf(在询问时输入密码)编辑文件并取消注释(;从开头删除)以加载php5_module模块的行。

LoadModule php5_module libexec/apache2/libphp5.so

Start Apache HTTP with sudo apachectl start(or restartif it's already started and needs to be restarted to re-read the configuration file).

启动 Apache HTTP sudo apachectl start(或者restart如果它已经启动并且需要重新启动以重新读取配置文件)。

Make sure that /var/log/apache2/error_logcontains a line that tells you the php5_module is enabled - you should see PHP/5.3.15(or similar).

确保/var/log/apache2/error_log包含一行告诉您 php5_module 已启用 - 您应该看到PHP/5.3.15(或类似)。

[notice] Apache/2.2.22 (Unix) DAV/2 PHP/5.3.15 with Suhosin-Patch configured -- resuming normal operations

Looking up Socket file's name

查找套接字文件名

When MySQL is up and running (with ./bin/mysqld_safe) there should be debug lines printed out to the console that tell you where you can find the log files. Note the hostname in the file name - localhostin my case - that may be different for your configuration.

当 MySQL 启动并运行(使用./bin/mysqld_safe)时,应该会在控制台上打印出调试行,告诉您在哪里可以找到日志文件。请注意文件名中的主机名 -localhost在我的情况下 - 可能与您的配置不同。

The file that comes after Logging tois important. That's where MySQL logs its work.

后面的文件Logging to很重要。这就是 MySQL 记录其工作的地方。

130309 12:17:59 mysqld_safe Logging to '/Users/jacek/apps/mysql/data/localhost.err'.
130309 12:17:59 mysqld_safe Starting mysqld daemon with databases from /Users/jacek/apps/mysql/data

Open the localhost.errfile (again, yours might be named differently), i.e. tail -1 /Users/jacek/apps/mysql/data/localhost.errto find out the socket file's name - it should be the last line.

打开localhost.err文件(同样,您的文件可能以不同的方式命名),即tail -1 /Users/jacek/apps/mysql/data/localhost.err找出套接字文件的名称——它应该是最后一行。

$ tail -1 /Users/jacek/apps/mysql/data/localhost.err
Version: '5.5.27'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)

Note the socket:part - that's the socket file you should use in php.ini.

请注意socket:部分 - 那是您应该在php.ini.

There's another way (some say an easier way) to determine the location of the socket's file name by logging in to MySQL and running:

还有另一种方法(有人说更简单的方法)通过登录 MySQL 并运行来确定套接字文件名的位置:

show variables like '%socket%';

Configuring PHP5 with MySQL support - /etc/php.ini

使用 MySQL 支持配置 PHP5 - /etc/php.ini

Speaking of php.ini...

说到 php.ini...

In /etcdirectory there's /etc/php.ini.defaultfile. Copy it to /etc/php.ini.

/etc目录中有/etc/php.ini.default文件。将其复制到/etc/php.ini

sudo cp /etc/php.ini.default /etc/php.ini

Open /etc/php.iniand look for mysql.default_socket.

打开/etc/php.ini并查找mysql.default_socket

sudo vi /etc/php.ini

The default of mysql.default_socketis /var/mysql/mysql.sock. You should change it to the value you have noted earlier - it was /tmp/mysql.sockin my case.

的默认mysql.default_socket值为/var/mysql/mysql.sock。您应该将其更改为您之前注意到的值 - 就/tmp/mysql.sock我而言。

Replace the /etc/php.inifile to reflect the socket file's name:

替换/etc/php.ini文件以反映套接字文件的名称:

mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock

Final verification

最终验证

Restart Apache HTTP.

重新启动 Apache HTTP。

sudo apachectl restart 

Check the logs if there are no error related to PHP5. No errors means you're done and PHP5 with MySQL should work fine. Congrats!

如果没有与 PHP5 相关的错误,请检查日志。没有错误意味着你已经完成了,PHP5 和 MySQL 应该可以正常工作。恭喜!

回答by Oleg

Replacing 'localhost' to '127.0.0.1' in config file (db connection) helped!

在配置文件(数据库连接)中将 'localhost' 替换为 '127.0.0.1' 有帮助!

回答by Avani Khabiya

Restarting the mysql server might help. In my case, restarting the server saved a lot of time.

重新启动 mysql 服务器可能会有所帮助。就我而言,重新启动服务器节省了大量时间。

service mysql restart

P.S.- use sudo service mysql restartfor non-root user.

PS-sudo service mysql restart用于非 root 用户。

回答by Todd Moses

First, ensure MySQL is running. Command: mysqld start

首先,确保 MySQL 正在运行。命令:mysqld 启动

If you still cannot connect then: What does your /etc/my.cnf look like? (or /etc/msyql/my.cnf)

如果您仍然无法连接,那么:您的 /etc/my.cnf 是什么样的?(或 /etc/msyql/my.cnf)

The other 2 posts are correct in that you need to check your socket because 2002 is a socket error.

其他 2 个帖子是正确的,您需要检查您的套接字,因为 2002 是一个套接字错误。

A great tutorial on setting up LAMP is: http://library.linode.com/lamp-guides/centos-5.3/index-print

关于设置 LAMP 的一个很好的教程是:http: //library.linode.com/lamp-guides/centos-5.3/index-print

回答by Arthur Reutenauer

Not that it helps you much, but in the recent versions(and even less recent) of MySQL, error code 2002 means “Can't connect to local MySQL server through socket [name-of-socket]”, so that might tell you a bit more.

并不是说它对您有多大帮助,但是在MySQL的最新版本(甚至是新的版本)中,错误代码 2002 表示“无法通过套接字 [name-of-socket] 连接到本地 MySQL 服务器”,因此这可能会告诉您多一点。

回答by mattbell87

Expanding on Matthias D's answer hereI was able to resolve this 2002 erroron both MySQL and MariaDB with exact paths using these commands:

这里扩展Matthias D 的回答,我能够使用以下命令使用精确路径解决MySQL 和 MariaDB 上的这个2002 错误

First get the actual pathto the MySQL socket:

首先获取MySQL 套接字的实际路径

netstat -ln | grep -o -m 1 '/.*mysql.sock'

Then get the PHP path:

然后获取PHP路径

php -r 'echo ini_get("mysql.default_socket") . "\n";'

Using the output of these two commands, link them up:

使用这两个命令输出,将它们链接起来:

sudo ln -s /actualpath/mysql.sock /phppath/mysql.sock

If that returns No such file or directoryyou just need to create the path to the PHP mysql.sock, for example if your path was /var/mysql/mysql.sockyou would run:

如果返回,No such file or directory您只需要创建 PHP mysql.sock 的路径,例如,如果您的路径是,/var/mysql/mysql.sock您将运行:

sudo mkdir -p /var/mysql

Then try the sudo lncommand again.

然后再次尝试sudo ln命令。

回答by masoud2011

in my case I have problem with mysqli_connect.
when I want to connect
mysqli_connect('localhost', 'myuser','mypassword')
mysqli_connect_error() return me this error "No such file or directory"

就我而言,mysqli_connect 有问题。
当我想连接
mysqli_connect('localhost', 'myuser','mypassword')
mysqli_connect_error() 时返回这个错误“没有这样的文件或目录”

this worked for me mysqli_connect('localhost:3306', 'myuser','mypassword')

这对我有用 mysqli_connect('localhost:3306', 'myuser','mypassword')

回答by Myles

I'd check your php.ini file and verify the mysql.default_socket is set correctly and also verify that your mysqld is correctly configured with a socket file it can access. Typical default is "/tmp/mysql.sock".

我会检查您的 php.ini 文件并验证 mysql.default_socket 设置是否正确,并验证您的 mysqld 是否正确配置了它可以访问的套接字文件。典型的默认值是“/tmp/mysql.sock”。