MYSQL_ROOT_PASSWORD is set but getting quot;Access denied for user #39;root#39;@#39;localhost#39; (using password: YES)quot; in docker container(MYSQL_ROOT_PASSWORD 已设置,但“访问被拒绝用户 root@localhost(使用密码:YES);在码头集装箱)
问题描述
我有一个 docker-compose 文件和一个 Dockerfile.MySQL 已正确安装.我已经设置了MYSQL_ROOT_PASSWORD.但是当尝试访问 mysql db 时,出现错误 - 访问被拒绝.我已经阅读了本网站的其他主题,但没有得到太多帮助.:(
I have a docker-compose file and a Dockerfile. MySQL is installed properly. I have set MYSQL_ROOT_PASSWORD. But when trying to access mysql db, getting the error - Access denied. I have read the other threads of this site, but couldn't get that much help. :(
这是我的 docker-compose 文件:
Here is my docker-compose file:
和 Dockerfile:
and Dockerfile:
这里是构建日志:
查看容器:
这是这个容器的日志:
现在进入容器:
我已经检查过 MYSQL_ROOT_PASSWORD 是否设置正确(在容器中):
I have checked if MYSQL_ROOT_PASSWORD is set correctly(in the container):
然后尝试登录mysql:
Then tried to log into mysql:
我的问题是,如何解决这个问题?为什么我不能访问mysql?我也试过没有密码选项.这给了我这个错误:
My question is, how to solve this problem? Why can't I access mysql? I tried with no password option too. That gave me this error:
这是我的项目结构:
推荐答案
假设您已经显示了整个启动日志,看来您已经针对预先存在的 db_data
卷启动了 mysql 容器包含一个 mysql 数据库文件系统.
Taking for granted you have shown your entire start log, it appears you started your mysql container against a pre-existing db_data
volume already containing a mysql database filesystem.
在这种情况下,容器启动时绝对不会初始化任何内容,环境变量也无用.引用环境变量"中的官方图片文档部分:
In this case, absolutely nothing will be initialized on container start and environment variables are useless. Quoting the official image documentation in the "Environment Variables" section:
请注意,如果您使用已包含数据库的数据目录启动容器,则以下任何变量都不会产生任何影响:在容器启动时,任何预先存在的数据库将始终保持不变.
Do note that none of the variables below will have any effect if you start the container with a data directory that already contains a database: any pre-existing database will always be left untouched on container startup.
如果你想初始化你的实例,你必须从头开始.在使用像您这样的命名卷时,使用 docker compose 很容易.警告:这将永久删除您的 db_data
卷中的内容,清除您在那里拥有的任何先前数据库.如果您需要保留内容,请先创建一个备份.
If you want your instance to be initialized, you have to start from scratch. It is quite easy to do with docker compose when using a named volume like in your case. Warning: this will permanently delete the contents in your db_data
volume, wiping out any previous database you had there. Create a backup first if you need to keep the contents.
如果您转换为绑定挂载,则必须自己删除所有内容(即 rm -rf/path/to/bind/mount/*
)
If you ever convert to a bind mount, you will have to delete all it's content yourself (i.e. rm -rf /path/to/bind/mount/*
)
注意:许多其他官方 db docker 镜像(postgres、mongo ....)的工作方式类似.
Note: many other official db docker images (postgres, mongo....) work a similar way.
这篇关于MYSQL_ROOT_PASSWORD 已设置,但“访问被拒绝用户 'root'@'localhost'(使用密码:YES)";在码头集装箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!