在 Docker 容器中更改 mysql 密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48249912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change mysql password in Docker container
提问by u7137467
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 容器并部署一个新容器吗?
回答by VonC
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
会话从正在运行的容器中更改它,如“从容器内连接到 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.
Use thedocker exec -it
command to start amysql
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 服务器容器中运行 mysql 客户端并将其连接到 MySQL 服务器。
使用该docker exec -it
命令在您启动mysql
的 Docker 容器内启动一个客户端,如下所示:docker exec -it mysql1 mysql -uroot -p
询问时,输入生成的 root 密码(请参阅上面有关如何找到它的说明)。因为
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');
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. Once the password is reset, the server is ready for use.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
替换
newpassword
为您选择的密码。一旦密码被重置,服务器就可以使用了。
Note that the above command will only change the password for 'root' connecting from 'localhost' host. You can verify this by using the command:
请注意,上述命令只会更改从 'localhost' 主机连接的 'root' 的密码。您可以使用以下命令验证这一点:
select * from mysql.user;
select * from mysql.user;
To alter the password for 'root' from all hosts, use:
要从所有主机更改“root”的密码,请使用:
ALTER USER 'root'@'%' IDENTIFIED BY 'newpassword';
ALTER USER 'root'@'%' IDENTIFIED BY 'newpassword';
Then, as described in "hub.docker.com/mysql
", dont forget docker secrets:
然后,如“ 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
作为通过环境变量传递敏感信息的替代方法,
_FILE
可以将其附加到先前列出的环境变量中,从而使初始化脚本从容器中存在的文件中加载这些变量的值。
特别是,这可用于从存储在/run/secrets/<secret_name>
文件中的Docker 机密加载密码。
例如:$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD_FILE=/run/secrets/mysql-root -d mysql:tag