Laravel MySQL SQLSTATE[HY000] [2002] 连接被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38147396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel MySQL SQLSTATE[HY000] [2002] Connection refused
提问by captainooze
So I have been trying to get an connection my database with homestead, but everytime I make a DB select statement I get an SQLSTATE[HY000] [2002] Connection refused. It seems like my problem is not the same as the others with the same title. I am using homestead with virtual box
所以我一直在尝试将我的数据库与 homestead 建立连接,但是每次我执行数据库选择语句时,我都会收到 SQLSTATE[HY000] [2002] 连接被拒绝。似乎我的问题与具有相同标题的其他问题不同。我正在使用带有虚拟盒子的宅基地
Here is my select statement:
这是我的选择语句:
<?php
echo DB::select('select * from bruger where id = 1')
?>;
This is simply to check if I can get any info out.
这只是为了检查我是否可以获得任何信息。
I have tried various things in both my .env file and and database.php
我在我的 .env 文件和 database.php 中尝试了各种方法
This is what they are looking like currently:
这是他们目前的样子:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
and
和
APP_ENV=local
APP_DEBUG=true
APP_KEY=randomKey
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=33060
DB_DATABASE=phpDatabase
DB_USERNAME=homestead
DB_PASSWORD=secret
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=secret
REDIS_PORT=33060
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
回答by trommelaap
For DB_HOST and DB_PORT try using the IP and port as set in the Homestead.yaml file.
对于 DB_HOST 和 DB_PORT,请尝试使用 Homestead.yaml 文件中设置的 IP 和端口。
Default:DB_HOST=192.168.10.10
DB_PORT=3306
默认:DB_HOST=192.168.10.10
DB_PORT=3306
回答by alepeino
The port should be 3306, since you are accessing from within the homestead machine, if I understood correctly.
如果我理解正确的话,端口应该是 3306,因为您是从宅基地机器内部访问的。
In fact, check the documentation
事实上,检查文档
You will use the default 3306 and 5432 ports in your Laravel database configuration file since Laravel is running within the virtual machine.
由于 Laravel 在虚拟机中运行,因此您将在 Laravel 数据库配置文件中使用默认的 3306 和 5432 端口。