mysql - 更改 Docker 容器中的 mysql 密码

标签 mysql docker

如何更改 docker 容器中的 root 密码,因为一旦我停止 mysql 服务,容器就会自动停止。

我应该停止 mysql 容器并部署一个新容器吗?

最佳答案

您可以使用 docker exec session 从正在运行的容器中更改它,如“Connecting to MySQL Server from within the Container”中所述

Once the server is ready, you can run the mysql client within the MySQL Server container you just started and connect it to the MySQL Server.
Use the docker exec -it command to start a mysql client inside the Docker container you have started, like this:

docker exec -it mysql1 mysql -uroot -p

When asked, enter the generated root password (see the instructions above on how to find it). Because the MYSQL_ONETIME_PASSWORD option is true by default, after you started the server container with the sample command above and connected a mysql client to the server, you must reset the server root password by issuing this statement for MySQL 5.7 and above :

mysql> update user set authentication_string=password('new_password') where user='root';

或者运行,

mysql> SET PASSWORD FOR 'root' = PASSWORD('new_password');

对于 MySQL 5.7 和更早的版本,运行,

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';

Substitute newpassword with the password of your choice. Once the password is reset, the server is ready for use.

请注意,上述命令只会更改从 'localhost' 主机连接的 'root' 的密码。您可以使用以下命令验证这一点:

select * from mysql.user;

要更改所有主机的“root”密码,请使用:

ALTER USER 'root'@'%' IDENTIFIED BY 'newpassword';

然后,如“ hub.docker.com/mysql ”中所述,不要忘记docker secrets :

As an alternative to passing sensitive information via environment variables, _FILE may be appended to the previously listed environment variables, causing the initialization script to load the values for those variables from files present in the container.
In particular, this can be used to load passwords from Docker secrets stored in /run/secrets/<secret_name> files.
For example:

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD_FILE=/run/secrets/mysql-root -d mysql:tag

xerufthe comments 中指出到“ MYSQL_ROOT_PASSWORD is set but getting " Access denied for user 'root'@'localhost' (using password: YES)" in docker container”,添加:

If you mount a volume, make sure to clear it when changing the USER/PASSWORD!

关于mysql - 更改 Docker 容器中的 mysql 密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48249912/

相关文章:

MySQL触发器更新最后插入的行

php - 处理有关批量插入时重复行的反馈

networking - 连接Docker容器

reactjs - Kubernetes 中的 Dockerfile 入口点未执行

azure - 无法从 Azure 注册表中的 docker 镜像运行 Azure Web 服务

php - 检查mysql中的字段

php - MySQL 查询返回 double

sql - 摆脱 "Using temporary; Using filesort"

Windows 版 Docker 上的 MongoDB ~ 导出/导入数据

macos - 使用osxfs的mac docker卷挂载不起作用