簡體   English   中英

未知的MySQL服務器主機'db'Rails和Docker

[英]Unknown MySQL server host 'db' Rails and Docker

這很奇怪。 我目前正在將Rails 5.1.5與Docker和Docker-Compose結合使用。 我正在連接到遠程MySQL(已進行防火牆保護,並且訪問受限。不,數據庫不在 docker容器內;它在自己的服務器上運行)。 我能夠運行rails db:migrate並且成功創建了架構。

但是,當我嘗試導航到具有數據庫調用的站點部分時,它將顯示:

我們很抱歉,但有些不對勁。

我繼續並啟用STOUT日志來檢查正在發生的一切。 似乎其中一部分說:

Mysql2::Error (Unknown MySQL server host 'db'.  (-2));

在此處輸入圖片說明

請注意,“ 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

當前的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'] %>

我目前正在使用

config.read_encrypted_secrets = true

加密的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)
  # https://stackoverflow.com/questions/30652299/having-docker-access-external-files
  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"

閱讀此答案可以告訴我Rails無法解析MySQL服務器的名稱。 我認為這可以轉換為Rails默認返回其原始配置。

有任何想法嗎? 謝謝 :)

盡管我還沒有完全解決問題(無法連接到數據庫),但似乎與Nginx和config.force_ssl中的config.force_ssl有關

顯然,我在Nginx配置中有一個錯誤。 在配置文件中缺少設置X-Forwarded-Proto https頭的信息。 這導致了無限重定向(老實說,我不知道為什么它們不出現在前一天……我認為這是因為瀏覽器中的cookie)。

之后,Rails正確使用了我的配置。 我仍然必須找出問題所在(這似乎是防火牆問題)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM