简体   繁体   中英

Connecting remote mysql database to heroku rails app

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?

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
  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM