docker-compose mysql容器拒绝访问wordpress容器 [英] docker-compose mysql container denies access to wordpress container
问题描述
我对mysql 5.7容器有问题,拒绝访问wordpress容器。我正在使用docker-compose,并且在Mac OSX上运行docker。 Docker应该是可用的最新版本。
I have a problem with mysql 5.7 container denying access to wordpress container. I'm using docker-compose and I'm running docker on Mac OSX. Docker should be on latest version available.
这是我的docker-compose.yml
Here's my docker-compose.yml
version: '2'
services:
wordpress:
depends_on:
- db
image: wordpress:latest
container_name: wordpress
ports:
- "8000:80"
- "443:443"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: blog
WORDPRESS_DB_USER: blog_admin
WORDPRESS_DB_PASSWORD: userpasswd
networks:
- wordpress_net
db:
image: mysql:5.7
container_name: db
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpasswd
MYSQL_DATABASE: blog
MYSQL_USER: blog_admin
MYSQL_PASSWORD: userpasswd
networks:
- wordpress_net
networks:
wordpress_net:
volumes:
db_data:
日志从db容器中获取的数据为:
Logs from db container are:
2017-05-12T23:28:06.138429Z 321 [Note] Access denied for user 'blog_admin'@'172.19.0.3' (using password: YES)
wordpress容器中的日志为:
Logs from wordpress container are:
MySQL Connection Error: (1045) Access denied for user 'blog_admin'@'172.19.0.3' (using password: YES)
Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'blog_admin'@'172.19.0.3' (using password: YES) in - on line 22
docker ps:
docker ps:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b02f0146fe7 wordpress:latest "docker-entrypoint..." 25 minutes ago Up 26 seconds 0.0.0.0:443->443/tcp, 0.0.0.0:8000->80/tcp wordpress
5d932ed6c269 mysql:5.7 "docker-entrypoint..." 25 minutes ago Up 25 minutes 0.0.0.0:3306->3306/tcp db
我尝试了什么:
- 重新启动Docker主机。
- docker-compose rm -v,然后再次docker-compose up -d。
- 使用这些用户凭据和root凭据登录到wordpress容器之外。
- 删除docker映像并从头开始将其再次拉出。
- 在
WORDPRESS_DB_HOST,WORDPRESS_DB_USER
中使用根凭据$ b
- Restarting docker host.
- docker-compose rm -v and then docker-compose up -d again.
- Logging in with those user credentials and root credentials outside of wordpress container.
- Removing docker images and pulling them again from scratch.
- Using root credentials in
WORDPRESS_DB_HOST, WORDPRESS_DB_USER
当我连接到db容器时,我可以看到db的所有环境变量。 WordPress的容器不断自我重新启动。我在堆栈溢出上看到一个答案,建议使用刷新权限并设置新用户帐户,但我想知道我是否做错了什么,可能导致此问题再次出现在其他计算机上。
I can see all the env vars for db when I connect to db container. Wordpress container keeps restarting it self. I saw one answer on stack overflow which recommended flushing privileges and setting new user account but I want to know if I'm doing something wrong that could cause this problem to appear again on other machine.
推荐答案
我做了什么:
docker-compose rm -v
不适用于我,因为我一直使用 docker-compose down
关闭容器。我认为这是问题的根源。
docker-compose rm -v
hasn't worked for me as I've always used docker-compose down
to shutdown containers. And I think this is the root of the problem.
- 我用
docker-compose.yml删除了该文件夹
- 然后我创建了一个仅包含
mysql
容器配置的撰写文件,启动它并尝试以root
的身份连接到mysql
服务器。 - 有效。然后我不得不使用
docker stop containerID
停止容器。 - 然后我运行了
docker-compose rm- v
(出于某些原因,rm -v
仅在停止容器时起作用。在使用docker-compose down时不起作用
这导致数据库的状态持续存在,因为我在db容器中使用了一个卷)并使用wordpress容器配置完成了yml文件。
- I deleted the folder with my
docker-compose.yml
and created a new one. - Then I created a compose file with just the config for
mysql
container, launched it and tried to connect to themysql
server asroot
. - It worked. Then I had to stop the container with
docker stop containerID
. - Then I ran
docker-compose rm -v
(For some reasonrm -v
works only when you stop the container. Not when you usedocker-compose down
this caused the db's state to persist, as I used a volume for the db container) and completed the yml file with the wordpress container config.
我最终遇到了这样的情况:
version: '2'
services:
wordpress:
image: wordpress:latest
container_name: wordpress-blog
depends_on:
- mysql
ports:
- "8000:80"
- "443:443"
restart: always
environment:
WORDPRESS_DB_HOST: mysql
WORDPRESS_DB_USER: admin
WORDPRESS_DB_PASSWORD: password
WORDPRESS_DB_NAME: wordpress
mysql:
image: mysql:5.7
container_name: mysql-db
ports:
- "3306:3306"
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: admin
MYSQL_PASSWORD: password
注意:以前,我不仅遇到问题,不仅从wordpress容器连接数据库,还从db容器本身连接数据库。我上面描述的方法帮助我解决了这个问题。
NOTE: I had a problem previously not only connecting to the database from the wordpress container, but also from the db container itself. The method I described above helped me to solve this issue.
这篇关于docker-compose mysql容器拒绝访问wordpress容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!