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

时间:2017-07-28 21:53:32

标签: php laravel laravel-5 pdo

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

这是我的.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文件

'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,
],

当我加载页面时,它会出现以下错误:

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

1 个答案:

答案 0 :(得分:1)

我想你可以试试这个:

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

希望这对你有用!!!

希望这对你有用!!!