ruby-on-rails-4 - 当终端中的相同启动命令有效时,无法在 debian 上使用 systemd 运行 puma(fe_sendauth:未提供密码错误)
问题描述
我尝试使用 systemd 在我的 ruby on rails 网站上管理 Puma 服务器。Puma 无法以以下错误开始:PG::ConnectionBad: fe_sendauth: no password provided。当我在终端中使用与 systemd 相同的启动命令自己启动 Puma 时,它运行正常。请帮忙。
我在 Debian 9.12 上使用 RoR 4.2.11.1、postgresql 11.2,它们在 VirtualBox 6.0 上运行。
网站文件结构:
/mytarifs/current - symlink to last release
/mytarifs/releases - relseas
/mytarifs/shared - shared files like database connections
我通过以下命令在终端中成功启动 Puma:
root@mt-staging-1:/mytarifs/current# bundle exec puma -C config/puma.production.rb
Database_URL 环境变量:
DATABASE_URL=postgresql://login_name:password@localhost:5432/db_tarif
有了这个数据库 url,我可以用 psql 连接到我的数据库
错误日志:
Mar 07 02:20:39 mt-staging-1 systemd[1]: Started puma for mytarifs (production).
Mar 07 02:20:40 mt-staging-1 puma[12237]: [12237] Puma starting in cluster mode...
Mar 07 02:20:40 mt-staging-1 puma[12237]: [12237] * Version 4.3.3 (ruby 2.3.8-p459), codename: Mysterious Traveller
Mar 07 02:20:40 mt-staging-1 puma[12237]: [12237] * Min threads: 0, max threads: 5
Mar 07 02:20:40 mt-staging-1 puma[12237]: [12237] * Environment: production
Mar 07 02:20:40 mt-staging-1 puma[12237]: [12237] * Process workers: 1
Mar 07 02:20:40 mt-staging-1 puma[12237]: [12237] * Preloading application
Mar 07 02:20:47 mt-staging-1 puma[12237]: The PGconn, PGresult, and PGError constants are deprecated, and will be
Mar 07 02:20:47 mt-staging-1 puma[12237]: removed as of version 1.0.
Mar 07 02:20:47 mt-staging-1 puma[12237]: You should use PG::Connection, PG::Result, and PG::Error instead, respectively.
Mar 07 02:20:47 mt-staging-1 puma[12237]: Called from /mytarifs/releases/20200306184828/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.11.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/mytarifs/current/config/puma.production.rb
threads Integer(ENV['MIN_THREADS'] || 0), Integer(ENV['MAX_THREADS'] || 5)
workers Integer(ENV['PUMA_WORKERS'] || 1)
preload_app!
bind 'unix:///mytarifs/shared/tmp/sockets/puma.sock'
pidfile '/mytarifs/shared/tmp/pids/puma.production.pid'
state_path '/mytarifs/shared/tmp/pids/puma.state'
rackup DefaultRackup
environment ENV['RACK_ENV'] || 'production'
on_worker_boot do
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.establish_connection
end
end
/mytarifs/current/config/database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: 125
username: <%= ENV["PG_USERNAME"] %>
password: <%= ENV["PG_PASSWORD"] %>
host: localhost
template: template0
reconnect: true
production:
<<: *default
url: <%= ENV["DATABASE_URL"] %>
/etc/systemd/system/puma.service
[Unit]
Description=puma for mytarifs (production)
After=network.target
[Service]
Type=simple
Environment=RAILS_ENV=production
Environment=PUMA_DEBUG=1
WorkingDirectory=/mytarifs/current
ExecStart=/root/.rbenv/shims/bundle exec puma -e production -C config/puma.production.rb
ExecReload=/bin/kill -TSTP $MAINPID
ExecStop=/bin/kill -TERM $MAINPID
User=root
Group=root
RestartSec=1
Restart=on-failure
SyslogIdentifier=puma
[Install]
WantedBy=multi-user.target
解决方案
好的,我找到了错误的原因。这是因为 systemd 执行时环境变量不可用(等于“”)。我不知道如何从内存中获取环境变量,但是 systemd 可以使用指令 EnvironmentFile=/absolute/path/to/environment/file 从文件中获取它们
推荐阅读
- ruby-on-rails - 在 Rails 中运行时连接到多个数据库
- java - 使用一些操作反序列化嵌套的 json java
- azure - SQL Server Management Studio 忽略用于连接的 Windows 代理
- java - 当对每个元素的操作产生不同的结果计数/持续时间时,保持初始可迭代排序顺序稳定
- python - Python创建空的二维数组
- sql - 如果项目已经被分配,TSQL 检索下一个项目
- azure-logic-apps - Microsoft Flow 或 Logic 应用程序将文件从共享点发送到 API 太大
- excel - 一次返回所有集合项以正常工作
- python - 插入数据时删除 DataFrame 列
- ansible - 对 Docker Swarm MariaDB 容器进行数据库备份的最佳方法是什么?