未知的MySQL服务器主机'db'Rails和Docker

Jos*_*e A 5 mysql ruby-on-rails production-environment docker docker-compose

这很奇怪。我目前正在将Rails 5.1.5与Docker和Docker-Compose结合使用。我正在连接到远程MySQL(已进行防火墙保护,并且访问受限。不,数据库不在 docker容器内;它在自己的服务器上运行)。我能够运行rails db:migrate并且成功创建了架构。

但是,当我尝试导航到具有数据库调用的站点部分时,它将显示:

我们很抱歉,但有些不对劲。

我继续并启用STOUT日志来检查正在发生的一切。似乎其中一部分说:

Mysql2::Error (Unknown MySQL server host 'db'.  (-2));
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

请注意,“ db”是我的开发环境的主机。生产环境是另一个。

我不认为这是Docker问题(尽管我可能错了)

这是当前的database.yml:

default: &default
  adapter: mysql2
  pool: 5
  encoding: utf8
  database: <%= Rails.application.secrets.mysql_database %>
  username: <%= Rails.application.secrets.mysql_username %>
  password: <%= Rails.application.secrets.mysql_password %>
  host:  <%= Rails.application.secrets.mysql_host %>
  port: 3306
development: *default
test:
  <<: *default
  database: db/test.sqlite3
production: *default
Run Code Online (Sandbox Code Playgroud)

当前的secrets.yml如下:

development:
  secret_key_base: the_secret_key_base
  mysql_database: <%= ENV["SECRET_MYSQL_DATABASE"] %>
  mysql_username: <%= ENV["SECRET_MYSQL_USERNAME"] %>
  mysql_password: <%= ENV["SECRET_MYSQL_PASSWORD"] %>
  mysql_host: <%= ENV['SECRET_MYSQL_HOST'] %>
Run Code Online (Sandbox Code Playgroud)

我目前正在使用

config.read_encrypted_secrets = true
Run Code Online (Sandbox Code Playgroud)

加密的secrets.yml.enc是: 在此处输入图片说明

这是我当前正在使用的Docker-Compose文件:

version: '3.2'
services:
  app:
    image: jja3324/ntid:cprintservicehub_app
    restart: always
    environment:
      RAILS_ENV: production
      # What this is going to do is that all the logging is going to be printed into the console. 
      # Use this with caution as it can become very verbose and hard to read.
      # This can then be read by using docker-compose logs app.
      RAILS_LOG_TO_STDOUT: 'true'
    # The first command, the remove part, what it does is that it eliminates a file that 
    # tells rails and puma that an instance is running. This was causing issues, 
    # https://github.com/docker/compose/issues/1393
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -e production -p 5000 -b '0.0.0.0'"
    volumes:
      - /var/www/cprint
    ports:
      - "5000:5000"
    expose:
      - "5000"
  # Uses Nginx as a web server (Access everything through http://localhost)
  # /sf/ask/2145660961/
  web:
    image: jja3324/ntid:cprintservicehub_web
    restart: always
    links:
      - app
    volumes:
      - type: bind
        source: /path-to/ssl/certs
        target: /path-to/ssl/certs
      - type: bind
        source: /path-to-private-ssl/private/
        target: /path-to-private-ssl/private
    links:
      - app
    ports:
      - "80:80"
      - "443:443"
Run Code Online (Sandbox Code Playgroud)

阅读此答案可以告诉我Rails无法解析MySQL服务器的名称。我认为这可以转换为Rails默认返回其原始配置。

有任何想法吗?谢谢 :)

Jos*_*e A 1

虽然我还没有完全解决问题(还无法连接到数据库),但这似乎与 Nginx 有关,并且config.force_ssl在 production.rb 中

显然,我的 Nginx 配置中有一个错误。我缺少X-Forwarded-Proto https在配置文件中设置标头。这导致了无限重定向(老实说,我不知道为什么它们前一天没有出现......我认为这是因为我的浏览器中的cookie)。

完成此操作后,Rails 可以正确使用我的配置。我仍然需要找出问题所在(这似乎是防火墙问题)。

  • 编程中有两件事让我很头疼:#1)当你遇到这个“错误”或“问题”,持续了好几天,而你无法解决它......#2)当事情正常时,你不知道它为什么会正常工作,但它正在正常工作...... #2 刚刚发生:/我不知道我是否应该感到兴奋、害怕或其他什么...... (7认同)