My Mad Libs程序(code,Heroku app; earlier version on repl.it)在我的Ubuntu VirtualBox中正常运行,在将代码推送到Heroku后几分钟,它实际上正在运行Heroku也是。然后在大概15到30分钟之后,我在服务器上做了一些事情或事情,并且自从应用页面说出"内部服务器错误"以及服务器日志的相关部分:
2018-04-14T02:03:45.717579+00:00 heroku[web.1]: Starting process with command `bundle exec ruby app.rb -p 7330`
2018-04-14T02:03:48.443468+00:00 app[web.1]: [2018-04-14 02:03:48] INFO ruby 2.4.0 (2016-12-24) [x86_64-linux]
2018-04-14T02:03:48.443866+00:00 app[web.1]: == Sinatra (v2.0.1) has taken the stage on 7330 for production with backup from WEBrick
2018-04-14T02:03:48.443437+00:00 app[web.1]: [2018-04-14 02:03:48] INFO WEBrick 1.3.1
2018-04-14T02:03:48.444166+00:00 app[web.1]: [2018-04-14 02:03:48] INFO WEBrick::HTTPServer#start: pid=4 port=7330
2018-04-14T02:03:48.855874+00:00 heroku[web.1]: State changed from starting to up
2018-04-14T02:04:04.372745+00:00 heroku[router]: at=info method=GET path="/" host=youmadlibs.herokuapp.com request_id=118d681f-3402-44aa-9d04-75d930461195 fwd="74.135.49.116" dyno=web.1 connect=0ms service=20ms status=500 bytes=854 protocol=https
2018-04-14T02:04:04.368607+00:00 app[web.1]: 2018-04-14 02:04:04 - PG::ConnectionBad - could not connect to server: Connection refused
2018-04-14T02:04:04.368644+00:00 app[web.1]: Is the server running on host "localhost" (127.0.0.1) and accepting
2018-04-14T02:04:04.368648+00:00 app[web.1]: TCP/IP connections on port 5432?
2018-04-14T02:04:04.368651+00:00 app[web.1]: :
2018-04-14T02:04:04.368653+00:00 app[web.1]: /app/vendor/bundle/ruby/2.4.0/gems/pg-1.0.0/lib/pg.rb:56:in `initialize'
2018-04-14T02:04:04.368654+00:00 app[web.1]: /app/vendor/bundle/ruby/2.4.0/gems/pg-1.0.0/lib/pg.rb:56:in `new'
2018-04-14T02:04:04.368656+00:00 app[web.1]: /app/vendor/bundle/ruby/2.4.0/gems/pg-1.0.0/lib/pg.rb:56:in `connect'
2018-04-14T02:04:04.368659+00:00 app[web.1]: /app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:684:in `connect'
etc.
相关的行似乎是:
PG::ConnectionBad - could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?
我尝试了很多东西,比如重新启动服务器,将dynos减少到0,然后重新升级到1,然后运行$ heroku run psql -h HEROKU_POSTGRESQL_SILVER_URL -U deploy youmadlibs
。我阅读了Heroku文档,用于将Ruby连接到数据库,而对于database.yml,它有host: localhost
。我试过了(尽管它之前已经使用了我在下面的设置,我从教程中获取)。我还尝试了this tutorial的另一组database.yml设置。没有骰子!我在这里和其他地方读到了许多答案,无法弄清楚。如何重新建立数据库服务器连接?
database.yml中:
development:
adapter: sqlite3
database: db/madlibs.sqlite
host: localhost
pool: 5
timeout: 5000
production:
url: <%= ENV['DATABASE_URL'] %>
adapter: postgresql
database: mydb
host: <%= ENV['DATABASE_HOST'] %>
database: <%= ENV['DATABASE_NAME'] %>
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
pool: 5
timeout: 5000
.ENV:
DATABASE_URL=<...my database name...>
配置/ environment.rb中:
configure :development do
set :database, {adapter: "sqlite3", database: "db/madlibs.sqlite3"}
set :show_exceptions, true
end
configure :production do
db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)
end
的Gemfile:
source 'https://rubygems.org'
ruby '2.4.0'
gem 'sinatra', '2.0.1'
gem 'pg', '1.0.0'
gem 'activerecord', '5.2.0'
gem 'sinatra-activerecord', '2.0.13'
gem 'sinatra-flash', '0.3.0' # Probably not nec.
gem 'sinatra-redirect-with-flash', '0.2.1' # Probably not nec.
gem 'rake', '10.4.2'
gem 'rack', '2.0.3'
gem 'sass', '3.4.24'
gem 'json', '2.1.0'
group :development do
gem 'sqlite3', '1.3.13'
gem 'tux', '0.3.0' # Maybe unnec.
end
group :production do
end
app.rb的顶部:
require 'sinatra'
require 'sinatra/activerecord'
require './config/environment'
enable :sessions
class Madlib < ActiveRecord::Base
end
答案 0 :(得分:2)
如果您有数据库网址,则不需要其他行,它可以是1行,也可以让postgres超时默认,不需要设置它。你可能想要更大的游泳池尺寸。转到你应该在Heroku中添加到你的应用程序的postgres数据库。单击View Credentials按钮。将数据从那里复制到您当地的.env文件,并将.env添加到您的.gitignore文件,因为您不想将您的私人凭据提交到您的公共存储库。
然后在heroku上
database.yml:
development:
adapter: sqlite3
database: db/madlibs.sqlite
host: localhost
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: unicode
host: <%= ENV['DB_HOST'] %>
username: <%= ENV['DB_USERNAME'] %>
password: <%= ENV['DB_PASSWORD'] %>
port: <%= ENV['DB_PORT'] %>
database: <%= ENV['DB_NAME'] %>
pool: 25
或者,您可以使用1个环境变量将所有这些连接成一个看起来像
的URI。#postgresql://username:password@db-shared-us-east-1-or-something.blah:1234/db
#If you use this format you can use just a single environment variable somthinglike
production:
adapter: postgresql
encoding: unicode
url: <%= ENV['DATABASE_URI'] %>
pool: 25
在任何一种情况下,您都需要设置您在Heroku仪表板中设置的database.yml文件中使用的任何环境变量:https://dashboard.heroku.com/apps/yourappname/settings并单击Reveal Config Vars。您可以在此处设置所需的变量,因为这是您的应用从中加载它们的位置。在我的第一个例子中设置vars,或者在我的第二个例子中设置一个vars。无论哪种方式都应该有效。
确保您在Gemfile中的生产组中拥有pg gem。并确保您已在heroku服务器上设置环境变量。