Laravel MySQL SQLSTATE[HY000] [2002] 连接被拒绝
Laravel MySQL SQLSTATE[HY000] [2002] Connection refused
所以我一直在尝试将我的数据库与 homestead 建立连接,但每次我创建 DB select 语句时,我都会收到 SQLSTATE[HY000] [2002] 连接被拒绝。看来我的问题与其他同标题的问题不一样。我正在使用 homestead 和虚拟框
这是我的 select 声明:
<?php
echo DB::select('select * from bruger where id = 1')
?>;
这只是为了检查我是否可以得到任何信息。
我在我的 .env 文件和 database.php
中尝试了各种方法
这是他们目前的样子:
'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,
],
和
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
对于 DB_HOST 和 DB_PORT 尝试使用 Homestead.yaml 文件中设置的 IP 和端口。
默认:
DB_HOST=192.168.10.10
DB_PORT=3306
端口应该是 3306,因为你是从 homestead 机器内部访问的,如果我没理解错的话。
事实上,检查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.
所以我一直在尝试将我的数据库与 homestead 建立连接,但每次我创建 DB select 语句时,我都会收到 SQLSTATE[HY000] [2002] 连接被拒绝。看来我的问题与其他同标题的问题不一样。我正在使用 homestead 和虚拟框
这是我的 select 声明:
<?php
echo DB::select('select * from bruger where id = 1')
?>;
这只是为了检查我是否可以得到任何信息。
我在我的 .env 文件和 database.php
中尝试了各种方法这是他们目前的样子:
'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,
],
和
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
对于 DB_HOST 和 DB_PORT 尝试使用 Homestead.yaml 文件中设置的 IP 和端口。
默认:
DB_HOST=192.168.10.10
DB_PORT=3306
端口应该是 3306,因为你是从 homestead 机器内部访问的,如果我没理解错的话。
事实上,检查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.