我相信我已经成功地部署了我的(非常基本的)网站到fortrabbit,但只要我连接到SSH运行一些命令(如php artisan migrate或php artisan db:seed),我得到一个错误消息:

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

在某种程度上,迁移一定是有效的,因为我的表在那里——但这并不能解释为什么它现在不适合我。


当前回答

在Laravel 5中,数据库用户名和密码保存在项目目录中的.env文件中,例如。

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

正如你所看到的,这些环境变量覆盖了这里的“forge”字符串,所以改变它们没有任何影响:

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

更多信息请访问https://mattstauffer.co/blog/laravel-5.0-environment-detection-and-environment-variables

其他回答

在我的情况下,我不得不删除引导/缓存文件夹,然后再试一次。

我的场景是在服务器迁移之后。

我遇到了同样的问题,我运行的是Mac OS X 10.10 Yosemite。我已经启用了操作系统自带的Apache服务器和PHP。然后,我刚刚配置了mCrypt库开始。在那之后,当我使用模型和DB时,我得到了一个错误:

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

我发现的原因只是因为PHP和MySQL不能自己连接。 为了解决这个问题,我遵循以下步骤:

Open a terminal and connect to the mysql with: mysql -u root -p It will ask you for the related password. Then once you get the mysql promt type the next command: mysql> show variables like '%sock%' You will get something like this: +-----------------------------------------+-----------------+ | Variable_name | Value | +-----------------------------------------+-----------------+ | performance_schema_max_socket_classes | 10 | | performance_schema_max_socket_instances | 322 | | socket | /tmp/mysql.sock | +-----------------------------------------+-----------------+ Keep the value of the last row: /tmp/mysql.sock In your laravel project folder, look for the database.php file there is where you configure the DB connection parameters. In the mysql section add the next line at the end: 'unix_socket' => '/tmp/mysql.sock' You must have something like this: 'mysql' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'SchoolBoard', 'username' => 'root', 'password' => 'venturaa', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'unix_socket' => '/tmp/mysql.sock', ),

现在只需保存更改,并重新加载页面,它必须工作!

我在使用docker容器运行应用程序时遇到了这个问题。

解决方案是把我正在使用的MySQL服务容器的名称放在docker_compose中。DB_HOST. yml。在我的例子中,它是db:

DB_HOST=db

希望能有所帮助。

解决了

在我的情况下,这是代码中的逻辑问题,连接值在if语句中:

if($_SERVER['HTTP_HOST'] == "localhost")

所以解决方案是添加一个管道,并添加127.0.0.1,这解决了我的问题

if($_SERVER['HTTP_HOST'] == "localhost" || $_SERVER['HTTP_HOST'] == "127.0.0.1")

这是因为PDO特别对待“localhost”主机:

注意:仅适用于Unix:当主机名设置为"localhost"时,则 到服务器的连接是通过域套接字进行的。如果PDO_MYSQL是 根据libmysqlclient编译后的套接字文件的位置 在libmysqlclient的编译位置。如果编译PDO_MYSQL mysqlnd的默认套接字可以通过 pdo_mysql.default_socket设置。

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

将localhost更改为127.0.0.1将“强制”使用TCP。

注意:mysqli_connect与localhost一起工作很好。