简体   繁体   中英

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

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}

You need to change host from localhost to 127.0.0.1

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

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

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!

Restart your database and local web server:

sudo service mysqld restart

It should work!

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

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.

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

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

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

On Mac OSX/MAMP you may find a mysql.sock.lock file in your /Applications/MAMP/tmp/mysql folder.

You can safely remove the file mysql.sock.lock and you should be in good shape.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM