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

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

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

phpmamp

提问by hhwithgroups

I have put PushChatServer dir in htdocs folder and create database puschat try to run @"http://localhost/PushChatServer/api/test/database.php"

我已将 PushChatServer 目录放在 htdocs 文件夹中并创建数据库 puschat 尝试运行 @" http://localhost/PushChatServer/api/test/database.php"

Then I got following exception.

然后我得到了以下异常。

I want do same thing to explain this link http://www.raywenderlich.com/32963/apple-push-notification-services-in-ios-6-tutorial-part-2

我想做同样的事情来解释这个链接http://www.raywenderlich.com/32963/apple-push-notification-services-in-ios-6-tutorial-part-2

I have done all that but I got this exception

我已经完成了所有这些,但我得到了这个例外

Could not connect to the database. Reason: exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /Applications/MAMP/htdocs/PushChatServer/api/test/database.php:17 Stack trace: #0 /Applications/MAMP/htdocs/PushChatServer/api/test/database.php(17): PDO->__construct('mysql:host=loca...', 'root', 'root', Array) #1 {main}

无法连接到数据库。原因:在 /Applications/MAMP/htdocs/PushChatServer/api/test/database.php:17 堆栈跟踪:#0 /Applications/MAMP 中出现消息“SQLSTATE[HY000] [2002] No such file or directory”的异常“PDOException” /htdocs/PushChatServer/api/test/database.php(17): PDO->__construct('mysql:host=loca...', 'root', 'root', Array) #1 {main}

回答by Adrian Enriquez

You need to change host from localhostto 127.0.0.1

您需要将主机从 更改localhost127.0.0.1

Laravel 4: In your app/config/database.phptry changing host from localhostto 127.0.0.1

Laravel 4:在您app/config/database.php尝试将主机从 更改localhost127.0.0.1

Laravel 5: In the .envfile, change DB_HOSTfrom localhostto 127.0.0.1

Laravel 5:在.env文件,更改DB_HOSTlocalhost127.0.0.1

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

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

回答by hhwithgroups

Quick test (run in shell):

快速测试(在 shell 中运行):

php -r "new PDO('mysql:hostname=localhost;dbname=test', 'username', 'password');"

SQLSTATE[HY000] [2002] No such file or directorymeans php cannot find the mysql.default_socket file. Fix it by modifying php.ini file. On Mac it is mysql.default_socket = /tmp/mysql.sock(See PHP - MySQL connection not working: 2002 No such file or directory)

