![](/img/trans.png)
[英]Docker + Mysql ConnectionError: Unknown MySQL server host 'db'
[英]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
這是我當前正在使用的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默認返回其原始配置。
有任何想法嗎? 謝謝 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.