Host 'X' is not allowed to connect to this MySQL server

Viewed 23891

I wanna deploy MySQL+PHPMyAdmin. My docker-compose.yml:

version: "3"
services:
  db:
    image: mysql:5.7
    restart: always
    container_name: db
    volumes:
      - ./~mysql:/var/lib/mysql
      - ./mysql.cnf:/etc/mysql/conf.d/my.cnf
    environment:
      MYSQL_DATABASE: "dbtest"
      MYSQL_ROOT_PASSWORD: "123456"
      MYSQL_ROOT_HOST: "%"
    networks:
      - db
    command: --default-authentication-plugin=mysql_native_password
    healthcheck:
      test: "mysqladmin ping -h localhost"
      interval: 1s
      timeout: 1s
      retries: 60

  phpmyadmin:
    image: phpmyadmin/phpmyadmin:4.7
    restart: always
    container_name: phpmyadmin
    ports:
      - 8080:80
    networks:
      - external-net
      - db
    environment:
      PMA_HOST: db
    depends_on:
      - db

networks:
  external-net:
    external:
      name: external-net
  db:
    driver: bridge

After some time later I getting subject error. MYSQL_ROOT_HOST don't helped. When I trying to connect to mysql from db-container:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

I really don't know what to do with this magic... Thx.

5 Answers

This problem might occur, if the files on your local system were created in a corrupted way or are not correctly accessible by the Docker daemon. This might be due to the following reasons:

  • Docker is lacking the access rights on your local hard drive, on Windows e.g. C.
  • While building up your containers Docker didn't have the access rights to your local hard drive. Even though Docker asks during the first --build process to allow an access to C on Windows these files might still be corrupted.

The solution could be do delete the according local files after the access to Docker has been granted, in your case these are files in /~mysql and the file mysql.cnf.

I've recreated your setup and was just adding some ENV configuration to do the trick, I've removed volumes section because there was no problem with it:

docker-compose.yml

version: "3"
services:
  db:
    image: mysql:5.7
    restart: always
    container_name: db
    environment:
      - MYSQL_ROOT_PASSWORD=rootpasswd
      - MYSQL_DATABASE=phpmyadmin
      - MYSQL_USER=user
      - MYSQL_PASSWORD=userpasswd
    networks:
      - db
    command: --default-authentication-plugin=mysql_native_password
    healthcheck:
      test: "mysqladmin ping -h localhost"
      interval: 1s
      timeout: 1s
      retries: 60

  phpmyadmin:
    image: phpmyadmin/phpmyadmin:4.7
    restart: always
    container_name: phpmyadmin
    ports:
      - 8080:80
    networks:
      - external-net
      - db
    environment:
      PMA_HOST: db
    depends_on:
      - db

networks:
  external-net:
    external:
      name: external-net
  db:
    driver: bridge

Accessing PHPMyadmin with root:rootpasswd works fine.

You can pass an extra environment variable when starting the MySQL container MYSQL_ROOT_HOST= this will create a root user with permission to login from given IP address. In case where you want to allow login from any IP you can specify MYSQL_ROOT_HOST=%.

This will work only for a newly created containers.

When spinning new container:

docker run --name some-mysql -e MYSQL_ROOT_HOST=% -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest

In compose file it would be

version: '2'
services:

  ### Mysql container
  mysql:
    image: mysql:latest
    ports:
      - "3306:3306"
    volumes:
      - /var/lib/mysql:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: test_db
      MYSQL_USER: test
      MYSQL_PASSWORD: test_pass
      MYSQL_ROOT_HOST: '%'  # needs to be enclosed with quotes

For some reason using "~" before the volume path solves the problem for me.

  volumes:
  - ./~mysql:/var/lib/mysql
  - ./mysql.cnf:/etc/mysql/conf.d/my.cnf

Change this code to

  volumes:
  - ~/mysql:/var/lib/mysql

for MySQL container.

In Genetal settings of Docker enable daemon without TLS. I think It works.

Docker image

Related