• <small id='vy98i'></small><noframes id='vy98i'>

    <tfoot id='vy98i'></tfoot>

        <bdo id='vy98i'></bdo><ul id='vy98i'></ul>
      1. <i id='vy98i'><tr id='vy98i'><dt id='vy98i'><q id='vy98i'><span id='vy98i'><b id='vy98i'><form id='vy98i'><ins id='vy98i'></ins><ul id='vy98i'></ul><sub id='vy98i'></sub></form><legend id='vy98i'></legend><bdo id='vy98i'><pre id='vy98i'><center id='vy98i'></center></pre></bdo></b><th id='vy98i'></th></span></q></dt></tr></i><div id='vy98i'><tfoot id='vy98i'></tfoot><dl id='vy98i'><fieldset id='vy98i'></fieldset></dl></div>
        <legend id='vy98i'><style id='vy98i'><dir id='vy98i'><q id='vy98i'></q></dir></style></legend>

        连接到 docker-compose mysql 容器拒绝访问,但运行相同图像的 docker 不会

        时间:2023-09-18
        <legend id='26nXV'><style id='26nXV'><dir id='26nXV'><q id='26nXV'></q></dir></style></legend>

        1. <i id='26nXV'><tr id='26nXV'><dt id='26nXV'><q id='26nXV'><span id='26nXV'><b id='26nXV'><form id='26nXV'><ins id='26nXV'></ins><ul id='26nXV'></ul><sub id='26nXV'></sub></form><legend id='26nXV'></legend><bdo id='26nXV'><pre id='26nXV'><center id='26nXV'></center></pre></bdo></b><th id='26nXV'></th></span></q></dt></tr></i><div id='26nXV'><tfoot id='26nXV'></tfoot><dl id='26nXV'><fieldset id='26nXV'></fieldset></dl></div>
            1. <tfoot id='26nXV'></tfoot>
              • <bdo id='26nXV'></bdo><ul id='26nXV'></ul>
              • <small id='26nXV'></small><noframes id='26nXV'>

                    <tbody id='26nXV'></tbody>

                  本文介绍了连接到 docker-compose mysql 容器拒绝访问,但运行相同图像的 docker 不会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在连接到使用 docker-compose 启动的 mysql docker 容器时遇到了一些问题.这是一篇长文(抱歉!).

                  I am having some issues connecting to the mysql docker container that I have launched with docker-compose. This is a long post (sorry!).

                  这是我的 docker-compose.yml 文件:

                  Here is my docker-compose.yml file:

                  db:
                    image: mysql:5.7
                    ports:
                      - "3306:3306" # I have tried both ports and expose "3306". Still doesn't work 
                    environment:
                      - MYSQL_ROOT_PASSWORD="secret"
                      - MYSQL_USER="django"
                      - MYSQL_PASSWORD="secret"
                      - MYSQL_DATABASE="myAppDB"
                  

                  那么:

                  $> docker-compose build
                  db uses an image, skipping #expected!
                  $> docker-compose up
                  <<LOTS OF OUTPUT>>
                  

                  好的,现在我有一个已启动并正在运行的 docker 容器运行程序 mysql:5.7.伟大的!或者是吗?在我的 django 应用程序中进行测试时,我收到操作错误,指出不允许用户连接数据库.好的,那么也许是我的 django ?

                  OK, so now I have an up and running docker container runner mysql:5.7. Great! Or is it? When testing in my django app, I get Operational errors saying that the user isn't allowed to connect the database. Ok, so maybe it's my django then?

                  $> docker ps
                  CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
                  c7216f99ca0f        mysql:5.7           "docker-entrypoint.sh"   3 minutes ago       Up 3 minutes        0.0.0.0:3306->3306/tcp   sharpfin_db_1
                  
                  $> docker-machine ip dev
                  192.168.99.100
                  $> mysql -h 192.168.99.100 -P 3306 -u django -p
                  Enter password: 
                  ERROR 1045 (28000): Access denied for user 'django'@'192.168.99.1' (using password: YES)
                  

                  好吧,也许这与连接到 docker-compose 容器有关?如果我尝试从 docker 容器内部连接怎么办?

                  ok so maybe It's something to do with connecting to the docker-compose container? What if I try connecting from inside the docker container?

                  $> docker exec -it c7216f99ca0f /bin/bash
                  root@c7216f99ca0f:/#
                  root@c7216f99ca0f:/# mysql -u django -p                                                                                                                                                           
                  Enter password: 
                  ERROR 1045 (28000): Access denied for user 'django'@'localhost' (using password: YES)
                  

                  好的,所以 docker mysql 不会让我连接,不知道为什么.让我们看看当我尝试在没有 docker-compose 的情况下执行此操作时会发生什么:

                  ok, so docker mysql won't let me connect, don't know why. Let's see what happens when I try do this without docker-compose:

                  $> docker run --name run-mysql -e MYSQL_ROOT_PASSWORD="secret" -e MYSQL_USER="django" -e MYSQL_PASSWORD="secret" -e MYSQL_DATABASE="myAppDB" -p "3306:3306" mysql:5.7
                  <<LOTS OF OUTPUT SAME AS BEFORE>>
                  

                  好的,现在我们有一个容器,运行与以前相同的图像,并具有相同的设置.(我认为这个断言可能不是真的 - docker-compose 正在做一些与 docker run 不同的事情).

                  Ok, so now we have a container running the same image as before with the same settings. (I think this assertion is probably not true - docker-compose is doing something different to docker run).

                  $> docker ps
                  CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
                  73071b929e82        mysql:5.7           "docker-entrypoint.sh"   3 minutes ago       Up 3 minutes        0.0.0.0:3306->3306/tcp   run-mysql
                  

                  这是我的容器(称为 run-mysql).让我们联系吧!

                  There's my container (called run-mysql). Let's connect!

                  $> mysql -h 192.168.99.100 -P 3306 -u django -p
                  Enter password: 
                  Welcome to the MySQL monitor.  Commands end with ; or g.
                  Your MySQL connection id is 2
                  Server version: 5.7.12 MySQL Community Server (GPL)
                  
                  Copyright (c) 2000, 2016, 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> SHOW DATABASES;
                  +--------------------+
                  | Database           |
                  +--------------------+
                  | information_schema |
                  | myAppDB            |
                  +--------------------+
                  2 rows in set (0.01 sec)
                  
                  mysql>
                  

                  好的.可以登录.这很奇怪...从容器内部呢?

                  Alright. Can log in. That's weird... What about from inside the container?

                  $> docker exec -it 73071b929e82 /bin/bash
                  root@73071b929e82:/# mysql -u django -p                                                                                                                                                           
                  Enter password: 
                  Welcome to the MySQL monitor.  Commands end with ; or g.
                  Your MySQL connection id is 3
                  Server version: 5.7.12 MySQL Community Server (GPL)
                  
                  Copyright (c) 2000, 2016, 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> SHOW DATABASES;
                  +--------------------+
                  | Database           |
                  +--------------------+
                  | information_schema |
                  | myAppDB            |
                  +--------------------+
                  2 rows in set (0.00 sec)
                  
                  mysql> 
                  

                  好的,当我使用 docker run 启动时,我可以从容器外部和内部登录,但不能使用 docker-compose.这是怎么回事?一定是 docker-compose 在幕后做了一些改变数据库初始化方式的事情.

                  Ok, I can log in from outside and inside the container when I launch with docker run, but not with docker-compose. What's going on? There must be something either docker-compose is doing behind the scenes that changes how the database is initialized.

                  如果我也尝试使用 root 用户,以上所有内容都完全相同.所以这不是 django 用户的权限问题.

                  All the above is the exact same if I try with the root user as well. So it's not a permissions issue with the django user.

                  任何想法如何解决这个问题?

                  Any ideas how to resolve this?

                  推荐答案

                  docker-compose.yml 文件中的环境变量在使用数组定义时不应有引号:

                  Environment variables in docker-compose.yml file should not have quotes when using array definition:

                  db:
                    image: mysql:5.7
                    ports:
                      - "3306:3306"
                    environment:
                      - MYSQL_ROOT_PASSWORD=secret
                      - MYSQL_USER=django
                      - MYSQL_PASSWORD=secret
                      - MYSQL_DATABASE=myAppDB
                  

                  <小时>

                  如果您在 docker-compose.yml 文件中使用它们:

                  db:
                    image: mysql:5.7
                    ports:
                      - "3306:3306"
                    environment:
                      - MYSQL_ROOT_PASSWORD="secret"
                      - MYSQL_USER="django"
                      - MYSQL_PASSWORD="secret"
                      - MYSQL_DATABASE="myAppDB"
                  

                  然后运行:

                  $ docker-compose up -d
                  

                  并输入正在运行的容器:

                  and enter running container:

                  $ docker-compose exec db /bin/bash
                  

                  你会看到输出:

                  root@979813643b0c:/# echo $MYSQL_ROOT_PASSWORD                                                                                                                                              
                  "secret"
                  

                  这篇关于连接到 docker-compose mysql 容器拒绝访问,但运行相同图像的 docker 不会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:MYSQL_ROOT_PASSWORD 已设置,但“访问被拒绝用户 'root'@'localh 下一篇:在 Kubernetes 上创建时如何初始化 mysql 容器?

                  相关文章

                • <small id='QKwYn'></small><noframes id='QKwYn'>

                      <tfoot id='QKwYn'></tfoot>
                      <legend id='QKwYn'><style id='QKwYn'><dir id='QKwYn'><q id='QKwYn'></q></dir></style></legend>
                      <i id='QKwYn'><tr id='QKwYn'><dt id='QKwYn'><q id='QKwYn'><span id='QKwYn'><b id='QKwYn'><form id='QKwYn'><ins id='QKwYn'></ins><ul id='QKwYn'></ul><sub id='QKwYn'></sub></form><legend id='QKwYn'></legend><bdo id='QKwYn'><pre id='QKwYn'><center id='QKwYn'></center></pre></bdo></b><th id='QKwYn'></th></span></q></dt></tr></i><div id='QKwYn'><tfoot id='QKwYn'></tfoot><dl id='QKwYn'><fieldset id='QKwYn'></fieldset></dl></div>
                      • <bdo id='QKwYn'></bdo><ul id='QKwYn'></ul>