简体   繁体   中英

Docker + Mysql ConnectionError: Unknown MySQL server host 'db'

I am running into this error with my Rails + MySQL Docker setup:

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 :

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 :

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? However I added a health check in the healthcheck section of the docker-compose file and it didn't work.

I also tried to use netcat to check the port but it also didn't work. How come I can't connect to the host db from my Docker web container? What am I doing wrong?

Turns out for my problem, I needed to clear out my docker volumes and recreate everything. MySQL was experiencing an error while booting up .

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 .

This message is complaining about a missing "MYSQL host" called 'db'

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

According to your docker-compose file

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). Also make sure

  1. you can access MYSQL from console with the specified credintials.
  2. The specified MYSQL user has access from external host ( docker is will not be on the same network )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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