php PDOException SQLSTATE[HY000] [2002] 没有这样的文件或目录

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

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

phpmysqllaravelpdo

提问by Daniel Hollands

I believe that I've successfully deployed my (very basic) site to fortrabbit, but as soon as I connect to SSH to run some commands (such as php artisan migrateor php artisan db:seed) I get an error message:

我相信我已经成功地将我的(非常基本的)站点部署到了 fortrabbit,但是一旦我连接到 SSH 以运行一些命令(例如php artisan migratephp artisan db:seed),我就会收到一条错误消息:

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

At some point the migration must have worked, because my tables are there - but this doesn't explain why it isn't working for me now.

在某些时候,迁移肯定有效,因为我的表在那里 - 但这并不能解释为什么它现在对我不起作用。

采纳答案by ukautz

The error message indicates that a MySQL connection via socket is tried (which is not supported).

错误消息表明尝试通过套接字建立 MySQL 连接(不支持)。

In the context of Laravel (artisan), you probably want to use a different / the correct environment. Eg: php artisan migrate --env=production(or whatever environment). See here.

在 Laravel(工匠)的上下文中,您可能想要使用不同的/正确的环境。例如:(php artisan migrate --env=production或任何环境)。见这里

回答by stuyam

Laravel 4:Change "host" in the app/config/database.phpfile from "localhost" to "127.0.0.1"

Laravel 5:Change "DB_HOST" in the .envfile from "localhost" to "127.0.0.1"

Laravel 4:app/config/database.php文件中的“host”从“localhost”更改为“127.0.0.1”

Laravel 5:.env文件中的“DB_HOST”从“localhost”更改为“127.0.0.1”

I had the exact same problem. None of the above solutions worked for me. I solved the problem by changing the "host" in the /app/config/database.php file from "localhost" to "127.0.0.1".

我有同样的问题。以上解决方案都不适合我。我通过将 /app/config/database.php 文件中的“host”从“localhost”更改为“127.0.0.1”解决了这个问题。

Not sure why "localhost" doesn't work by default but I found this answer in a similar question solved in a symfony2 post. https://stackoverflow.com/a/9251924/1231563

不知道为什么默认情况下“localhost”不起作用,但我在 symfony2 帖子中解决的类似问题中找到了这个答案。https://stackoverflow.com/a/9251924/1231563

Update:Some people have asked as to why this fix works so I have done a little bit of research into the topic. It seems as though they use different connection types as explained in this post https://stackoverflow.com/a/9715164/1231563

更新:有些人问为什么这个修复有效,所以我对这个话题做了一些研究。似乎他们使用不同的连接类型,如本文中所述https://stackoverflow.com/a/9715164/1231563

The issue that arose here is that "localhost" uses a UNIX socket and can not find the database in the standard directory. However "127.0.0.1" uses TCP (Transmission Control Protocol), which essentially means it runs through the "local internet" on your computer being much more reliable than the UNIX socket in this case.

这里出现的问题是“本地主机”使用 UNIX 套接字并且在标准目录中找不到数据库。然而,“127.0.0.1”使用 TCP(传输控制协议),这实质上意味着它通过您计算机上的“本地互联网”运行,在这种情况下比 UNIX 套接字更可靠。

回答by alexventuraio

I got the same problem and I'm running Mac OS X 10.10 Yosemite. I have enabled the Apache Server and PHP that already comes with the OS. Then I just configured the mCrypt library to get started. After that when I was working with models and DB I got the error:

我遇到了同样的问题,我正在运行 Mac OS X 10.10 Yosemite。我已经启用了操作系统自带的 Apache 服务器和 PHP。然后我刚刚配置了 mCrypt 库就可以开始了。之后,当我使用模型和数据库时,出现错误:

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

The reason I found is just because PHP and MySQL can't get connected themselves. To get this problem fixed, I follow the next steps:

