简体   繁体   English

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

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

This is strange. 这很奇怪。 I'm currently using Rails 5.1.5 with Docker and Docker-Compose. 我目前正在将Rails 5.1.5与Docker和Docker-Compose结合使用。 I am connecting to a remote MySQL (which is firewalled, and has limited access to. No, the database is not inside a docker container; it runs in its own server). 我正在连接到远程MySQL(已进行防火墙保护,并且访问受限。不,数据库不在 docker容器内;它在自己的服务器上运行)。 I was able to run rails db:migrate and the schema was successfully created . 我能够运行rails db:migrate并且成功创建了架构。

But, when I try to navigate to the part of the site which has a database call, it displays: 但是,当我尝试导航到具有数据库调用的站点部分时,它将显示:

We're sorry, but something went wrong. 我们很抱歉,但有些不对劲。

I went ahead and enabled STOUT logs to check for everything that was happening. 我继续并启用STOUT日志来检查正在发生的一切。 It seems that there is a part in which it says: 似乎其中一部分说:

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

在此处输入图片说明

Note that 'db' is the host for my development environment. 请注意,“ db”是我的开发环境的主机。 The production environment is another one. 生产环境是另一个。

I don't think this is a Docker problem (although I could be wrong) 我不认为这是Docker问题(尽管我可能错了)

This is the current database.yml: 这是当前的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

The current secrets.yml is as follows: 当前的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'] %>

I am currently using 我目前正在使用

config.read_encrypted_secrets = true

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

This is the Docker-Compose file I'm currently using: 这是我当前正在使用的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"

Reading this answer tells me that Rails couldn't resolve the name for the MySQL server. 阅读此答案可以告诉我Rails无法解析MySQL服务器的名称。 I think this translates to Rails defaulting back to its original config. 我认为这可以转换为Rails默认返回其原始配置。

Any ideas? 有任何想法吗? Thanks :) 谢谢 :)

While I haven't solved entirely the problem (Can't connect to the Database yet) it seems this had something to do with Nginx, and config.force_ssl in production.rb 尽管我还没有完全解决问题(无法连接到数据库),但似乎与Nginx和config.force_ssl中的config.force_ssl有关

Apparently, I had a bug in the Nginx configuration. 显然,我在Nginx配置中有一个错误。 I was missing setting the X-Forwarded-Proto https header in the configuration file. 在配置文件中缺少设置X-Forwarded-Proto https头的信息。 This was causing infinite redirects (which I honestly don't know why they weren't present the day before... I think it was because of the cookies in my browser). 这导致了无限重定向(老实说,我不知道为什么它们不出现在前一天……我认为这是因为浏览器中的cookie)。

After doing that, Rails is correctly using my configuration. 之后,Rails正确使用了我的配置。 I still have to figure out what's the problem (which seems to be a firewall issue). 我仍然必须找出问题所在(这似乎是防火墙问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM