Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change mysql password in Docker container

Tags:

docker

mysql

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?

like image 840
胡亦朗 Avatar asked Jan 14 '18 13:01

胡亦朗


People also ask

How do I find my Docker MySQL password?

Logging into the MySQL Server You will be prompted for the root user's password. Use the password revealed by the docker logs mysql01 command. Once within the MySQL server, you can then change the password with the command: ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';

How do I change the root password in MySQL?

To change the root password, type the following at the MySQL/MariaDB command prompt: ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyN3wP4ssw0rd'; flush privileges; exit; Store the new password in a secure location.


2 Answers

You could change it from a running container, using a docker exec session, 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.
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';

or alternatively run,

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

For MySQL 5.7 and older versions, run,

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.

Note that the above command will only change the password for 'root' connecting from 'localhost' host. You can verify this by using the command:

select * from mysql.user;

To alter the password for 'root' from all hosts, use:

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

Then, as described in "hub.docker.com/mysql", dont forget 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

xeruf points out in the comments to "MYSQL_ROOT_PASSWORD is set but getting "Access denied for user 'root'@'localhost' (using password: YES)" in docker container", adding:

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

like image 167
VonC Avatar answered Sep 19 '22 21:09

VonC


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

TLDR: create a mysql-init.sql file with the queries to reset the password:

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.

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

Try to connect, exit the docker container and start using the new password 🤯

Oh, and never forget you password again 😅

like image 31
Marin Bînzari Avatar answered Sep 20 '22 21:09

Marin Bînzari