[英]Docker MySQL: Connecting a Rails app to MySQL
I have a rails app that I'm running in Docker. 我有一个我在Docker中运行的rails应用程序。 I want to run MySQL in a separate container and connect the two. 我想在一个单独的容器中运行MySQL并连接两者。
My docker-compose.yml
file looks like this: 我docker-compose.yml
文件如下所示:
# docker-compose.yml
db:
image: "mysql:5.6"
ports:
- 3306
environment:
MYSQL_ROOT_PASSWORD: pass
MYSQL_DATABASE: dbname
MYSQL_USER: user
MYSQL_PASSWORD: pass
web:
build: .
ports:
- "80:80"
env_file:
- .env.development
links:
- db
volumes:
- "/webapp:/home/app/webapp"
When I run docker-machine ip default
I get 192.168.99.100
. 当我运行docker-machine ip default
我得到192.168.99.100
。
When I run docker ps
I see mysql is running on PORT: 3306/tcp, 0.0.0.0:32782->32781/tcp
Edit: After removing the mysql container and restarting it, the port is actually 0.0.0.0:32784->3306/tcp
当我运行docker ps
我看到mysql正在PORT上运行: 3306/tcp, 0.0.0.0:32782->32781/tcp
: 3306/tcp, 0.0.0.0:32782->32781/tcp
编辑:删除mysql容器并重新启动后,该端口实际上是0.0.0.0:32784->3306/tcp
I'm using the Sequel
gem, and using the following params to connect to my db: 我正在使用Sequel
gem,并使用以下参数连接到我的db:
Sequel::Model.db = Sequel.connect(adapter: 'mysql2',
database: 'dbname',
user: 'user',
password: 'pass',
host: '192.168.99.100',
port: '3306',
loggers: [logger] )
When I run my app, I get: 当我运行我的应用程序时,我得到:
rake aborted!
Sequel::DatabaseConnectionError: Mysql2::Error: Can't connect to MySQL server on '192.168.99.100' (111)
/var/lib/gems/2.0.0/gems/mysql2-0.3.18/lib/mysql2/client.rb:70:in `connect'
/var/lib/gems/2.0.0/gems/mysql2-0.3.18/lib/mysql2/client.rb:70:in `initialize'
/var/lib/gems/2.0.0/gems/sequel-4.23.0/lib/sequel/adapters/mysql2.rb:36:in `new'
/var/lib/gems/2.0.0/gems/sequel-4.23.0/lib/sequel/adapters/mysql2.rb:36:in `connect'
/var/lib/gems/2.0.0/gems/sequel-4.23.0/lib/sequel/connection_pool.rb:101:in `make_new'
// Lots more traces
Any ideas what I'm doing wrong here? 我在这里做错了什么想法?
Try using the link name as "host" in your connection settings. 尝试在连接设置中使用链接名称作为“主机”。 Change the IP address to "db". 将IP地址更改为“db”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.