問題描述
我正在 Laravel 5 中構(gòu)建一個 Web 應(yīng)用程序.該應(yīng)用程序應(yīng)該將類別名稱"存儲在 MySQL 數(shù)據(jù)庫中,并顯示一個表單以添加新的類別名稱".當(dāng)我執(zhí)行命令 php artisan serve
并導(dǎo)航到 http://localhost:8000/admin/categories/,我收到以下錯誤消息:
I am building a web application in Laravel 5. The application is supposed to get "category names" stored on a MySQL database and display a form to add new "category names". When I execute the command php artisan serve
and I navigate to http://localhost:8000/admin/categories/, I receive the following error message:
PDOException in Connector.php line 50:
SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
根據(jù)我讀到的關(guān)于堆棧溢出的幾篇文章,許多遇到此錯誤的用戶沒有正確配置 .env 文件,該文件覆蓋了在 database.php 文件中指定的 PHP 數(shù)據(jù)對象 (PDO) 的默認(rèn)設(shè)置..env 文件定義如下:
According to several posts I read on stack overflow, many users encountering this error did not properly configure the .env file, which overrides the default settings for the PHP Data Object (PDO), as specified in the database.php file. The .env file is defined below:
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
database.php 文件中的 mysql 鍵為:
And the mysql key within the database.php file is given as:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
奇怪的是,當(dāng)我通過 ssh 進入我的虛擬機并運行 mysql -uhomestead -psecret homestead
時,我能夠連接到數(shù)據(jù)庫.問題是,當(dāng)我可以用相同的參數(shù)直接連接到它時,為什么 Laravel 無法連接到 MySQL?還有什么可以拒絕訪問 Laravel?
Oddly, when I ssh into my virtual machine and I run mysql -uhomestead -psecret homestead
, I am able to connect to the database. The question is, why is Laravel unable to connect to MySQL when I can connect to it directly with the same parameters? What else could be denying access to Laravel?
推薦答案
我將 homestead.yaml 中指定的 ip 地址從 localhost 更改為 192.168.10.10.然后我運行了 homestead provision
,這似乎解決了問題.主機無法解析 localhost 或 127.0.0.1,因為它們已經(jīng)映射到自身.
I changed the ip address specified in the homestead.yaml from localhost to 192.168.10.10. Then I ran homestead provision
and that seemed to fix the problem. The host machine cannot resolve localhost or 127.0.0.1 because that is already mapped to itself.
這篇關(guān)于PHP Laravel:無法建立連接,因為目標(biāo)機器主動拒絕它的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!