PG::ConnectionBad:无法连接到服务器:连接被拒绝(Ubuntu 16.04,Rails 5,Capistrano)
PG::ConnectionBad: could not connect to server: Connection refused (Ubuntu 16.04, Rails 5, Capistrano)
将 Rails 5 应用程序部署到 Ubuntu 16.04.3 (Digitalocean VPS) 时,当 运行 cap production deploy --trace
我得到以下错误:
.
航站楼
$HOME/.rbenv/bin/rbenv exec bundle exec rake db:migrate
rake aborted!
PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "138.68.6.26" and accepting
TCP/IP connections on port 5432?
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/pg-0.21.0/lib/pg.rb:56:in `initialize'
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/pg-0.21.0/lib/pg.rb:56:in `new'
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/pg-0.21.0/lib/pg.rb:56:in `connect'
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:695:in...
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:220:in…
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:38:in …
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/activerecord-
...
/home/deploy/.rbenv/versions/2.4.1/bin/bundle:22:in `load'
/home/deploy/.rbenv/versions/2.4.1/bin/bundle:22:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
cap aborted!
.
Gemfile
group :development do
gem 'capistrano', '~> 3.7', '>= 3.7.1'
gem 'capistrano-rails', '~> 1.2'
gem 'capistrano-passenger', '~> 0.2.0'
gem 'capistrano-rbenv', '~> 2.1'
gem 'capistrano-bundler'
end
.
Capfile
require 'capistrano/rbenv'
set :rbenv_type, :user
set :rbenv_ruby, '2.4.1'
require 'capistrano/bundler'
require 'capistrano/rails'
require 'capistrano/passenger'
.
config/deploy.rb
set :application, "my-app"
set :repo_url, "git@github.com:username/my-app.git"
set :deploy_to, '/home/deploy/my-app'
append :linked_files, "config/database.yml", "config/secrets.yml"
append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "vendor/bundle", "public/system", "public/uploads"
.
config/deploy/production.rb
server '138.68.6.26', user: 'deploy', roles: %w{app db web}
.
在服务器上,我已经创建了符号链接文件 /home/deploy/my-app/shared/config/database.yml
和 /home/deploy/my-app/shared/config/secrets.yml
并将它们从我的存储库中删除。我也 git 也忽略了它们。
.
database.yml
production:
adapter: postgresql
host: 138.68.6.26
database: my-app
username: deploy
password: password
encoding: unicode
pool: 5
.
secrets.yml
production:
secret_key_base: 9b865db3b261f66576ef6b2a...
.
我还设置了一个名为 postgres 的 Ubuntu 系统用户,并创建了一个名为 deploy 的数据库用户。我按照 database.yml
文件中的指示正确命名了数据库。我到处搜索,无法使它正常工作。大多数答案都是针对本地调试的。这是用于部署。
尝试通过 psql 连接到此服务器,如果不能,则问题出在您的 postgresql 服务器配置中。 (从应用程序的服务器执行此操作)
psql -h 138.68.6.26 -U deploy my-app
您的 Postgres 服务器配置为仅侦听 localhost
接口,因此无法从外部访问。
要将 Postgres 配置为允许外部 TCP 连接,请进行以下更改:
1) 在/etc/postgresql/<>/main/postgresql.conf
中将listen_addresses
设置为*
2) 在 /etc/postgresql/<>/main/pg_hba.conf 中设置访问权限以允许来自外部 IP 的 TCP 连接
只需确保您设置了像 UFW 这样的防火墙,以防止任意主机连接到您的数据库。
事实证明,我在 database.yml
文件中输入了错误的 IP 地址。当它应该设置为本地 ip (127.0.0.1) 时,我将它设置为我的主机 ip (138.68.6.26)。
将 Rails 5 应用程序部署到 Ubuntu 16.04.3 (Digitalocean VPS) 时,当 运行 cap production deploy --trace
我得到以下错误:
.
航站楼
$HOME/.rbenv/bin/rbenv exec bundle exec rake db:migrate
rake aborted!
PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "138.68.6.26" and accepting
TCP/IP connections on port 5432?
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/pg-0.21.0/lib/pg.rb:56:in `initialize'
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/pg-0.21.0/lib/pg.rb:56:in `new'
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/pg-0.21.0/lib/pg.rb:56:in `connect'
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:695:in...
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:220:in…
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:38:in …
/home/deploy/my-app/shared/bundle/ruby/2.4.0/gems/activerecord-
...
/home/deploy/.rbenv/versions/2.4.1/bin/bundle:22:in `load'
/home/deploy/.rbenv/versions/2.4.1/bin/bundle:22:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
cap aborted!
.
Gemfile
group :development do
gem 'capistrano', '~> 3.7', '>= 3.7.1'
gem 'capistrano-rails', '~> 1.2'
gem 'capistrano-passenger', '~> 0.2.0'
gem 'capistrano-rbenv', '~> 2.1'
gem 'capistrano-bundler'
end
.
Capfile
require 'capistrano/rbenv'
set :rbenv_type, :user
set :rbenv_ruby, '2.4.1'
require 'capistrano/bundler'
require 'capistrano/rails'
require 'capistrano/passenger'
.
config/deploy.rb
set :application, "my-app"
set :repo_url, "git@github.com:username/my-app.git"
set :deploy_to, '/home/deploy/my-app'
append :linked_files, "config/database.yml", "config/secrets.yml"
append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "vendor/bundle", "public/system", "public/uploads"
.
config/deploy/production.rb
server '138.68.6.26', user: 'deploy', roles: %w{app db web}
.
在服务器上,我已经创建了符号链接文件 /home/deploy/my-app/shared/config/database.yml
和 /home/deploy/my-app/shared/config/secrets.yml
并将它们从我的存储库中删除。我也 git 也忽略了它们。
.
database.yml
production:
adapter: postgresql
host: 138.68.6.26
database: my-app
username: deploy
password: password
encoding: unicode
pool: 5
.
secrets.yml
production:
secret_key_base: 9b865db3b261f66576ef6b2a...
.
我还设置了一个名为 postgres 的 Ubuntu 系统用户,并创建了一个名为 deploy 的数据库用户。我按照 database.yml
文件中的指示正确命名了数据库。我到处搜索,无法使它正常工作。大多数答案都是针对本地调试的。这是用于部署。
尝试通过 psql 连接到此服务器,如果不能,则问题出在您的 postgresql 服务器配置中。 (从应用程序的服务器执行此操作)
psql -h 138.68.6.26 -U deploy my-app
您的 Postgres 服务器配置为仅侦听 localhost
接口,因此无法从外部访问。
要将 Postgres 配置为允许外部 TCP 连接,请进行以下更改:
1) 在/etc/postgresql/<>/main/postgresql.conf
中将listen_addresses
设置为*
2) 在 /etc/postgresql/<>/main/pg_hba.conf 中设置访问权限以允许来自外部 IP 的 TCP 连接
只需确保您设置了像 UFW 这样的防火墙,以防止任意主机连接到您的数据库。
事实证明,我在 database.yml
文件中输入了错误的 IP 地址。当它应该设置为本地 ip (127.0.0.1) 时,我将它设置为我的主机 ip (138.68.6.26)。