Laravel 5.4:无法连接到在线服务器上的数据库

问题描述:

我刚刚将Laravel项目上载到Web服务器,但是无法将其连接到数据库.我已将数据库导入到phpmyadmin中,并且还设置了.envconfig/database.php文件.当我运行该页面时,它仍然显示我的本地主机数据库设置(用户名,密码,数据库等).

I have just uploaded a Laravel project to a web server but it cannot be connected to the database. I have imported the database to phpmyadmin, and have also set the .env and config/database.php file. When I run the page, it still shows me my localhost database settings (username, password, database etc).

这是我的.env文件

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:qvuzPrZW3awGcn8etObsyAT7SaKRfgr6AHfpnUPfygE=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://findajob.af/

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=database_name_on_server
DB_USERNAME=username_on_server
DB_PASSWORD=password_on_server

这是我的config/database.php文件

This is my config/database.php file

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', 'localhost'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'database_name_on_server'),
    'username' => env('DB_USERNAME', 'username_on_server'),
    'password' => env('DB_PASSWORD', 'password_on_server'),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => null,
],

当我加载页面时,它给我以下错误:

When I load the page, it gives me following errors:

QueryException in Connection.php line 647:
SQLSTATE[HY000] [2002] Connection refused (SQL: )

第二条错误消息是

PDOException in Connector.php line 68:
SQLSTATE[HY000] [2002] Connection refused

    1. in Connector.php line 68
    2. at PDO->__construct('mysql:host=127.0.0.1;port=3306;dbname=databasename_on_localhost', 'localhost_username', 'localhost_password', array(0, 2, 0, false, false)) in Connector.php line 68

我认为您可以尝试以下方法:

I think you can try this :

DB_HOST=hostname_from_server
DB_PORT=3306
DB_DATABASE=database_name_on_server
DB_USERNAME=username_on_server
DB_PASSWORD=password_on_server

清除缓存后,像这样:

php artisan config:cache
php artisan cache:clear

希望这项工作能为您服务!

Hope this work for you !!!

希望这项工作能为您服务!

Hope this work for you !!!