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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37459031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:50:12  来源:igfitidea点击:

connecting to a docker-compose mysql container denies access but docker running same image does not

mysqldockerdocker-compose

提问by Davy Kavanagh

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 启动的 mysql docker 容器时遇到了一些问题。这是一篇很长的文章(对不起!)。

Here is my docker-compose.yml file:

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

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"

Then:

然后:

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

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 容器运行程序 mysql:5.7。伟大的!或者是吗?在我的 django 应用程序中进行测试时,我收到操作错误,指出不允许用户连接数据库。好的,那么也许这是我的 Django 呢?

$> 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)

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-compose 容器有关?如果我尝试从 docker 容器内部连接怎么办?

$> 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)

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 mysql 不会让我连接,不知道为什么。让我们看看当我尝试在没有 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>>

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-compose 正在做一些与 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

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

这是我的容器(称为 run-mysql)。让我们连接起来!

$> 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> 

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.

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

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.

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

Any ideas how to resolve this?

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

采纳答案by Wallace

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

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


If you use them in your docker-compose.ymlfile:

如果您在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"

and run:

并运行:

$ docker-compose up -d

and enter running container:

并输入正在运行的容器:

$ docker-compose exec db /bin/bash

you will see the output:

你会看到输出:

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

回答by threeve

I am using the official mysql image with docker-compose and not having a problem. The only difference in my compose file is that I am using a dictionary instead of an array:

我正在使用带有 docker-compose 的官方 mysql 图像并且没有问题。我的撰写文件的唯一区别是我使用的是字典而不是数组:

environment:
  MYSQL_ROOT_PASSWORD: secret
  MYSQL_USER: django
  MYSQL_PASSWORD: secret
  MYSQL_DATABASE: myAppDB

I have noticed that the compose file documentation is still stuck in V1 in some places, so you could try this, if you're using V2. Otherwise, for debugging you can use docker-compose execto interact with the container created by compose directly.

我注意到撰写文件文档在某些地方仍然停留在 V1 中,因此如果您使用的是 V2,您可以试试这个。否则,为了调试,您可以使用docker-compose exec直接与 compose 创建的容器进行交互。

docker-compose exec db /bin/bashwill get you a shell on the container that is giving you trouble and you can check things like SHOW GRANTS FOR django@'%'or whether the ports are being forwarded correctly. I hope this helps.

docker-compose exec db /bin/bash会给你一个给你带来麻烦的容器上的外壳,你可以检查诸如SHOW GRANTS FOR django@'%'端口是否被正确转发之类的东西。我希望这有帮助。

回答by gvlasov

I had a similar issue, and this helped me:

我有一个类似的问题,这对我有帮助:

https://github.com/docker-library/mysql/issues/51#issuecomment-76989402

https://github.com/docker-library/mysql/issues/51#issuecomment-76989402

Have you changed the passwords since you first tried running the containers? docker-compose does extra work to preserve volumes between runs (thus preserving the database); you may want to try docker-compose rm -vto delete everything and try starting it up again.

自从您第一次尝试运行容器以来,您是否更改过密码?docker-compose 做了额外的工作来保留运行之间的卷(从而保留数据库);您可能想尝试docker-compose rm -v删除所有内容并尝试重新启动它。

回答by gyr9i

I had the same error "Access denied for user 'admin'@'172.20.0.1' (using password: YES)". And for the long time could not figure out how to resolve it. In my situation doker-compose takes configuration from .env file.

我有同样的错误“用户'admin'@'172.20.0.1'的访问被拒绝(使用密码:是)”。并且很长一段时间想不出如何解决它。在我的情况下 doker-compose 从 .env 文件中获取配置。

environment:
  MYSQL_DATABASE: ${DB_DATABASE}
  MYSQL_USER: ${DB_USERNAME}
  MYSQL_PASSWORD: ${DB_PASSWORD}
  MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}

Finally I have found issue! The problem was in double quots in parameters.

最后我发现了问题!问题在于参数中的双引号。

DB_PASSWORD="dbpassword"

doesn't work

不起作用

DB_PASSWORD=dbpassword

work

工作

回答by mowwwalker

In my case I had mariadb installed and was trying to start a mysql container. Somehow starting the container didn't fail and running docker psshowed the container as listening on 3306, but in reality, the mariad mysqld was running and I was getting access denied. to that db rather than the one in the container. I'm using a Mac and was able to stop mariadb with: launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mariadb.plist

就我而言,我安装了 mariadb 并试图启动一个 mysql 容器。不知何故,启动容器并没有失败,运行时docker ps显示容器正在侦听 3306,但实际上,mariad mysqld 正在运行,而我的访问被拒绝。到那个数据库而不是容器中的那个。我正在使用 Mac 并且能够通过以下方式停止 mariadb:launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mariadb.plist

回答by HankCa

It looks like your problem is solved. I thought I'd discuss my problems similar to this.

看起来你的问题已经解决了。我想我会讨论与此类似的问题。

I am running tomcat (web) and mysql (db) using docker-compose on a Synology NAS (DSM 6.0.2). It worked fine on an Ubuntu box I have but not on the NAS.

我在 Synology NAS (DSM 6.0.2) 上使用 docker-compose 运行 tomcat (web) 和 mysql (db)。它在我拥有的 Ubuntu 机器上运行良好,但在 NAS 上却没有。

The problem was the firewall on the NAS - I had modified my firewall rules to allow certain ports open but then DENY ALL at end. When I added :3306 to the allowed ports it worked!

问题是 NAS 上的防火墙 - 我修改了我的防火墙规则以允许打开某些端口,但最后拒绝全部。当我将 :3306 添加到允许的端口时,它起作用了!

This is not a good solution and I don't know why DSM would require this since the docker-compose is running on a BRIDGE network. I've put in a support ticket about this.

这不是一个好的解决方案,我不知道为什么 DSM 会要求这样做,因为 docker-compose 正在 BRIDGE 网络上运行。我已经为此提供了支持票。

This answer may help others with this blocked container issue.

这个答案可能会帮助其他人解决这个被阻塞的容器问题。