[英]How to connect mysql in a rails application with docker?
I created a rails app use mysql database.我创建了一个使用 mysql 数据库的 rails 应用程序。 Now want to put them into docker container.现在想把它们放入 docker 容器中。 I am using docker, docker-machine, docker-compose.我正在使用 docker、docker-machine、docker-compose。
My docker-compose.yml我的 docker-compose.yml
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: <%= ENV['MYSQL_ROOT_PASSWORD'] %>
ports:
- "3306:3306"
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3000:3000"
links:
- db
My config/database.yml我的配置/database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: <%= ENV['MYSQL_ROOT_PASSWORD'] %>
host: <%= ENV['DB_PORT_3306_TCP_ADDR'] %>
port: <%= ENV['DB_PORT_3306_TCP_PORT'] %>
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
database: myapp_production
When boot app启动应用程序时
$ docker-compose up
...
sidekiq_1 | Host '172.17.0.5' is not allowed to connect to this MySQL server
...
My web env我的网络环境
$ docker-compose run web env
...
DB_PORT=tcp://172.17.0.3:3306
DB_PORT_3306_TCP=tcp://172.17.0.3:3306
DB_PORT_3306_TCP_ADDR=172.17.0.3
DB_PORT_3306_TCP_PORT=3306
DB_PORT_3306_TCP_PROTO=tcp
WEB_PORT=tcp://172.17.0.6:3000
WEB_PORT_3000_TCP=tcp://172.17.0.6:3000
WEB_PORT_3000_TCP_ADDR=172.17.0.6
WEB_PORT_3000_TCP_PORT=3000
WEB_PORT_3000_TCP_PROTO=tcp
...
My docker-machine ip我的 docker-machine ip
$ docker-machine ip myapp
192.168.99.100
Access from browser: http://192.168.99.100:3000
, then saw error:从浏览器访问: http://192.168.99.100:3000
,然后看到错误:
Mysql2::Error
Host '172.17.0.6' is not allowed to connect to this MySQL server
172.17.0.3
, 172.17.0.5
, 172.17.0.6
, etc. Why they use different ips? 172.17.0.3
, 172.17.0.5
, 172.17.0.6
等为什么他们使用不同的ip? Then how to connect mysql?那么如何连接mysql呢? I can't understand the host
and port
written in the config/database.yml
file.我无法理解config/database.yml
文件中写入的host
和port
。
In my case, I'm running Rails in host, and MySQL in docker, the official image.就我而言,我在主机中运行 Rails,在官方映像 docker 中运行 MySQL。
Adding host: 127.0.0.1
solved the problem.添加host: 127.0.0.1
解决了问题。
config/database.yml配置/数据库.yml
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password:
host: 127.0.0.1
You have to add the container name ( db
) to the host, Example:您必须将容器名称 ( db
) 添加到主机,例如:
config/database.yml配置/数据库.yml
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: <%= ENV['MYSQL_ROOT_PASSWORD'] %>
host: db
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
database: myapp_production
Look at you this example: docker-to-rails-with-rbenv看看你这个例子: docker-to-rails-with-rbenv
IPs are assigned by internal DHCP server. IP 由内部 DHCP 服务器分配。 This is why they can changed...这就是为什么他们可以改变...
I would recommend reading this post http://blog.carbonfive.com/2015/03/17/docker-rails-docker-compose-together-in-your-development-workflow/ which explains how to configure rails app using host name (and not IPs) to properly connect to a database while using docker-compose.我建议阅读这篇文章http://blog.carbonfive.com/2015/03/17/docker-rails-docker-compose-together-in-your-development-workflow/ ,其中解释了如何使用主机名配置 rails 应用程序(而不是 IP)在使用 docker-compose 时正确连接到数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.