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

后端 未结 30 2573
南方客
南方客 2020-11-22 03:23

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 migrate

相关标签:
30条回答
  • 2020-11-22 03:35

    As of Laravel 5 the database username and password goes in the .env file that exists in the project directory, e.g.

    DB_HOST=127.0.0.1
    DB_DATABASE=db1
    DB_USERNAME=user1
    DB_PASSWORD=pass1
    

    As you can see these environment variables are overriding the 'forge' strings here so changing them has no effect:

        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],
    

    More information is here https://mattstauffer.co/blog/laravel-5.0-environment-detection-and-environment-variables

    0 讨论(0)
  • 2020-11-22 03:36

    This is because PDO treats "localhost" host specially:

    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.

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

    Changing localhost to 127.0.0.1 will "force" the use of TCP.

    Note: mysqli_connect is working fine with localhost.

    0 讨论(0)
  • 2020-11-22 03:36

    Attempt to connect to localhost:

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

    Attempt to connect to 127.0.0.1:

    SQLSTATE[HY000] [2002] Connection refused
    

    OK, just comment / remove the following setting from my.cnf (on OS X 10.5: /opt/local/etc/mysqlxx/my.cnf) to obtain:

    [mysqld]
    # skip-networking
    

    Of course, stop and start MySQL Server.

    0 讨论(0)
  • 2020-11-22 03:38

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

    sudo service mysqld start
    
    0 讨论(0)
  • 2020-11-22 03:39

    Building on the answer from @dcarrith ...

    Instead of editing the config files, I created an alias in the location that PHP is looking that connects to the real mysql.sock. (source)

    Just run these two commands (no restart needed):

    mkdir /var/mysql
    ln -s /tmp/mysql.sock /var/mysql/mysql.sock
    
    0 讨论(0)
  • 2020-11-22 03:40

    Check your port carefully . In my case it was 8889 and i am using 8888. change "DB_HOST" from "localhost" to "127.0.0.1" and vice versa

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