MySQL root user password is not generated

I have created docker container on Overlay Network, and when I execute

docker logs <container_name>

I don’t find MySQL user - root password generated.
So I am not sure why it is not generating sometime and generate sometime.
Question -

  1. Is there any another way to find root user password except docker logs <container_name>?
  2. How to troubleshoot what is going on and not generating root user password for mysql?

Thanks
Shrenik

To start mysql docker image, you have to set one of these :

  • MYSQL_ROOT_PASSWORD
  • MYSQL_ALLOW_EMPTY_PASSWORD
  • MYSQL_RANDOM_ROOT_PASSWORD

The later one is probably the one you’re used to use since it’s the only one that print the password in the logs. Would you mind having a look on how you started it with :
docker inspect <container_name> --format '{{json .Config.Env}}'|jq (just remove the |jq if you dont have it installed)
and maybe
docker inspect <container_name> --format '{{.Config.Cmd}}'
So we know why the password is not in the logs.

In the end :

  • if you have MYSQL_ROOT_PASSWORD then you know the password now.
  • if you have MYSQL_ALLOW_EMPTY_PASSWORD then
    docker exec -it <container_name> mysql -u root -D mysql
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
    FLUSH PRIVILEGES;
    

where newpassword is the new password obviously :stuck_out_tongue:

  • the last case is a bit trickier :
    docker stop <container_name>
    docker run -dv <path_to_volume>:/var/lib/mysql --name tmp_mysql mysql su mysql -c "mysqld_safe --skip-grant-tables --skip-networking"
    docker exec -it tmp_mysql mysql -uroot -D mysql
    FLUSH PRIVILEGES;
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
    FLUSH PRIVILEGES;
    exit
    docker rm -f tmp_mysql
    docker start <container_name>
    
1 Like

Hi Sebt3,

Thanks you so much for sharing possible options to get MySQL user root password from MySQL docker container. Let me see which one works. The best option I realize to set root password while defining / running the container. Again your response in a timely manner is highly appreciated. Have a wonderful time ahead.

Thanks
Shrenik