简体   繁体   English

在 Docker 容器中更改 mysql 密码

[英]Change mysql password in Docker container

How could I change root password in docker container since the container stop automatically once I stop the mysql service.我如何更改 docker 容器中的 root 密码,因为一旦我停止 mysql 服务,容器就会自动停止。

Should I stop the mysql container and deploy a new one?我应该停止 mysql 容器并部署一个新容器吗?

You could change it from a running container, using a docker exec session , as described in " Connecting to MySQL Server from within the Container "您可以使用docker exec session从正在运行的容器更改它,如“ 从容器内连接到 MySQL 服务器”中所述

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.服务器准备就绪后,您可以在刚刚启动的 MySQL 服务器容器中运行 mysql 客户端并将其连接到 MySQL 服务器。
Use the docker exec -it command to start a mysql client inside the Docker container you have started, like this:使用docker exec -it命令在您启动的 Docker 容器内启动一个mysql客户端,如下所示:

 docker exec -it mysql1 mysql -uroot -p

When asked, enter the generated root password (see the instructions above on how to find it).询问时,输入生成的 root 密码(请参阅上面有关如何找到它的说明)。 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_ONETIME_PASSWORD选项为 true,所以在您使用上面的示例命令启动服务器容器并将 mysql 客户端连接到服务器后,您必须通过为 MySQL 5.7 及更高版本发出以下语句来重置服务器 root 密码:

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

or alternatively run,或者运行,

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

For MySQL 5.7 and older versions, run,对于 MySQL 5.7 及更早版本,运行,

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

Substitute newpassword with the password of your choice.用您选择的密码替换newpassword Once the password is reset, the server is ready for use.一旦密码被重置,服务器就可以使用了。

Note that the above command will only change the password for 'root' connecting from 'localhost' host.请注意,上述命令只会更改从 'localhost' 主机连接的 'root' 的密码。 You can verify this by using the command:您可以使用以下命令验证这一点:

 select * from mysql.user;

To alter the password for 'root' from all hosts, use:要从所有主机更改“root”的密码,请使用:

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

Then, as described in " hub.docker.com/mysql ", dont forget docker secrets :然后,如“ hub.docker.com/mysql ”中所述,不要忘记hub.docker.com/mysql 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.作为通过环境变量传递敏感信息的替代方法,可以将_FILE附加到先前列出的环境变量,从而使初始化脚本从容器中存在的文件中加载这些变量的值。
In particular, this can be used to load passwords from Docker secrets stored in /run/secrets/<secret_name> files.特别是,这可用于从存储在/run/secrets/<secret_name>文件中的 Docker 机密加载密码。
For example:例如:

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

In case you forgot the old password, you can reset it following a few steps.如果您忘记了旧密码,您可以按照以下几个步骤重置密码。 I wrote a tutorial on how to do it (you can show you support by giving a few claps): https://medium.com/@marinbinzari/reset-root-mysql-password-in-docker-d961c71285e4我写了一个关于如何做到这一点的教程(你可以通过鼓掌表示支持): https : //medium.com/@marinbinzari/reset-root-mysql-password-in-docker-d961c71285e4

TLDR : create a mysql-init.sql file with the queries to reset the password: TLDR :使用查询创建一个mysql-init.sql文件以重置密码:

USE mysql;
UPDATE user SET Password=PASSWORD('YOURNEWPASSWORD') WHERE User='root';
FLUSH PRIVILEGES;

Mount the file in the /tmp folder, run the container (not execute so MySQLD is not started) and then start the mysqld with the init script.将文件挂载到/tmp文件夹中,运行容器(不执行,因此 MySQLD 未启动),然后使用 init 脚本启动 mysqld。

mysqld_safe --init-file=/tmp/mysql-init.sql &

Try to connect, exit the docker container and start using the new password 🤯尝试连接,退出docker容器并开始使用新密码🤯

Oh, and never forget you password again 😅哦,再也不会忘记你的密码了😅

First go to docker container bash using首先使用 docker container bash

docker exec -it containerId bash

Then然后

  • To Set the new mysql password设置新的mysql密码

    mysqladmin -u root -p'oldpassword' password 'newpassword'

  • To set the password empty use设置密码为空使用

    mysqladmin -u root -p'oldpassword' password ''

Make sure there is no space between -p and oldpassword确保-poldpassword之间没有空格

You can also set the password at the time of running the container using the -e option, which sets the environment variable MYSQL_ROOT_PASSWORD您还可以在运行容器时使用 -e 选项设置密码,该选项设置环境变量MYSQL_ROOT_PASSWORD

docker run -d \
  --name=mysql5.7 \
  -e MYSQL_ROOT_PASSWORD=123456 \
  -p 3306:3306 mysql/mysql-server:latest

To reset the root Password in MYQL 8.0 unfortunatley the upcoming tipp from Martin Binzari does not work anymore completly.不幸的是,要在 MYQL 8.0 中重置 root 密码,Martin Binzari 即将推出的 Tipp 不再完全有效。 But you can do following: (According his manual and How to reset the root password in MySQL 8.0.11? )但是您可以执行以下操作:(根据他的手册和如何在 MySQL 8.0.11 中重置 root 密码?

  1. Create a mysql-init.sql创建一个mysql-init.sql
UPDATE mysql.user SET authentication_string=null WHERE User='root';
FLUSH PRIVILEGES;
  1. Mount it to /tmp folder.将其挂载到 /tmp 文件夹。
  2. Stop the container with docker-compose stop mysql使用docker-compose stop mysql停止容器
  3. Run docker-compose run mysql bash运行docker-compose run mysql bash
  4. Inside run command mysqld_safe --init-file=/tmp/mysql-init.sql &里面运行命令mysqld_safe --init-file=/tmp/mysql-init.sql &
  5. Wait 10s, press enter and run mysql -uroot等待 10 秒,按回车并运行mysql -uroot
  6. Then you could run command ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';然后你可以运行命令ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
  7. Finally you could exit and login shoud work with new root password.最后,您可以使用新的 root 密码退出并登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM