25

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?

胡亦朗
  • 397
  • 1
  • 3
  • 9

5 Answers5

45

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!

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks a lot for the detailed answer. Docker should be a good choice for non-specialised maintainers! Cheers~ – 胡亦朗 Jan 14 '18 at 13:55
  • 2
    the `ALTER USER` command did not work for me, running mysql 5.6. I receive an `ERROR 1064 (42000): you have an error... near 'identified by .. at line 1` – tempcke Jun 17 '18 at 20:47
  • @Logikos did you type the `mysql>`? Because it is a prompt which is not part of the command. – VonC Jun 17 '18 at 20:48
  • @VonC no I did not, here is a screenshot: https://pasteboard.co/Hqmyrp5.png – tempcke Jun 17 '18 at 20:57
  • @Logikos mysql 5.6?... I am afraid that syntax is only valid in mySQL 5.7(!) and above: https://mysqlserverteam.com/improved-alter-user-syntax-support-in-5-7/#crayon-5b26cb8e7c00b541395201 – VonC Jun 17 '18 at 21:00
  • thanks @VonC I have not really done anything with this container yet, I'm just going to replace it. – tempcke Jun 17 '18 at 21:03
  • Using the docker MySQL client ... brilliant! Thanks for the tip! And I wasted time installing a MySQL client on the host ... ??? – Jeach Aug 15 '18 at 22:12
  • To solve the Logikos issue you should specify the full info for the user >>'root'@'localhost' < – LLBBL Aug 21 '19 at 20:40
  • 2
    what if I forget the password and I want to change a new password? – lily Dec 24 '19 at 07:51
  • @lily That I don't know: that would be a good separate question to ask. – VonC Dec 24 '19 at 10:37
  • 4
    @lily, you can try to exec into the container and issue command `$ printenv`. If you're lucky, the container might have it's password in the env variables – Hussam Mar 29 '20 at 23:03
  • If you mount a volume, make sure to clear it when changing the USER/PASSWORD! stackoverflow.com/a/59839180/6723250 – xeruf Mar 10 '22 at 00:17
  • @xeruf Thank you. I have included your comment in the answer for more visibility. – VonC Mar 10 '22 at 07:27
4

First go to docker container bash using

docker exec -it containerId bash

Then

  • To Set the new mysql password

    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

TIGER
  • 2,864
  • 5
  • 35
  • 45
4

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

Marin Bînzari
  • 5,208
  • 2
  • 26
  • 43
  • 1
    I basically used this method but instead of the init file I used `mysql_safe --skip-grant-tables` inside the running container. You can then `mysql -uroot` without a password and try different methods to directly change the password. – vlz Apr 07 '21 at 15:38
  • Does this method overwrite existing DB container? – deokyong song Feb 11 '22 at 21:39
2

To reset the root Password in MYQL 8.0 unfortunatley the upcoming tipp from Martin Binzari does not work anymore completly. But you can do following: (According his manual and How to reset the root password in MySQL 8.0.11?)

  1. Create a mysql-init.sql
UPDATE mysql.user SET authentication_string=null WHERE User='root';
FLUSH PRIVILEGES;
  1. Mount it to /tmp folder.
  2. Stop the container with docker-compose stop mysql
  3. Run docker-compose run mysql bash
  4. Inside run command mysqld_safe --init-file=/tmp/mysql-init.sql &
  5. Wait 10s, press enter and run mysql -uroot
  6. Then you could run command ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
  7. Finally you could exit and login shoud work with new root password.
1

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

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