0

I just made an api that works on the localhost, but after deploying it to Heroku and sending a GET request thru the Postman (https://app_name.herokuapp.com/api/v1/ads) I get a 500 internal server error. When I check out heroku logs I get this:

2018-11-03T09:21:21.115723+00:00 heroku[router]: at=info method=GET path="/api/v1/ads/1" host=app_name.herokuapp.com request_id=ea2d91ec-876f-4734-bf3b-997cdd9a5cca fwd="79.150.223.197" dyno=web.1 connect=0ms service=26ms status=500 bytes=274 protocol=https

Inside my database.yml under the production environment I have my api linked to a external database like this:

production:
  <<: *default
  host: host_name
  database: db_name
  adapter: mysql2
  encoding: utf8
  username: user_name
  password: ENV['DB_PASSWORD']

From this source: Remote mysql database on Heroku app

heroku config:add DATABASE_URL=mysql://username:password@host:3306/dbname

And got back this:

Setting DATABASE_URL and restarting ⬢ app_name... !
 ▸    Cannot overwrite attachment values DATABASE_URL.

Then based question: How to change DATABASE_URL for a heroku application

Added a ClearDB mysql addon inside Heroku and removed the Postgres addon.

Then I did this:

heroku config:add CLEARDB_DATABASE_URL=mysql://username:password@host:3306/dbname

And got this back:

Setting CLEARDB_DATABASE_URL and restarting ⬢ app_name... done, v16
CLEARDB_DATABASE_URL: mysql://username:password@host:3306/dbname

Afterwards tried sending a new GET request to https://app_name.herokuapp.com/api/v1/ads and got this back:

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta charset="utf-8">
        <title>Application Error</title>
        <style media="screen">
          html,body,iframe {
            margin: 0;
            padding: 0;
          }
          html,body {
            height: 100%;
            overflow: hidden;
          }
          iframe {
            width: 100%;
            height: 100%;
            border: 0;
          }
        </style>
    </head>
    <body>
        <iframe src="//www.herokucdn.com/error-pages/application-error.html"></iframe>
    </body>
</html>

The heroku logs gave this:

2018-11-03T11:53:13.315968+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/api/v1/ads/1" host=app_name.herokuapp.com request_id=0f339471-a5b8-4402-9941-d3aad757f9c7 fwd="79.150.223.197" dyno= connect= service= status=503 bytes= protocol=https

I almost tried everything. Can someone help me please?

OKMantis
  • 95
  • 9

1 Answers1

0

Make sure to have all the Environment variables set

in your Heroku App environment, set the following:

CLEARDB_DATABASE_URL = mysql://username:password@host:3306
DB_PASSWORD = <YOUR PASSWORD>
DB_NAME = db_name

in database.yml

production:
  <<:       *default
  host:     ENV['CLEARDB_DATABASE_URL']
  database: ENV['DB_NAME']
  adapter:  mysql2
  encoding: utf8
  username: user_name
  password: ENV['DB_PASSWORD']

Please verify that the PORT in default is the same as your Production DB. If not: set port

production:
  <<:   *default
  port: 3306
  ...
Amanze
  • 288
  • 2
  • 11
  • 'Please verify that the PORT in default is the same as your Production DB. If not: set port' @Amanze How do I verify this exactly? – OKMantis Nov 03 '18 at 13:45
  • Sorry I still get this same message in heroku logs: ```2018-11-03T13:50:09.982629+00:00 heroku[router]: at=info method=GET path="/api/v1/ads" host=app_name-api.herokuapp.com request_id=81af0755-c569-44e3-876b-e460f3aa6bbb fwd="79.150.223.197" dyno=web.1 connect=1ms service=78ms status=500 bytes=274 protocol=https``` And a 500 internal server error. – OKMantis Nov 03 '18 at 13:52
  • @OKMantis in the database.yml, check in the default config if the port value is set to 3306. if so then you are ok. If not, in your production config section, set `port: 3306` – Amanze Nov 04 '18 at 14:11
  • Can you please share the snippet of your database.yml, so we can find the error? and the full Heroku logs too `heroku logs --tail` – Amanze Nov 04 '18 at 14:13