简体   繁体   English

不允许主机“X”连接到此 MySQL 服务器

[英]Host 'X' is not allowed to connect to this MySQL server

I wanna deploy MySQL+PHPMyAdmin.我想部署 MySQL+PHPMyAdmin。 My docker-compose.yml:我的 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. MYSQL_ROOT_HOST 没有帮助。 When I trying to connect to mysql from db-container:当我尝试从 db-container 连接到 mysql 时:

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

I really don't know what to do with this magic... Thx.我真的不知道如何处理这个魔法......谢谢。

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.如果本地系统上的文件以损坏的方式创建或 Docker 守护程序无法正确访问,则可能会出现此问题。 This might be due to the following reasons:这可能是由于以下原因:

  • Docker is lacking the access rights on your local hard drive, on Windows eg C . Docker 缺乏对本地硬盘驱动器的访问权限,例如在 Windows 上C
  • While building up your containers Docker didn't have the access rights to your local hard drive.在构建容器时,Docker 没有访问本地硬盘的权限。 Even though Docker asks during the first --build process to allow an access to C on Windows these files might still be corrupted.即使 Docker 在第一个--build过程中要求允许在 Windows 上访问C ,这些文件可能仍然被损坏。

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 .解决方案是在授予对 Docker 的访问权限后删除相应的本地文件,在您的情况下,这些文件是/~mysql的文件和文件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:我重新创建了您的设置,只是添加了一些 ENV 配置来实现这一点,我删除了卷部分,因为它没有问题:

docker-compose.yml 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.使用 root:rootpasswd 访问 PHPMyadmin 工作正常。

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.您可以在启动 MySQL 容器 MYSQL_ROOT_HOST= 时传递一个额外的环境变量 = 这将创建一个具有从给定 IP 地址登录的权限的 root 用户。 In case where you want to allow login from any IP you can specify MYSQL_ROOT_HOST=%.如果您想允许从任何 IP 登录,您可以指定 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.对于 MySQL 容器。

In Genetal settings of Docker enable daemon without TLS.在 Docker 的基本设置中启用没有 TLS 的守护进程。 I think It works.我认为它有效。

Docker image Docker 镜像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 主机x不允许连接到该mysql服务器 - Host x is not allowed to connect to this mysql server “不允许主机'xxx.x.xx.xx'连接到该MySQL服务器” - “Host 'xxx.x.xx.xx' is not allowed to connect to this MySQL server” 主机不允许连接到该MySql服务器 - Host is not allowed to connect to this MySql server MySQL 主机 '::1' 或 '127.0.0.1' 不允许连接到此 MySQL 服务器 - MySQL Host '::1' or '127.0.0.1' is not allowed to connect to this MySQL server Kubernetes:不允许主机“ xxxx”连接到此MySQL - Kubernetes: Host 'x.x.x.x' is not allowed to connect to this MySQL 主机 — 不允许连接到此 MySQL 服务器 .Net core - Host — is not allowed to connect to this MySQL server .Net core 不允许主机(从VM)连接到该MySQL服务器 - Host is not allowed to connect to this MySQL server (from VM) 不允许主机 'localhost' 连接到此 MySQL 服务器 (#1130) - host 'localhost' is not allowed to connect to this MySQL server (#1130) #1130 - 不允许主机 'localhost' 连接到这个 MySQL 服务器 - #1130 - Host ‘localhost’ is not allowed to connect to this MySQL server 主机'localhost'不允许连接到此MySQL服务器 - Host 'localhost' is not allowed to connect to this MySQL server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM