php SQLSTATE[HY000] [2002] 没有那个文件或目录

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

SQLSTATE[HY000] [2002] No such file or directory

phpmysql

提问by Crystal

I am trying to run a laravel project on OS X. I installed MySql with homebrew. I tried googling around for why I'm getting this error:

我正在尝试在 OS X 上运行 laravel 项目。我用自制软件安装了 MySql。我试着在谷歌上搜索为什么我会收到这个错误:

SQLSTATE[HY000] [2002] No such file or directory

when I try to do

当我尝试做

php artisan migrate

And most of the answers I see say php and MySql cannot communicate to each other. I've been trying to see where I went wrong since I think I followed previous posts about it, and it still doesn't work. I created a page that just does this:

我看到的大多数答案都说 php 和 MySql 无法相互通信。我一直试图找出我哪里出错了,因为我认为我遵循了以前的帖子,但它仍然不起作用。我创建了一个页面来执行此操作:

<?php
    phpinfo();
?>

And I see these types of things: In the Configure area, I see:

我看到这些类型的东西: 在配置区域,我看到:

'--with-mysql-sock=/tmp/mysql.sock' '--with-mysqli=mysqlnd' '--with-mysql=mysqlnd' '--with-pdo-mysql=mysqlnd' '--enable-pcntl' '--enable-zend-signals' '--enable-dtrace'

loaded configuration file:

加载的配置文件:

/usr/local/etc/php/5.4/php.ini

If I ls /tmp, I do see mysql.sock.

如果我ls /tmp,我确实看到了 mysql.sock。

In that php.ini file, I set these lines: pdo_mysql.default_socket=/tmp/mysql.sock mysql.default_socket = /tmp/mysql.sock mysqli.default_socket = /tmp/mysql.sock

在那个 php.ini 文件中,我设置了以下几行: pdo_mysql.default_socket=/tmp/mysql.sock mysql.default_socket = /tmp/mysql.sock mysqli.default_socket = /tmp/mysql.sock

And then in /usr/local/Cellar/mysql/5.6.16/my.cnf, I added the following line:

然后在 /usr/local/Cellar/mysql/5.6.16/my.cnf 中,我添加了以下行:

socket = /tmp/mysql.sock

I DID restart apache, as well as check mysql is running. I haven't seen any other advice besides the php and mysql connection. I think there are two versions of php on my system because when I installed mcrypt or something else I can't remember, it installed another php version. But when I do look at the output of phpinfo, I'm editing the appropriate php.ini file. I didn't know if there is another piece I am missing. Any thoughts? Thanks

我确实重新启动了 apache,并检查了 mysql 是否正在运行。除了 php 和 mysql 连接之外,我还没有看到任何其他建议。我认为我的系统上有两个版本的 php,因为当我安装 mcrypt 或其他我不记得的东西时,它安装了另一个 php 版本。但是当我查看 phpinfo 的输出时,我正在编辑相应的 php.ini 文件。我不知道是否还有我遗漏的部分。有什么想法吗?谢谢

采纳答案by Crystal

After searching, I found this post:

经过搜索,我找到了这个帖子:

Setting up Laravel on a Mac php artisan migrate error: No such file or directory

在 Mac 上设置 Laravel php artisan migrate 错误:没有这样的文件或目录

Once I added

一旦我添加

'unix_socket' => '/tmp/mysql.sock',

to my database.php in the mysql array, it worked. Not sure exactly why that works, but it does.

到我的 mysql 数组中的 database.php,它起作用了。不确定为什么会这样,但确实如此。

回答by HappyCoder

I had a similar error. I am using MAMP and what solved my issue was:

我有一个类似的错误。我正在使用 MAMP,解决我的问题的是:

  1. creating a symbolic link in: /var/mysql (create the directory if it does not exist)
  2. cd /var/mysql && sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock
  1. 在以下位置创建符号链接:/var/mysql(如果目录不存在则创建该目录)
  2. cd /var/mysql && sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock

回答by F3L1X79

For users which are looking for a solution on XAMPP (Tested and worked on Mac OS X),

对于在 XAMPP 上寻找解决方案的用户(在 Mac OS X 上测试和工作),

Open terminal and create a symbolic link :

打开终端并创建一个符号链接:

sudo mkdir /var/mysql
cd /var/mysql
sudo ln -s /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock

回答by Steve

I found PHP on CentOS 6 built with mysql.default_socket=/var/lib/mysql/mysql.sockyet for some reason pdo_mysql.default_socket=/tmp/mysql.sock, which doesn't exist. Building it using --with-mysql-sock=/var/lib/mysql/mysql.sockshould force both to be the same.

mysql.default_socket=/var/lib/mysql/mysql.sock由于某种原因pdo_mysql.default_socket=/tmp/mysql.sock,我在 CentOS 6 上发现了 PHP,但它并不存在。使用构建它--with-mysql-sock=/var/lib/mysql/mysql.sock应该强制两者相同。

Alternatively edit php.inito contain:

或者编辑php.ini以包含:

pdo_mysql.default_socket="/var/lib/mysql/mysql.sock"

...or your correct socket location, of course.

...当然,或者您正确的插座位置。

回答by gamal

i fixed it like this.Just add a line. in laravel project APP=>Config=>database.php update the
Connections=>mysql after strict add below line

我像这样修复它。只需添加一行。在 Laravel 项目 APP=>Config=>database.php 中
严格添加以下行后更新Connections=>mysql

'unix_socket' => "/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock"

sample:-

样本:-

'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
            'unix_socket' => "/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock"
        ],

回答by Martian.titan

Found the solution for Ampps user:
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"

为 Ampps 用户找到了解决方案:
我遇到了同样的问题,我进行了大量搜索,但在查看 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 Stef

This happened to me out of nowhere, my app (Laravel) was working fine yesterday and dropped today with this error (SQLSTATE[HY000] [2002] No such file or directory).

这突然发生在我身上,我的应用程序(Laravel)昨天运行良好,今天因此错误而掉线(SQLSTATE [HY000] [2002] 没有这样的文件或目录)。

After checking that my config file had not been not corrupted, I connected to my control panel (I am using a VPS) and restarted MySQL server. That did the trick, everything is back online.

在检查我的配置文件没有损坏后,我连接到我的控制面板(我使用的是 VPS)并重新启动 MySQL 服务器。成功了,一切都重新上线了。