skip to Main Content

I have docker-compose (showing just MySQL part):

  site_mysql:
    image: site/mysql:latest
    build: ./images/mysql
    environment:
      MYSQL_ROOT_PASSWORD: secret
    volumes:
      -  site_mysqldata:/var/lib/mysql
    networks:
      -  site_appnet
    ports:
      - "3306:3306"

I can connect to MySQL from inside the container, but I can’t from localhost, the error I’m getting is Host site_mysql is unknown.

docker file is:

FROM mysql:5.7

MAINTAINER Author

# The official MySQL docker image will run all .sh and .sql scripts found in this directory
# the first time the container is started.
COPY init.sql /docker-entrypoint-initdb.d
COPY my.cnf /etc/mysql/conf.d

my.cnf:

[mysqld]
# Always use UTC
default-time-zone='+00:00'

# To turn off strict mode (alas, we started out that way, will be work to turn it on)
sql-mode="NO_ENGINE_SUBSTITUTION"

max-allowed-packet = 16M

### Per-thread Buffers
sort-buffer-size = 2M
read-buffer-size = 128K
read-rnd-buffer-size = 256K
join-buffer-size = 256K

### Temp Tables
tmp-table-size = 64M
max-heap-table-size = 64M

#### Storage Engines
default-storage-engine = InnoDB
innodb = FORCE

init.sql :

CREATE DATABASE IF NOT EXISTS site;
CREATE DATABASE IF NOT EXISTS site_test;
CREATE USER 'site'@'%' IDENTIFIED BY 'secret';
GRANT ALL PRIVILEGES ON site.* TO 'site'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

I’m using the same configuration for a while with different projects and all was okay until now. Any ideas why my host can’t recognize site_mysql?

Updated

Also, I have :

  site_memcached:
    image: memcached:1.4-alpine
    networks:
      -  videosite_appnet

and here I can connect using site_memcached as a name of the host

2

Answers


  1. If you connect from your host computer you have to use “localhost” instead of “site_mysql”

    site_mysql: --> this is the servername within docker
        image: site/mysql:latest
        build: ./images/mysql
        environment:
          MYSQL_ROOT_PASSWORD: secret
        volumes:
          -  site_mysqldata:/var/lib/mysql
        networks:
          -  site_appnet
        ports:
          - "3306:3306" --> here you open 
              port 3306 also on localhost (your host computer)
              and bind it to the containers port 3306
    

    My personal config (mac host) is: ( maybe “restart: always” helps)

    db:
        image: "mysql:5"
        ports:
          - "3306:3306"
        networks:
          - main
        volumes:
          - ./mysql/config:/etc/mysql/conf.d:cached
          - ./mysql/data:/var/lib/mysql:cached
          - ./mysql/init:/docker-entrypoint-initdb.d:cached
        restart: always
        environment:
          TZ: UTC
          MYSQL_ROOT_PASSWORD: somepassword
    
    Login or Signup to reply.
  2. To connect from Host to mySQL, Use below command

    docker exec -it mysql_container-name mysql -uroot -p
    

    “root” is the username for MySQL database.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search