连接到docker-compys mysql容器拒绝访问,但是停靠运行相同的映像没有

我有一些问题连接到我已经使用docker-compose启动的mysql docker容器.这是一个很长的帖子(对不起!).

这是我的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"

然后:

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

好的,所以现在我有一个运行的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)

好的,可能这是连接到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)

好的,所以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>>

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

$> 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).让我们连接!

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

好的.可以登录.这很奇怪…从容器里面怎么样?

$> 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-compose登录.这是怎么回事?必须有一些docker-compose在幕后进行的改变数据库初始化的方式.

如果我尝试使用root用户,上述都是一样的.所以这不是django用户的权限问题.

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

最佳答案
在使用数组定义时,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.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

并进入运行容器:

$docker-compose exec -it db /bin/bash

你会看到输出:

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

转载注明原文:连接到docker-compys mysql容器拒绝访问,但是停靠运行相同的映像没有 - 代码日志