我相信我已经成功地部署了我的(非常基本的)网站到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
在某种程度上,迁移一定是有效的,因为我的表在那里——但这并不能解释为什么它现在不适合我。
当前回答
Mamp用户启用选项允许网络访问MYSQL
其他回答
如果你使用Laravel Homestead, 这里是设置
(包括流浪者-虚拟机)
.bash-profile
alias vm="ssh vagrant@127.0.0.1 -p 2222"
database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
终端
vm
vagrant@homestead:~/Code/projectFolder php artisan migrate:install
在我的例子中,我在我的mac终端上运行php artisan migrate,当我需要ssh进入vagrant并从那里运行它时。希望这能帮助缓解头痛。
我只是在。env文件中做了一个更改
我有如下一行代码。
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=root
DB_PASSWORD=
修改主机名localhost为127.0.0.1
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=root
DB_PASSWORD=
这是在我的情况下工作,因为它不能找到任何主机名,如localhost
并在更改主机名后编写以下命令
php artisan config:clear
php artisan migrate:install
php artisan migrate
出现这个错误的一个最简单的原因是MySQL服务器没有运行。先验证一下。如果是,继续其他建议:
Laravel 4:将app/config/database.php文件中的“host”从“localhost”改为“127.0.0.1” Laravel 5+:将。env文件中的“DB_HOST”从“localhost”更改为“127.0.0.1”
我也有同样的问题。以上的方法对我来说都没用。我通过将/app/config/database.php文件中的“主机”从“localhost”更改为“127.0.0.1”来解决这个问题。
不知道为什么“localhost”默认不工作,但我在symfony2帖子中找到了一个类似问题的答案。https://stackoverflow.com/a/9251924
更新: 有些人问为什么这个修复工作,所以我对这个主题做了一些研究。似乎他们使用不同的连接类型,解释在这篇文章https://stackoverflow.com/a/9715164
这里出现的问题是“localhost”使用UNIX套接字,无法在标准目录中找到数据库。但是“127.0.0.1”使用TCP(传输控制协议),这本质上意味着它在您的计算机上通过“本地互联网”运行,在这种情况下比UNIX套接字更可靠。
我遇到了同样的问题,我运行的是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', ),
现在只需保存更改,并重新加载页面,它必须工作!