[英]Docker + Mysql ConnectionError: Unknown MySQL server host 'db'
I am running into this error with my Rails + MySQL Docker setup:我的 Rails + MySQL Docker 设置遇到了这个错误:
Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (-2)
/usr/local/bundle/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:in `connect'
/usr/local/bundle/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:in `initialize'
my docker-compose.yml
:我的
docker-compose.yml
:
version: '3.7'
services:
db:
# https://github.com/passbolt/passbolt_docker/issues/103
image: mysql:5.7
restart: always
healthcheck:
test: ["CMD-SHELL", 'mysql --database=$$MYSQL_DATABASE --password=$$MYSQL_ROOT_PASSWORD --execute="SELECT count(table_name) > 0 FROM information_schema.tables;" --skip-column-names -B']
interval: 30s
timeout: 10s
retries: 4
environment:
MYSQL_DATABASE: 'db'
MYSQL_USER: 'user'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
ports:
- "3305:3306"
expose:
- '3305'
volumes:
- my-db:/var/lib/mysql
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- '.:/app'
ports:
- '3000:3000'
environment:
DB_PORT: 3306
DB_HOST: db
DATABASE_URL: mysql2://user:password@db:3306
depends_on:
- db
volumes:
my-db:
and my database.yml
:和我的
database.yml
:
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: user
password: password
host: localhost
url: <%= ENV['DATABASE_URL'] %>
port: 3305
According to this , I'm supposed to wait for MySQL to start?据此,我应该等待 MySQL 启动? However I added a health check in the
healthcheck
section of the docker-compose file and it didn't work.但是,我在
healthcheck
文件的健康检查部分添加了健康检查,但它不起作用。
I also tried to use netcat to check the port but it also didn't work.我也尝试使用 netcat 检查端口,但也没有用。 How come I can't connect to the host
db
from my Docker web
container?为什么我无法从我的 Docker
web
容器连接到主机db
? What am I doing wrong?我究竟做错了什么?
Turns out for my problem, I needed to clear out my docker volumes and recreate everything.结果是我的问题,我需要清除我的 docker 卷并重新创建所有内容。 MySQL was experiencing an error while booting up .
MySQL 在启动时遇到错误。
Basically ran this:基本上跑了这个:
docker-compose down
docker system prune --force --volumes
And then restarted everything, ensuring that MySQL (" db
") was running successfully before trying to connect to db
.然后重新启动一切,确保 MySQL ("
db
") 在尝试连接到db
之前成功运行。
This message is complaining about a missing "MYSQL host" called 'db'此消息抱怨缺少名为“db”的“MYSQL 主机”
Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (-2)
According to your docker-compose file根据您的 docker-compose 文件
environment:
MYSQL_DATABASE: 'db'
MYSQL_USER: 'user'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
You should choose here a valid parameters (use ENV variables even better).你应该在这里选择一个有效的参数(使用 ENV 变量更好)。 Also make sure
还要确保
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.