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

匿名 (未验证) 提交于 2019-12-03 01:34:02

问题:

I have put PushChatServer dir in htdocs folder and create database puschat try to run @"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

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}

回答1:

Quick test (run in shell):

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

SQLSTATE[HY000] [2002] No such file or directory means 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] [1044] Access denied for user 'username'@'localhost' CONGRATULATION! You have the correct mysql.default_socket setting now. Fix your dbname/username/password.

Also see Error on creating connection to PDO in PHP



回答2:

Laravel 4: In your app/config/database.php try changing host from localhost to 127.0.0.1

Laravel 5: In the .env file, change DB_HOST from localhost to 127.0.0.1

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



回答3:

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

$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:

$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!



回答4:

Restart your database and local web server:

  • sudo service mysqld restart
  • sudo service mysqld restart

It should work!



回答5:

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'.

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),     ] 


回答6:

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

<?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



回答7:

PDO treats localhost very particularly:

From 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.

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

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



回答8:

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:

"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!



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!