DEV Community

NT
NT

Posted on • Edited on

Docker Memo -MySQL

Goal

I just need a MySQL instance for development purpose, but with persisted data.

Storage

There are 2 ways to persist MySQL data, docker volume, or mount a directory on the host machine. For my purpose, I use docker volume.

Create Volume

$ docker volume create sumpdatavolume
$ docker volume ls
(... snip ...)
local     sumpdatavolume
Enter fullscreen mode Exit fullscreen mode

Run MySQL

Run MySQL, create a test db and table.

$ docker run --name sumpdatamysql -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=myrootpswd -v sumpdatavolume:/var/lib/mysql mysql
$ docker exec -it sumpdatamysql bash
bash-4.4# mysql -pmyrootpswd
mysql> create database test;
use test;
create table test(id int, name varchar(10));
insert into test(id, name) values (1,"John"), (2, "Mike");
select * from test;
+------+------+
| id   | name |
+------+------+
|    1 | John |
|    2 | Mike |
+------+------+
Enter fullscreen mode Exit fullscreen mode

Check persistent data

Remove the existing container and recreate MySQL using the same docker volume.

$ docker stop sumpdatamysql
$ docker rm sumpdatamysql
$ docker run --name sumpdatamysql -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=myrootpswd -v sumpdatavolume:/var/lib/mysql mysql
$ docker exec -it sumpdatamysql bash
mysql -pmyrootpswd
use test;
select * from test;
+------+------+
| id   | name |
+------+------+
|    1 | John |
|    2 | Mike |
+------+------+
Enter fullscreen mode Exit fullscreen mode

As shown above in select statement, the same data is there.

Top comments (0)