【问题标题】:mysql:5.7 docker allow access from all hosts and create DBmysql:5.7 docker 允许从所有主机访问并创建数据库
【发布时间】:2021-04-25 05:45:01
【问题描述】:


我在 Docker 中遇到了 mysql:5.7 的问题。我知道有很多关于这个的问题,但我就是无法让它发挥作用。
我只是想使用 docker-compose 创建一个 mysql:5.7 容器,并在 STARTUP 上使用以下设置!!:

  • 使用密码“mypw”创建用户“root”
  • 允许 root 用户从所有主机和容器进行访问
  • 创建一个名为“mytable”的表

我的 yml:

db:
 build: "."
  command:
   - "--default-authentication-plugin=mysql_native_password"
  ports:
   - "3306:3306"
  environment:
   - MYSQL_ROOT_PASSWORD="mypw"
  volumes:
   - "./mysql/database:/var/lib/mysql"

我尝试了什么:

  • Dockerfile: COPY ./mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf
  • mysqld.cnf 末尾包含“bind-address = 0.0.0.0”行
  • 将脚本添加到 /docker-entrypoint-initdb.d/,内容为“CREATE DATABASE mydb;”
  • MYSQL_USER、MYSQL_PASSWORD、MYSQL_DATABASE 变量(也无法连接)
  • 在 mysqld.cnf 中使用 skip-grant-tables(错误)
  • MYSQL_ROOT_HOST="%"

我只是不明白为什么要特别允许来自任何地方的访问如此困难。
谁能帮忙看看在哪里放什么?
谢谢

【问题讨论】:

    标签: mysql docker


    【解决方案1】:

    还有一个变量叫做MYSQL_ROOT_HOST。您需要将此变量设置为%

    所以您的docker-compose.yaml 文件将是:

    $ cat docker-compose.yaml 
    version: "3"
    
    networks:
      net: {}
    
    services:
    
      db:
        #    build: "."
        image: mysql:5.7
        command:
          - "--default-authentication-plugin=mysql_native_password"
        ports:
          - "3309:3306"
        environment:
          MYSQL_ROOT_PASSWORD: mypw
          MYSQL_ROOT_HOST: "%"
        # volumes:
        #  - "./mysql/database:/var/lib/mysql"
    

    然后我可以使用命令连接:

    $ mysql -uroot -h127.0.0.1 -pmypw -P3309
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 4
    Server version: 5.7.32 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> 
    

    您的问题

    您的问题可能与音量有关。使用某些凭据初始化数据库后,您必须停止容器,删除数据,然后修改 docker-compose.yaml 并重新启动服务。

    【讨论】:

    • 嘿,谢谢。我总是删除数据。我试过了,我什至无法从容器内连接。从主机:用户'root'@'172.19.0.1'的访问被拒绝(使用密码:YES)从容器内部:用户'root'@'localhost'的访问被拒绝(使用密码:YES)
    • 尝试无密码连接。
    猜你喜欢
    • 2016-11-22
    • 2017-11-30
    • 1970-01-01
    • 2016-12-23
    • 2020-06-21
    • 1970-01-01
    • 2020-08-06
    • 2015-09-23
    • 1970-01-01
    相关资源
    最近更新 更多