使用 MySQL 容器的用户 root 的 Wordpress 访问被拒绝

Wordpress Access denied for user root with MySQL container

我正在尝试使 MySQL 实例可用于其他容器,我正在关注此文档 mysql and this wordpress 官方文档,我收到此错误

MySQL Connection Error: (1045) Access denied for user 'root'@'172.17.0.3' (using password: YES)

MySQL 实例的代码

docker run -d --restart on-failure -v hatchery:/var/lib/mysql \
           -e MYSQL_ROOT_PASSWORD=Kerrigan \
           -e MYSQL_DATABASE=zerglings --name spawning-pool mysql

WordPress 实例代码

docker run -d --name lair -p 8080:80 --link spawning-pool:mysql wordpress

如何才能成功 link wordpress 和 mysql 容器?

您需要通过环境变量将您的数据库连接凭据传递给 wordpress:

docker run -d --name lair -p 8080:80 --link spawning-pool:mysql \
    -e WORDPRESS_DB_HOST=mysql \
    -e WORDPRESS_DB_NAME=zerglings \
    -e WORDPRESS_DB_PASSWORD=zerglings wordpress

我已经通过删除所有内容并尝试重新启动来解决它。

docker rm -v spawning-pool # -v Remove the volumes associated with the container

也删除卷

docker volume rm hatchery

然后我又创建了容器

# create the volume
docker volume create hatchery

# MySQL instance
docker run -it -d --restart on-failure -v hatchery:/var/lib/mysql \
           -e MYSQL_ROOT_PASSWORD=Kerrigan \
           -e MYSQL_DATABASE=zerglings --name spawning-pool mysql

# creating wordpress
docker run -d --name lair -p 8080:80 --link spawning-pool:mysql \
           -e WORDPRESS_DB_HOST=mysql -e WORDPRESS_DB_NAME=zerglings 
           -e WORDPRESS_DB_PASSWORD=Kerrigan wordpress