34

I am try to run a mysql container and connect with mysql client then:

I've used following commands:

docker run --name=mysql -d mysql/mysql-server:latest
docker exec -it mysql mysql -uroot -p

According to tutorial, the last command allow me configure database password but When I introduce the first password it fails...

enter image description here

The first password was root and I get an unusual error, then I try with admin, admin an even my linux user password but They don't work...

I would like to know what's the error?

Cesar Miguel
  • 661
  • 1
  • 13
  • 38

5 Answers5

45

There are couple of ways to see the password. First Approach - Don't Run container in daemon mode

Check the below command

docker run --name=mysql mysql/mysql-server:latest

and this will print the password in the terminal as look at the below logs

2020-05-28T23:41:01.418347Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock'
2020-05-28T23:41:01.666070Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-05-28T23:41:01.714420Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.20'  socket: '/var/lib/mysql/mysql.sock'  port: 0  MySQL Community 
Server - GPL.
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
[Entrypoint] GENERATED ROOT PASSWORD: PopiKQIjAS#OGj3x]yJOBLEn80p
[Entrypoint] ignoring /docker-entrypoint-initdb.d/*
2020-05-28T23:41:06.208480Z 10 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.20).
2020-05-28T23:41:07.861667Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.20)  MySQL Community Server - GPL.
[Entrypoint] Server shut down
[Entrypoint] Setting root user as expired. Password will need to be changed before database can be used.
[Entrypoint] MySQL init process done. Ready for start up.
[Entrypoint] Starting MySQL 8.0.20-1.1.16
2020-05-28T23:41:08.534785Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.20) starting as process 1
2020-05-28T23:41:08.549216Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-05-28T23:41:09.135591Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-05-28T23:41:09.369412Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
2020-05-28T23:41:09.448584Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-05-28T23:41:09.500464Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.20'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Communi
ty Server - GPL.

Second Approach - Run container in daemon mode & fetch the password from logs Check the below command to run the container

docker run -d --name=mysql mysql/mysql-server:latest

and then run the below command to fetch the password

docker logs mysql 2>&1 | grep GENERATED

Output of above command is:

[Entrypoint] GENERATED ROOT PASSWORD: PopiKQIjAS#OGj3x]yJOBLEn80p

Once you have the password by one of the above-mentioned methods, you can then login with the below command using that password

docker exec -it mysql 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 have 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 'password';

Substitute password with the password of your choice. Once the password is reset, the server is ready for use.

Ref: https://hub.docker.com/r/mysql/mysql-server/

Jeremy
  • 1,170
  • 9
  • 26
nischay goyal
  • 3,206
  • 12
  • 23
  • I've noted that in Docker Hub there are two images: mysql and mysql/mysql-server. I tried to connect to mysql/mysql-server image as if it were a mysql image. It was the origin of solution. I've noted mysql image run container with root user by defauly. I would like to know if mysql-server image run a mysql container where any user can connect. Thanks, – Cesar Miguel May 29 '20 at 01:26
40

docker inspect <container_name_here> command output shows root password among other params

atlascoder
  • 2,746
  • 3
  • 26
  • 34
10

When starting a mysql container for the first time, there is no default password. The password you set while running the container is the default password that will be assigned to your root.

Starting a MySQL instance is simple: $ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

where some-mysql is the name you want to assign to your container, my-secret-pw is the password to be set for the MySQL root user and tag is the tag specifying the MySQL version you want. See the list above for relevant tags.

Harry Coder
  • 2,429
  • 2
  • 28
  • 32
4

Following the documentation in: https://dev.mysql.com/doc/refman/8.0/en/docker-mysql-getting-started.html

You get the password with command:

docker logs mysql 2>&1 | grep GENERATED

Example output:

GENERATED ROOT PASSWORD: Axegh3kAJyDLaRuBemecis&EShOs
paltaa
  • 2,985
  • 13
  • 28
-2

There is no default password, try login in using:

sudo docker exec -it mysql mysql -u root
André Oliveira
  • 1,105
  • 4
  • 20
  • 53
  • I get this error `OCI runtime exec failed: exec failed: container_linux.go:349: starting container process caused "exec: \"-u\": executable file not found in $PATH": unknown ` – Cesar Miguel May 28 '20 at 19:32
  • Sorry i forget to add the service name. – André Oliveira May 28 '20 at 19:40
  • 2
    Its not really your question but i think you be easier to just run docker passing the password: docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag – André Oliveira May 28 '20 at 19:41
  • 2
    with the new command I get this: `ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO).` I will also try set the password when run the mysql server but the tutorial suggested another method... – Cesar Miguel May 28 '20 at 19:44
  • If you set the password you then need to pass -p when you run the command, like this: sudo docker exec -it mysql mysql -u root -p – André Oliveira May 28 '20 at 19:55
  • 1
    You got confused with mysql and mysql-server, which are different images that have different behaviour – paltaa May 29 '20 at 13:21