迁移到外部 postgres instance 时出错

我使用 discourse-docker 和默认的自包含配置设置了我的论坛。我需要切换到使用单独的 postgres 服务器。

我运行了备份,然后修改了 app.yml,重建并尝试通过 CLI 从备份中恢复。恢复失败了,并且似乎恢复正在使用正确的宿主机来访问 postgres,但它使用的是默认端口 (5432) 而不是我配置的端口 (9432)。

错误:

psql: error: connection to server at "[REDACTED HOST]" ([REDACTED IP]), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
EXCEPTION: psql failed: 	Is the server running on that host and accepting TCP/IP connections?
/var/www/discourse/lib/backup_restore/database_restorer.rb:92:in `restore_dump'
/var/www/discourse/lib/backup_restore/database_restorer.rb:26:in `restore'
/var/www/discourse/lib/backup_restore/restorer.rb:51:in `run'
script/discourse:149:in `restore'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/command.rb:28:in `run'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/invocation.rb:127:in `invoke_command'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor.rb:527:in `dispatch'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/base.rb:584:in `start'
script/discourse:290:in `<top (required)>'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli/exec.rb:58:in `load'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli/exec.rb:58:in `kernel_load'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli/exec.rb:23:in `run'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli.rb:451:in `exec'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/vendor/thor/lib/thor/command.rb:28:in `run'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/vendor/thor/lib/thor.rb:527:in `dispatch'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli.rb:34:in `dispatch'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/vendor/thor/lib/thor/base.rb:584:in `start'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli.rb:28:in `start'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/exe/bundle:28:in `block in <top (required)>'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/friendly_errors.rb:117:in `with_friendly_errors'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/exe/bundle:20:in `<top (required)>'
/usr/local/bin/bundle:25:in `load'
/usr/local/bin/bundle:25:in `<main>'
Trying to rollback...
Rolling back...
Cleaning stuff up...
Dropping functions from the discourse_functions schema...
Removing tmp '/var/www/discourse/tmp/restores/default/2024-01-17-201724' directory...
Unpausing sidekiq...
Marking restore as finished...
Notifying 'system' of the end of the restore...
Finished!
[FAILED]

配置:
在 app.yml 的 templates: 下,我注释掉了 - "templates/postgres.template.yml"

我还添加了以下内容到 env 下:

  DISCOURSE_DB_USERNAME: [REDACTED]
  DISCOURSE_DB_PASSWORD: [REDACTED]
  DISCOURSE_DB_HOST:  [REDACTED]
  DISCOURSE_DB_PORT: 9432
  DISCOURSE_DB_NAME: [REDACTED]

重新构建时是否生成了新的空白站点?如果没有,那么你那里就有问题了。

你能否使用 psql <指向数据库的某些开关>) 从容器内部访问数据库?

在容器内部,如果你执行 set|grep DB,是否能看到你的环境变量?

全部是。它会生成一个全新的空白站点,并连接到正确的数据库。当我使用 grep 查找环境变量时,它们会被设置,并且我可以在容器内部手动连接到数据库。

有一种远程的可能性是恢复脚本以某种方式不遵守环境变量(但其他所有脚本都遵守?)。检查这一点的方法是查看代码并尝试从用户界面执行恢复。如果您可以从用户界面执行恢复,那么我认为您可以将此问题移至 #bug。

我也通过用户体验尝试过恢复,但遇到了同样的错误。

那么我想我接下来会查看代码。
它尤其奇怪的是,它对其他所有东西都有效。

您能否进入 Rails 控制台并向我们展示以下值:

BackupRestore.database_configuration
BackupRestore::DatabaseRestorer.psql_command

例如:

→ rails c
Loading development environment (Rails 7.0.8)
[1] pry(main)> BackupRestore.database_configuration
=> #<struct BackupRestore::DatabaseConfiguration
   host=nil,
   port=nil,
   username="michael",
   password=nil,
   database="discourse_development">

[2] pry(main)> BackupRestore::DatabaseRestorer.psql_command
=> "psql --dbname='discourse_development' --single-transaction --variable=ON_ERROR_STOP=1 --username=michael"
1 个赞

没问题!我这里提供的所有内容都已正确 redaction。只有端口号是错误的。

[1] pry(main)> BackupRestore.database_configuration
=> #<struct BackupRestore::DatabaseConfiguration
   host="[REDACTED]",
   port=5432,
   username="[REDACTED]",
   password="[REDACTED]",
   database="[REDACTED]">
[2] pry(main)> BackupRestore::DatabaseRestorer.psql_command
=> "PGPASSWORD='[REDACTED]' psql --dbname='[REDACTED]' --single-transaction --variable=ON_ERROR_STOP=1 --host=[REDACTED] --port=5432 --username=[REDACTED]"

我还要重申一下,空白实例能够连接到数据库,只是无法从备份中恢复。

是什么

ActiveRecord::Base.connection_pool.db_config.configuration_hash
1 个赞
[3] pry(main)> ActiveRecord::Base.connection_pool.db_config.configuration_hash
=> {:adapter=>"postgresql",
 :pool=>8,
 :connect_timeout=>5,
 :socket=>"/var/run/postgresql",
 :host=>"[REDACTED]",
 :port=>9432,
 :backup_port=>5432,
 :username=>"[REDACTED]",
 :password=>"[REDACTED]",
 :host_names=>["[REDACTED]"],
 :database=>"[REDACTED]",
 :prepared_statements=>false,
 :idle_timeout=>30,
 :reaping_frequency=>30,
 :advisory_locks=>true}

Ah!您还需要设置

DISCOURSE_DB_BACKUP_PORT: 9432

然后事情应该就能正常工作了。

编辑:这个默认设置已经存在一段时间了,但似乎弊大于利。

FIX: clear db_backup_port default value by Supermathie · Pull Request #25316 · discourse/discourse · GitHub 应该也能解决这个问题,避免给未来的用户造成混淆。

2 个赞

非常感谢!谢谢!

1 个赞

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.