我发现的原因只是因为 PHP 和 MySQL 无法自行连接。为了解决这个问题,我按照以下步骤操作:

  1. Open a terminal and connect to the mysql with:

    mysql -u root -p
    
  2. It will ask you for the related password. Then once you get the mysql promt type the next command:

    mysql> show variables like '%sock%'
    
  3. You will get something like this:

    +-----------------------------------------+-----------------+
    | Variable_name                           | Value           |
    +-----------------------------------------+-----------------+
    | performance_schema_max_socket_classes   | 10              |
    | performance_schema_max_socket_instances | 322             |
    | socket                                  | /tmp/mysql.sock |
    +-----------------------------------------+-----------------+
    
  4. Keep the value of the last row:

    /tmp/mysql.sock
    
  5. In your laravelproject folder, look for the database.phpfile there is where you configure the DB connection parameters. In the mysqlsection add the next line at the end:

    'unix_socket' => '/tmp/mysql.sock'
    
  6. You must have something like this:

    'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'SchoolBoard',
            'username'  => 'root',
            'password'  => 'venturaa',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'unix_socket' => '/tmp/mysql.sock',
        ),
    
  1. 打开终端并使用以下命令连接到 mysql:

    mysql -u root -p
    
  2. 它会询问您相关的密码。然后一旦你得到 mysql promt 输入下一个命令:

    mysql> show variables like '%sock%'
    
  3. 你会得到这样的东西:

    +-----------------------------------------+-----------------+
    | Variable_name                           | Value           |
    +-----------------------------------------+-----------------+
    | performance_schema_max_socket_classes   | 10              |
    | performance_schema_max_socket_instances | 322             |
    | socket                                  | /tmp/mysql.sock |
    +-----------------------------------------+-----------------+
    
  4. 保留最后一行的值:

    /tmp/mysql.sock
    
  5. 在您的laravel项目文件夹中,查找database.php文件,您可以在其中配置数据库连接参数。在mysql部分的最后添加下一行:

    'unix_socket' => '/tmp/mysql.sock'
    
  6. 你必须有这样的东西:

    'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'SchoolBoard',
            'username'  => 'root',
            'password'  => 'venturaa',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'unix_socket' => '/tmp/mysql.sock',
        ),
    

Now just save changes, and reload the page and it must work!

现在只需保存更改,然后重新加载页面,它就可以工作了!

回答by dcarrith

I encountered the [PDOException] SQLSTATE[HY000] [2002] No such file or directoryerror for a different reason. I had just finished building a brand new LAMP stack on Ubuntu 12.04 with Apache 2.4.7, PHP v5.5.10 and MySQL 5.6.16. I moved my sites back over and fired them up. But, I couldn't load my Laravel 4.2.x based site because of the [PDOException]above. So, I checked php -i | grep pdoand noticed this line:

[PDOException] SQLSTATE[HY000] [2002] No such file or directory由于不同的原因,我遇到了错误。我刚刚在 Ubuntu 12.04 上用 Apache 2.4.7、PHP v5.5.10 和 MySQL 5.6.16 构建了一个全新的 LAMP 堆栈。我将我的网站移回并启动了它们。但是,由于[PDOException]上述原因,我无法加载基于 Laravel 4.2.x 的站点。所以,我检查php -i | grep pdo并注意到这一行:

pdo_mysql.default_socket => /tmp/mysql.sock => /tmp/mysql.sock

But, in my /etc/my.cnf the sock file is actually in /var/run/mysqld/mysqld.sock.

但是,在我的 /etc/my.cnf 中,sock 文件实际上位于/var/run/mysqld/mysqld.sock.

So, I opened up my php.ini and set the value for pdo_mysql.default_socket:

所以,我打开了我的 php.ini 并设置了以下值pdo_mysql.default_socket

pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock

Then, I restarted apache and checked php -i | grep pdo:

然后,我重新启动 apache 并检查php -i | grep pdo

pdo_mysql.default_socket => /var/run/mysqld/mysqld.sock => /var/run/mysqld/mysqld.sock

That fixed it for me.

那为我修好了。

回答by Marlow

The answer from @stuyam solved the "No such file or directory" issue for me

@stuyam 的回答为我解决了“没有这样的文件或目录”问题

Short answer: Change "host" in the /app/config/database.php file from "localhost" to "127.0.0.1"

简短回答:将 /app/config/database.php 文件中的“host”从“localhost”更改为“127.0.0.1”

But then I had a "Connection refused" error. If anyone had the same issue, my solution for this was to update the app/config/local/database.php file so the port is 8889:

但是后来我遇到了“连接被拒绝”错误。如果有人遇到同样的问题,我的解决方案是更新 app/config/local/database.php 文件,使端口为 8889:

'mysql' => array(
        'driver'    => 'mysql',
        'host'      => '127.0.0.1',
        'port'      => '8889',
        'database'  => 'databaseName',
        'username'  => 'root',
        'password'  => 'root',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

回答by Koen B.

If you are using Laravel Homestead, make sure you're calling the commands on the server.

如果您使用的是 Laravel Homestead,请确保您正在调用服务器上的命令。

homestead ssh

Then simply cd to the right directory and fire your command there.

然后只需 cd 到正确的目录并在那里启动您的命令。

回答by Carlos ABS

In my case i had no problem at all, just forgot to start the mysql service...

就我而言,我完全没有问题,只是忘了启动 mysql 服务...

sudo service mysqld start

回答by George John

Mamp user enable option Allow network access to MYSQL

Mamp 用户启用选项允许网络访问 MYSQL

enter image description here

在此处输入图片说明

回答by RaviNila

Add mysql.sock path in database.php file like below example

在 database.php 文件中添加 mysql.sock 路径,如下例所示

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

Eample

示例

'mysql' => [
        'driver' => 'mysql',
        'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '8889'),

回答by NuOne

It worked after I change from DB_HOST=localhostto DB_HOST=127.0.0.1at .env file

它的工作我的变化后,从DB_HOST=localhostDB_HOST=127.0.0.1在.ENV文件