docker 持久化存储

[root@localhost ~]# docker run --name mysql -p 3306:3306 -v /mysql(主机上的目录):/var/lib/mysql(容器内部目录) -d hub.c.163.com/public/centos:6.7-tools
[root@localhost ~]# docker exec -it mysql /bin/bash
[root@0534fd344f72 /]# yum -y install mysql mysql-server
[root@0534fd344f72 /]# service mysqld start
[root@0534fd344f72 /]# exit
exit
[root@localhost ~]# cd /mysql/
[root@localhost mysql]# ls
ibdata1  ib_logfile0  ib_logfile1  mysql  mysql.sock  test
[root@localhost mysql]# docker exec -it mysql /bin/bash
[root@0534fd344f72 /]# mysqladmin -uroot password 123
[root@0534fd344f72 /]#  mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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 aa;
Query OK, 1 row affected (0.00 sec)

mysql> use aa;
Database changed
mysql> create table a1 (id int,name char(30));
Query OK, 0 rows affected (0.01 sec)

mysql> select * from a1;
Empty set (0.00 sec)

mysql> insert into a1 values (1,"lishi");
Query OK, 1 row affected (0.00 sec)

mysql> select * from a1;                 
+------+-------+
| id   | name  |
+------+-------+
|    1 | lishi |
+------+-------+
1 row in set (0.00 sec)
mysql> select * from aa.a1;
+------+-------+
| id   | name  |
+------+-------+
|    1 | lishi |
+------+-------+
1 row in set (0.00 sec)
[root@localhost mysql]# cd /mysql/
[root@localhost mysql]# ls
aa  ibdata1  ib_logfile0  ib_logfile1  mysql  mysql.sock  test
[root@localhost mysql]# cd aa
[root@localhost aa]# ls
a1.frm  a1.MYD  a1.MYI  db.opt
a1.frm    存放表结构,a1.MYD    存储数据,   a1.MYI    索引,  db.opt    数据库相关信息,在这个库下的一些特殊信息存在这里。

对于多个容器我可以把他们的挂载到同一个目录下实现他们的数据一至。这个是一个web集群的话,只要本地的磁盘足够大,那就可以做到一个目录共享,在小集群下我们可以这么做,如果在大集群下比如web集群有上百台的话,不建议这么做,本地磁盘io会成为一瓶颈,他也可以实现本的磁盘远程挂载的方式,先本地目录挂载成远程文件系统,容器挂载到这个远程文件系统实现的就是操作远程目录的权限。并且操作的还是同一个远程文件系统,这样就可以实现数据的一至性。

猜你喜欢

转载自www.cnblogs.com/liujunjun/p/12119145.html
今日推荐