MySQL | Installing MySQL
MySQL is currently available in two editions: MySQL Enterprise Edition and MySQL Community Server. MySQL Enterprise is the commercial edition and includes official support and administration tools. By contrast, MySQL Community Server is a free database. This article explains how to install MySQL Community Server with Docker.
Check the Docker Version
First, as expected, check whether Docker is installed.
docker --version
If the command prints a version as shown below, Docker is installed correctly.
% docker --version
Docker version 20.10.8, build 3967b7d
Download the MySQL Docker Image
Run the following command to download the MySQL Docker image.
docker pull mysql
The output will look like this.
% docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
07aded7c29c6: Pull complete
f68b8cbd22de: Pull complete
30c1754a28c4: Pull complete
1b7cb4d6fe05: Pull complete
79a41dc56b9a: Pull complete
00a75e3842fb: Pull complete
b36a6919c217: Pull complete
635b0b84d686: Pull complete
6d24c7242d02: Pull complete
5be6c5edf16f: Pull complete
cb35eac1242c: Pull complete
a573d4e1c407: Pull complete
Digest: sha256:4fcf5df6c46c80db19675a5c067e737c1bc8b0e78e94e816a778ae2c6577213d
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
If you run the command without specifying a version, Docker downloads the latest MySQL Docker image. To install a specific version, include the version as shown below.
docker pull mysql:{version}
Run a MySQL Docker Container
Run the following command to start the container.
docker run --name mysql -e MYSQL_ROOT_PASSWORD=1234 -d -p 3306:3306 mysql:latest
Here, the Docker container name is set to mysql with the --name option. You can use another name if you prefer.
% docker run --name mysql -e MYSQL_ROOT_PASSWORD=1234 -d -p 3306:3306 mysql:latest
79c4333a40db5621a9cb7051fac2a8702c09d0f2706223cf65a5c448b08cb061
Check That the MySQL Docker Container Is Running
Run the following command to check the container status.
docker ps
If it is running, you can see MySQL listed as shown below.
% docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
79c4333a40db mysql:latest "docker-entrypoint.s..." 40 seconds ago Up 39 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
Start, Stop, and Restart the MySQL Docker Container
Stop the MySQL Docker container.
$ docker stop mysql
Start the MySQL Docker container.
$ docker start mysql
Restart the MySQL Docker container.
$ docker restart mysql
Connect to the MySQL Docker Container
docker exec -it mysql bash
% docker exec -it mysql bash
root@79c4333a40db:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql> ^C
mysql>