title: SQLSTATE[HY000] [2002] Connection refused when trying to add connect laravel database to Heroku
body: <p>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:</p>
  
  <p><code>PDOException in Connector.php line 55: SQLSTATE[HY000] [2002] Connection refused</code></p>
  
  <p>I'm using <a href="http://db4free.net" rel="nofollow noreferrer">db4free.net</a> to try to access a MySQL database. This is my database.php file. The ****** masking the actual values.</p>
  
  <pre><code>'default' =&gt; env('DB_CONNECTION', 'mysql-production'),
  
  'mysql-production' =&gt; [
              'driver' =&gt; 'mysql',
              'host' =&gt; env('DB_HOST', 'db4free.net'),
              'port' =&gt; env('DB_PORT', '3306'),
              'database' =&gt; env('DB_DATABASE', 'en*******'),
              'username' =&gt; env('DB_USERNAME', '*******'),
              'password' =&gt; env('DB_PASSWORD', '*******'),
              'unix_socket' =&gt; env('DB_SOCKET', ''),
              'charset' =&gt; 'utf8mb4',
              'collation' =&gt; 'utf8mb4_unicode_ci',
              'prefix' =&gt; '',
              'strict' =&gt; true,
              'engine' =&gt; null,
          ],
  </code></pre>
  
  <p>This my env file:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The env file is ignored by git.</p>
  
  <p>Prior to trying db4free.net I tried following the steps in this article
  <a href="https://mattstauffer.com/blog/laravel-on-heroku-using-a-postgresql-database/" rel="nofollow noreferrer">https://mattstauffer.com/blog/laravel-on-heroku-using-a-postgresql-database/</a> 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.</p>
  
  <p>Anyone have any ideas on why I get this error?</p>