How to start MySQL server in Docker

Running MySQL in a Docker container offers numerous advantages. It eliminates the need to install MySQL directly on your machine, ensuring a clean and isolated environment. This approach allows you to quickly start and stop the database, try different versions without impacting your system, and maintain a consistent configuration across development and production. Additionally, Docker makes it easier to manage dependencies, share configurations, and replicate setups for team collaboration.

Starting MySql on docker

docker run --name mysql -d \
    -p 3306:3306 \   
    -e MYSQL_ROOT_PASSWORD=change-me \
    --restart unless-stopped \
    mysql:8

Check logs

docker logs mysql

Note. Password is "change-me"

Connect to DB from within docker container(no need to have mysql client installed)

docker exec -it mysql mysql -u root -p

Connect to DB from local (if mysql command line client is installed)

mysql -u root -p -P 3306 --host 127.0.0.1

See also: