How to start a local MySQL database with Docker

Bjorn Krols

Bjorn Krols

Published on
09 September 2022

Basic setup

docker-compose.yml

version: "3"
services:
  mysql:
    image: "mysql"
    container_name: "mysql"
    environment:
      - MYSQL_ROOT_PASSWORD=shhhht
      - MYSQL_DATABASE=project
    ports:
      - 3306:3306

Note: the MYSQL_DATABASE variable allows you to specify the name of a database to be created on image startup.

Interacting with the service

To start the service:

docker compose up -d

To stop the service:

docker compose down

Working with the MySQL shell

To access the shell:

docker exec -it mysql mysql --user=root --password=shhhht
  • docker exec -it - run a command to a running container
  • mysql - the container name
  • mysql - the command
  • --user=root - the user
  • --password=shhhht - the password

To exit the shell:

exit

Subscribe to our newsletter

The latest news, articles, and resources, sent to your inbox weekly.

More like this