Skip to content

Dev: Using MySQL Docker container as root

Code Hugger (Matthew Jones) edited this page Aug 24, 2022 · 2 revisions

Default access through Socket

By default to access the root account for the local MySQL container you have to be logged into the container and access it through the socket in /tmp/mysql.sock.

  • Login to the MySQL container

docker exec -it student_dashboard_mysql /bin/bash

  • Start MySQL using the socket

mysql -u root -p --host localhost -S /tmp/mysql.sock

Use the password in the .env which by default is student_dashboard_root_pw to login.

MYSQL_ROOT_PASSWORD=student_dashboard_root_pw

If you don't specify this password, one is generated on the first launch of the server and printed to the logs. You can see this with the command below, but only on the first launch of the container.

docker logs student_dashboard_mysql 2>&1 | grep GENERATED

If you didn't look at the logs you may have to delete the .data/mysql directory and start fresh.

You can use the same parameters for other commands like mysqldump to create a dump file.

Access via port outside the container

Alternatively may be able to modify the docker-compose.yml and change this value to allow access if you change

- MYSQL_ROOT_HOST="0.0.0.0"

to

- MYSQL_ROOT_HOST=%

This will allow access from outside the container as root. However you have to have this in there when the container was created, so you'd have to either add a user with this permission or delete the database directory and start fresh after changing this. I believe logging in through the socket and running this would do the same thing.

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;

You want to have a user in the mysql.user table SELECT host, user FROM mysql.user; of host: % user: root.

There are more environment variables you could use to allow a blank password as well. Please see the More Topics on Depolying MySQL Server with Docker page for more info.

References:

https://github.com/docker-library/mysql/issues/275#issuecomment-292208567
https://stackoverflow.com/questions/40825617/cannot-connect-to-mysql-docker-container-from-container-with-django-app