How to run MySQL Server in docker?

UPDATED: 10 November 2019
MySQL in Docker

In my previous article we understood Why Docker and What is Docker? Now lets see how you launch MySQL in Docker.

Prerequisite
Docker must be installed in your local system.

Step 1: Open terminal in root mode. Launch MySQL by following command. It'll download MySQL DockerImage if not available and then start it.
root@vicky:/home/vicky# docker run --name localmysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql
Unable to find image 'mysql:latest' locally
latest: Pulling from library/mysql
6ae821421a7d: Pull complete 
a9e976e3aa6d: Pull complete 
e3735e44a020: Pull complete 
bfd564e9483f: Pull complete 
df705f26e488: Pull complete 
0c5547f73d62: Pull complete 
f437382cf8a1: Pull complete 
b8e2d50f1513: Pull complete 
e2e3c9928180: Pull complete 
b60db6d282cd: Pull complete 
1d32deab69c6: Pull complete 
408a40cd2e9c: Pull complete 
Digest: sha256:a571337738c9205427c80748e165eca88edc5a1157f8b8d545fa127fc3e29269
Status: Downloaded newer image for mysql:latest
89911662a4793d0468427919d395535e05a3b3004aeb8173cd4d9eaede30fa3a
Step 2: Verify MySQL is running or not by executing following command.
root@vicky:/home/vicky# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
89911662a479        mysql               "docker-entrypoint..."   6 minutes ago       Up 6 minutes        0.0.0.0:3306->3306/tcp, 33060/tcp   localmysql
Step 3: Enter into Docker MySQL app by executing following command.
root@vicky:/home/vicky# docker exec -ti localmysql bash
Step 4: You are into MySQL app. You can now connect and execute to MySQL commands on it.
root@89911662a479:/# 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.15 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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> ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY 'root';
Query OK, 0 rows affected (0.05 sec)

mysql> CREATE DATABASE javaquery;
Query OK, 1 row affected (0.08 sec)

mysql> use javaquery;
Database changed

mysql> CREATE TABLE demo(id int, name varchar(100), email varchar(250));
Query OK, 0 rows affected (0.19 sec)

mysql> INSERT INTO demo(id, name, email) values(1, 'Vicky Thakor', 'vicky.thakor@javaquery.com');
Query OK, 1 row affected (0.05 sec)

mysql> SELECT * FROM demo;
+------+--------------+----------------------------+
| id   | name         | email                      |
+------+--------------+----------------------------+
|    1 | Vicky Thakor | vicky.thakor@javaquery.com |
+------+--------------+----------------------------+
1 row in set (0.00 sec)

mysql> exit
Bye
Step 5: Exit from container
root@89911662a479:/# exit
exit
Step 6: Stop MySQL container
root@vicky:/home/vicky# docker container stop localmysql
localmysql
Step 7: Remove MySQL container from docker engine.
root@vicky:/home/vicky# docker container rm localmysql
localmysql
Step 8: List docker images.
root@vicky:/home/vicky# docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              81f094a7e4cc        47 hours ago        477MB
Step 9: Remove MySQL completely from Docker.
root@vicky:/home/vicky# docker rmi 81f094a7e4cc
Untagged: mysql:latest
Untagged: mysql@sha256:a571337738c9205427c80748e165eca88edc5a1157f8b8d545fa127fc3e29269
Deleted: sha256:81f094a7e4ccc963fde3762e86625af76b6339924bf13f1b7bd3c51dbcfda988
Deleted: sha256:77dac193858f3954c3997272aabbad794166770b735ea18c313cd920c6f9ae56
Deleted: sha256:29c7593e2c24df6b8a0c73c4445dce420a41801bb28f1e207c32d7771dfb2585
Deleted: sha256:5b0034d0389c5476c01ad2217fc3eddfcceb7fb71489fa266aac13c28b973bb5
Deleted: sha256:a8380edd959f5f457dfaff93b58bd9926cd7226fc7cfade052459bcaecf5404b
Deleted: sha256:75082d1a98ce7ef9eb053ed89644e09c38b4ebd6c964ec3eb050c637480a2874
Deleted: sha256:afa9c09812bcbc0960c0db6d5c1b3af6286097935e4aa46153b4538ad7082e4f
Deleted: sha256:7d3a170fc2a4187c57e941e4f37913add9034ac7e44f658630d38bc617b673b9
Deleted: sha256:1414c04de349b69ee9d1a593d766a275b92b1a01e4c440092ccde60b1ff8e5d9
Deleted: sha256:bcf08b24b02cc14c6a934d7279031a3f50bc903d903a2731db48b8cb6a924300
Deleted: sha256:81b6eebc1d362d1ca2a888172e315c4e482e0e888e4de4caef3f9e29a3339b78
Deleted: sha256:2c5c6956c8c5752b2034f6ab742040b574d9e3598fbd0684d361c8fc9ccb3554
Deleted: sha256:0a07e81f5da36e4cd6c89d9bc3af643345e56bb2ed74cc8772e42ec0d393aee3

0 comments :