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.
Should I stop the mysql container and deploy a new one?
You could change it from a running container, using a
docker execsession, as described in "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.
docker exec -itcommand to start a
mysqlclient 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_PASSWORDoption 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:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
newpasswordwith the password of your choice. Once the password is reset, the server is ready for use.
As an alternative to passing sensitive information via environment variables,
_FILEmay 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
$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD_FILE=/run/secrets/mysql-root -d mysql:tag