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

前端 未结 10 2145
太阳男子
太阳男子 2020-12-05 11:19

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

相关标签:
10条回答
  • 2020-12-05 11:45

    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!

    0 讨论(0)
  • 2020-12-05 11:47

    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.

    0 讨论(0)
  • 2020-12-05 11:49

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

    php artisan cache:clear
    php artisan config:clear 
    php artisan view:clear
    
    0 讨论(0)
  • 2020-12-05 11:52

    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

    0 讨论(0)
提交回复
热议问题