我刚刚制作了一个可以在本地主机上运行的 api,但是在将其部署到 Heroku 并通过 Postman 发送 GET 请求之后(https://app_name.herokuapp.com/api/v1/ads
) 我收到 500 内部服务器错误。当我查看 heroku 日志
时,我得到以下信息:
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
在生产环境下的 database.yml 中,我将 api 链接到外部数据库,如下所示:
production:
<<: *default
host: host_name
database: db_name
adapter: mysql2
encoding: utf8
username: user_name
password: ENV['DB_PASSWORD']
来自此来源:Remote mysql database on Heroku app
heroku config:add DATABASE_URL=mysql://username:password@host:3306/dbname
并得到了这个:
Setting DATABASE_URL and restarting ⬢ app_name... !
▸ Cannot overwrite attachment values DATABASE_URL.
然后基于问题:How to change DATABASE_URL for a heroku application
在 Heroku 中添加了 ClearDB mysql 插件,并删除了 Postgres 插件。
然后我这样做了:
heroku config:add CLEARDB_DATABASE_URL=mysql://username:password@host:3306/dbname
并得到了这个:
Setting CLEARDB_DATABASE_URL and restarting ⬢ app_name... done, v16
CLEARDB_DATABASE_URL: mysql://username:password@host:3306/dbname
随后尝试向 https://app_name.herokuapp.com/api/v1/ads
发送新的 GET 请求并得到了此请求:
<!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>
heroku 日志给出了这个:
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
我几乎尝试了一切。有人可以帮我吗?
最佳答案
确保设置了所有环境变量
在您的 Heroku 应用程序环境中,设置以下内容:
CLEARDB_DATABASE_URL = mysql://username:password@host:3306
DB_PASSWORD = <YOUR PASSWORD>
DB_NAME = db_name
在数据库.yml中
production:
<<: *default
host: ENV['CLEARDB_DATABASE_URL']
database: ENV['DB_NAME']
adapter: mysql2
encoding: utf8
username: user_name
password: ENV['DB_PASSWORD']
请验证默认的端口是否与您的生产数据库相同。如果不: 设置端口
production:
<<: *default
port: 3306
...
关于mysql - 将远程 mysql 数据库连接到 Heroku Rails 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53131294/