【容器】Docker之mysql容器数据库更改不生效的解决方法

问题:

在docker中启动mysql容器,在mysql容器中对数据库的更改(如创建数据库,更改数据等),在commit后再次进入容器发现之前的更改全部没有保存,如:

步骤如下:

1.后台运行mysql容器,设置容器名称为mysql:

 

[root@localhost ~]# docker run --name=mysql -p 3306:3306 -d owenchen1992/mysql
f80791a0daf194fdba94f16a9d89ebec8ba8fbd8af28d3ea8b599b9d705f85ba


2.进入容器bash和mysql,创建一个数据库TEST_DB,并验证TEST_DB创建成功:

 

 

[root@localhost ~]# docker exec -it mysql bash
root@f80791a0daf1:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database TEST_DB;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| TEST_DB            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)


3.退出容器,并commit更改到镜像:

 

 

mysql> exit
Bye
root@f80791a0daf1:/# exit
exit
[root@localhost ~]# docker commit mysql owenchen1992/mysql
sha256:8beaeefdd1c3d23981f5d2d69089f998ae1dbfec0e023005df07031477011fe6

 

 

4.重新启动容器并进入bash和mysql,发现之前创建的TEST_DB不见了,说明之前的更改无效:

[root@localhost ~]# docker container stop mysql
mysql
[root@localhost ~]# docker container rm mysql 
mysql
[root@localhost ~]# docker run --name=mysql -p 3306:3306 -d owenchen1992/mysql
a1a1b4174caaadda0ec4b01b9fe5f92d6b3464d85284042274f71aebde0915dd
[root@localhost ~]# docker exec -it mysql bash
root@a1a1b4174caa:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)


问题原因:

在mysql的Dockerfile中有这样一行:

VOLUME /var/lib/mysql

这意味在容器中的目录/var/lib/mysql的所有修改会对应到宿主机的某个位置,可以通过命令查看具体对应的宿主机目录:docker inspect containerID/name. 当运行docker commit时,容器中的/var/lib/mysql目录的更改并不会提交到镜像中,但这些更改是随时与宿主机对应的目录同步的。当重新启动commit后的镜像时,container会重新在宿主机中创建一个目录来保存其数据更新,因此并不是原先的宿主机目录,所以新开启的容器看不到之前的数据更改。

 

解决方法:

我们已经知道了问题发生的原因,就不难解决这个问题了,步骤如下:

1.找到mysql容器对应的宿主机目录"/var/lib/docker/volumes/8496bbf33782bdadc027cdcf23197e5ebc36d11deb69ee833d63b557b3a7183d/_data":

 

[root@localhost ~]# docker inspect mysql
[
......
                "Source": "/var/lib/docker/volumes/8496bbf33782bdadc027cdcf23197e5ebc36d11deb69ee833d63b557b3a7183d/_data",
                "Destination": "/var/lib/mysql",
......
]


2.在mysql中创建一个数据库TEST_DB:

[root@localhost ~]# docker exec -it mysql bash
root@a1a1b4174caa:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> create database TEST_DB;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| TEST_DB            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

 

 

 

3.退出容器,停止容器,删除容器(不需要docker commit更新镜像):

 

mysql> exit
Bye
root@a1a1b4174caa:/# exit
exit
[root@localhost ~]# docker container stop mysql
mysql
[root@localhost ~]# docker container rm mysql
mysql

 

 

4.后台运行容器,并将宿主机对应的目录挂载到容器的/var/lib/mysql目录下并开启读写权限(关键步骤):

 

[root@localhost ~]# docker run --name=mysql -p 3306:3306 -v /var/lib/docker/volumes/8496bbf33782bdadc027cdcf23197e5ebc36d11deb69ee833d63b557b3a7183d/_data:/var/lib/mysql:rw -d owenchen1992/mysql


5.此时进入新开启的容器,并查看数据库:

 

 

[root@localhost ~]# docker exec -it mysql bash
root@c7a9c8415654:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| TEST_DB            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> 


6.发现刚才创建的数据库TEST_DB没有因为重新运行容器而消失,问题解决!

 


 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章