SQLSTATE[HY000] [2002] 没有这样的文件或目录意味着 php 找不到 mysql.default_socket 文件。通过修改 php.ini 文件修复它。在 Mac 上它是mysql.default_socket = /tmp/mysql.sock(见PHP - MySQL connection not working: 2002 No such file or directory

SQLSTATE[HY000] [1044] Access denied for user 'username'@'localhost'CONGRATULATION! You have the correct mysql.default_socket setting now. Fix your dbname/username/password.

SQLSTATE[HY000] [1044] 用户 'username'@'localhost' 的访问被拒绝恭喜!您现在拥有正确的 mysql.default_socket 设置。修复您的数据库名称/用户名/密码。

Also see Error on creating connection to PDO in PHP

另请参阅在 PHP 中创建与 PDO 的连接时出错

回答by Isa Souza

I had the same error using PHP 5.6.30 (macOS Sierra) with the simple PDO code like:

我在使用 PHP 5.6.30 (macOS Sierra) 和简单的 PDO 代码时遇到了同样的错误,例如:

$pdo = new PDO('mysql:host=localhost;dbname=db_php', 'root', '');

And I received the same error message. To fix, I changed "localhost" for IP (loopback) "127.0.0.1" in my code:

我收到了同样的错误信息。为了解决这个问题,我在我的代码中为 IP(环回)“127.0.0.1”更改了“localhost”:

$pdo = new PDO('mysql:host=127.0.0.1;dbname=db_php', 'root', '');

To test the connection:

要测试连接:

$ php -r "new PDO('mysql:host=127.0.0.1;dbname=db_php', 'root', '');"

This work's for me!

这份工作是给我的!

回答by RAWGIT

Restart your database and local web server:

重新启动您的数据库和本地 Web 服务器:

  • sudo service mysqld restart
  • sudo service mysqld restart
  • 须藤服务 mysqld 重启
  • 须藤服务 mysqld 重启

It should work!

它应该工作!

回答by Thomas Decaux

PDO treats localhost very particularly:

PDO 非常特别地对待 localhost:

From http://php.net/manual/en/ref.pdo-mysql.connection.php:

http://php.net/manual/en/ref.pdo-mysql.connection.php

Note: Unix only: When the host name is set to "localhost", then the connection to the server is made thru a domain socket. If PDO_MYSQL is compiled against libmysqlclient then the location of the socket file is at libmysqlclient's compiled in location. If PDO_MYSQL is compiled against mysqlnd a default socket can be set thru the pdo_mysql.default_socket setting.

注意:仅限 Unix:当主机名设置为“localhost”时,将通过域套接字连接到服务器。如果 PDO_MYSQL 是针对 libmysqlclient 编译的,则套接字文件的位置位于 libmysqlclient 的编译位置。如果 PDO_MYSQL 是针对 mysqlnd 编译的,则可以通过 pdo_mysql.default_socket 设置来设置默认套接字。

That why PDO and mysql_connect will give different behavior for localhost.

这就是为什么 PDO 和 mysql_connect 会给本地主机不同的行为。

So, if you want to use a TCP connection with PDO, never use localhost but 127.0.0.1.

所以,如果你想使用 PDO 的 TCP 连接,千万不要使用 localhost,而是 127.0.0.1。

回答by Kuhan

I had the same error for Mysql PDO, this code works for me!

我对 Mysql PDO 有同样的错误,这段代码对我有用!

<?php
$dsn = 'mysql:host=127.0.0.1;dbname=testdb';
$username = 'username';
$password = 'password';
$options = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
); 

$dbh = new PDO($dsn, $username, $password, $options);
?>

got this code from : http://php.net/manual/en/ref.pdo-mysql.connection.php

从以下代码获取此代码:http: //php.net/manual/en/ref.pdo-mysql.connection.php

回答by Kuhan

I'm not sure if this will work for you; but I use CakePHP, and I get this error whenever I forget to put the port number at the end of the 'host'.

我不确定这是否适合你;但是我使用 CakePHP,每当我忘记将端口号放在“主机”的末尾时,就会出现此错误。

Hope this helps!

希望这可以帮助!

Before

'test' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'username' => 'root',
        'password' => 'root',
        'database' => 'mylogin',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,
        'quoteIdentifiers' => false,
        'log' => false,
        //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
        'url' => env('DATABASE_TEST_URL', null),
    ]

After

'test' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost:8080',
        'username' => 'root',
        'password' => 'root',
        'database' => 'mylogin',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,
        'quoteIdentifiers' => false,
        'log' => false,
        //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
        'url' => env('DATABASE_TEST_URL', null),
    ]

回答by Tauhed

Run the following command in shell, I think it will help.

在 shell 中运行以下命令,我认为它会有所帮助。

php artisan cache:clear
php artisan config:clear 
php artisan view:clear

回答by Obsidian

Just ran into this same issue and the problem is the password. In the tutorial the author lists the password as:

刚刚遇到同样的问题,问题是密码。在教程中,作者将密码列为:

"d]682\#%yl1nb3"

So as per the suggestion given by @Marki555, I looked in the config file - api_config.php. and the password listed there is:

因此,根据@Marki555 给出的建议,我查看了配置文件 - api_config.php。那里列出的密码是:

"d]682\#%yI1nb3"

The upper case 'I' is what caused the issue because the password you set for user on the db has the password with the lowercase l but it really is looking for the uppercase I. After I changed the pushchat user's password to have an uppercase i, it worked!

大写的“I”是导致问题的原因,因为您在 db 上为用户设置的密码的密码是小写的 l 但它确实在寻找大写的 I。在我将 pushchat 用户的密码更改为大写后我, 有效!