wordpress拒绝访问_docker-compose mysql容器拒绝访问wordpress容器

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.

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:

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)

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:

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

What have I tried:

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

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.

解决方案

What have I done:

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.

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 the mysql server as root.

It worked. Then I had to stop the container with docker stop containerID.

Then I ran docker-compose rm -v(For some reason rm -v works only when you stop the container. Not when you use docker-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.

I've ended up with something like this:

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值