我相信我已经成功地部署了我的(非常基本的)网站到fortrabbit,但只要我连接到SSH运行一些命令(如php artisan migrate或php artisan db:seed),我得到一个错误消息:
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
在某种程度上,迁移一定是有效的,因为我的表在那里——但这并不能解释为什么它现在不适合我。
我相信我已经成功地部署了我的(非常基本的)网站到fortrabbit,但只要我连接到SSH运行一些命令(如php artisan migrate或php artisan db:seed),我得到一个错误消息:
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
在某种程度上,迁移一定是有效的,因为我的表在那里——但这并不能解释为什么它现在不适合我。
当前回答
我遇到了同样的问题,我运行的是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', ),
现在只需保存更改,并重新加载页面,它必须工作!
其他回答
这发生在我身上是因为MySQL没有运行。MySQL无法启动,因为我丢失了/usr/local/etc/my.cnf.d/目录。
这是我的/usr/local/etc/my.cnf配置文件作为glob include(包括/usr/local/etc/my.cnf.d/*.cnf)所要求的。
运行mkdir /usr/local/etc/my.cnf.d,然后启动MySQL,解决了这个问题。
我在docker_compose中使用Docker和MySQL服务名db时遇到了同样的问题。yml文件:
我在.env文件中添加了以下内容:
DB_HOST=db
您还应该确保可以从PHP应用程序中发现您的主机。
这是因为PHP没有弄清楚使用哪个主机来连接。
在我的例子中,我在我的mac终端上运行php artisan migrate,当我需要ssh进入vagrant并从那里运行它时。希望这能帮助缓解头痛。
由于不同的原因,我遇到了[PDOException] SQLSTATE[HY000] [2002] No这样的文件或目录错误。我刚刚在Ubuntu 12.04上用Apache 2.4.7、PHP v5.5.10和MySQL 5.6.16构建了一个全新的LAMP堆栈。我把我的网站搬了回来,重新启动。但是,我无法加载我的Laravel 4.2。基于x的站点,因为上面的[PDOException]。所以,我检查了php -i | grep pdo,并注意到这一行:
pdo_mysql.default_socket => /tmp/mysql.sock => /tmp/mysql.sock
但是,在我的/etc/my.cnf中,sock文件实际上在/var/run/mysqld/mysqld.sock中。
因此,我打开php.ini并设置pdo_mysql.default_socket的值:
pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock
然后,我重新启动apache并检查php -i | grep pdo:
pdo_mysql.default_socket => /var/run/mysqld/mysqld.sock => /var/run/mysqld/mysqld.sock
这为我解决了问题。
我在Elixir/Gulp和Homestead中运行PHPUnit时遇到了这个问题。
在我的例子中,我将.env文件从DB_HOST=localhost编辑为DB_HOST=192.168.10.10,其中192.168.10.10是我的Vagrant/Homestead主机的IP。