Skip to content
Runbook

Install MySQL on Docker

Popular open source SQL relational database.

Databasesqlrdbmsrelationaldatabaseacid

Installation

Container (official image) · Docker
docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=secret -p 3306:3306 -v mysqldata:/var/lib/mysql mysql:8

Replace 'secret' with a strong password. The named volume mysqldata persists the data.

Verify the installation

Verify
docker exec mysql mysql --version

Important files

TypePathDescription
data/var/lib/mysqlData directory INSIDE the container, to mount on a volume for persistence.

Default ports

3306

Command-line tools

  • mysqlInteractive SQL client, accessible via docker exec.
  • mysqladminAdministrative client, via docker exec.

Uninstall

Remove the container
docker rm -f mysql

Does NOT delete the mysqldata volume.

Also remove the data
docker volume rm mysqldata

Irreversible: destroys all data.

Good to know

  • MYSQL_ROOT_PASSWORD is MANDATORY on first startup (alternatives MYSQL_ALLOW_EMPTY_PASSWORD / MYSQL_RANDOM_ROOT_PASSWORD are discouraged).
  • Without a volume, data is lost when the container is removed: always use a named volume or bind mount.
  • Useful variables: MYSQL_DATABASE, MYSQL_USER and MYSQL_PASSWORD create a database and user at startup.

Starting MySQL with Docker

The official mysql image provides a ready-to-use server. The command above runs a detached container, publishes port 3306, and stores data in the named volume mysqldata. MYSQL_ROOT_PASSWORD is mandatory on first startup.

Data persistence

The internal data directory is /var/lib/mysql. Without a volume, data disappears when the container is removed. The named volume mysqldata keeps your databases across recreations.

Customization and connection

Set MYSQL_DATABASE, MYSQL_USER, and MYSQL_PASSWORD to create a database and user at startup. Open a SQL shell with docker exec -it mysql mysql -u root -p; from the host, connect to localhost:3306.