douyo770657
2018-06-03 20:15
浏览 97
已采纳

SQLSTATE [HY000] [2002]尝试将连接laravel数据库添加到Heroku时拒绝连接

So I have a site on Heroku, which loads fine as long I don't go to a page that requires a database connection. What happens when I do land on one of those pages, is I get this error:

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

I'm using db4free.net to try to access a MySQL database. This is my database.php file. The ****** masking the actual values.

'default' => env('DB_CONNECTION', 'mysql-production'),

'mysql-production' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'db4free.net'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'en*******'),
            'username' => env('DB_USERNAME', '*******'),
            'password' => env('DB_PASSWORD', '*******'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

This my env file:

APP_ENV=local
APP_DEBUG=true
APP_KEY=base64:tUWa6AteuAOfEAh/IKx3c13tJEzSmS4La8QdKlEgByQ=
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=enviro-2
DB_USERNAME=root
DB_PASSWORD=

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

The env file is ignored by git.

Prior to trying db4free.net I tried following the steps in this article https://mattstauffer.com/blog/laravel-on-heroku-using-a-postgresql-database/ but I still have the exact same problem. I also set the APP_KEY in the Heroku config variables to match the one in my env file.

Anyone have any ideas on why I get this error?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • doulubashang6936 2018-06-03 21:13
    最佳回答

    your connection details in .env file in incorrect. you should write your credentials details in .env file as well as database.php file

    评论
    解决 无用
    打赏 举报