docker数据卷学习

一 新建带有数据卷的容器

1.从docker hub下载centos7镜像

# docker pull centos

2. 创建container

# docker run --name mysql-container --hostname mysql -it centos /bin/bash

3. 配置container

# yum -y install libaio openssl openssl-devel net-tools vim wget libncurses*
# groupadd -r dba
# mkdir /usr/local/mysql
# useradd -r -g dba -G root -d /usr/local/mysql mysqladmin
# cat /my.cnf
# chown mysqladmin.dba /etc/my.cnf
# su - mysqladmin
$ cp -r /etc/skel/.bash* /usr/local/mysql
$ cat .bash_profile
# .bash_profile # Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi # User specific environment and startup programs export MYSQL_HOME=/usr/local/mysql
export PATH=$MYSQL_HOME/bin:$PATH:$HOME/bin $ cat .bashrc
# .bashrc # Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi # User specific environment
PATH="$HOME/.local/bin:$HOME/bin:$PATH"
export PATH # Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER= # User specific aliases and functions
alias ll="ls -l"

my.cnf

[client]
port = 3306
socket = /usr/local/mysql/data/mysql.sock [mysqld]
port = 3306
socket = /usr/local/mysql/data/mysql.sock skip-external-locking
key_buffer_size = 256M
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 4M
query_cache_size= 32M
max_allowed_packet = 16M
myisam_sort_buffer_size=128M
tmp_table_size=32M table_open_cache = 1024
thread_cache_size = 8
#wait_timeout = 86400
#interactive_timeout = 86400
max_connections = 1000
wait_timeout = 28800
interactive_timeout = 28800 #isolation level and default engine
default-storage-engine = INNODB
transaction-isolation = READ-COMMITTED server-id = 1
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
pid-file = /usr/local/mysql/data/hostname.pid #open performance schema
log-warnings
sysdate-is-now
log_timestamps=SYSTEM
log-error-verbosity = 3 binlog_format = MIXED
log_bin_trust_function_creators=1
log-error = /usr/local/mysql/data/hostname.err
log-bin=/usr/local/mysql/arch/mysql-bin
#other logs
#general_log =1
#general_log_file = /usr/local/mysql/data/general_log.err
#slow_query_log=1
#slow_query_log_file=/usr/local/mysql/data/slow_log.err #for replication slave
#log-slave-updates
#sync_binlog = 1 #for innodb options
innodb_data_home_dir = /usr/local/mysql/data/
innodb_data_file_path = ibdata1:500M:autoextend
innodb_log_group_home_dir = /usr/local/mysql/arch
innodb_log_files_in_group = 2
innodb_log_file_size = 200M innodb_buffer_pool_size = 1024M
#innodb_additional_mem_pool_size = 50M
innodb_log_buffer_size = 16M innodb_lock_wait_timeout = 100
#innodb_thread_concurrency = 0
innodb_flush_log_at_trx_commit = 1 #innodb io features: add for mysql5.5.8
performance_schema
innodb_read_io_threads=4
innodb-write-io-threads=4
innodb-io-capacity=200
#purge threads change default(0) to 1 for purge
innodb_purge_threads=1
innodb_use_native_aio=on #case-sensitive file names and separate tablespace
innodb_file_per_table = 1
lower_case_table_names=1 secure-file-priv = ""
explicit_defaults_for_timestamp = 1 [mysqldump]
quick
max_allowed_packet = 16M [mysql]
no-auto-rehash [mysqlhotcopy]
interactive-timeout [myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M

4. 以上container配置好后生成模板镜像

# docker commit mysql-container mysql:version1

删除现有容器

# docker stop mysql-container
# docker rm mysql-container
mysql-container

5. 创建数据卷

# docker volume create mysql-vol
# docker volume inspect mysql-vol
[
{
"CreatedAt": "2019-10-23T15:23:28+08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/usr/local/docker/volumes/mysql-vol/_data",
"Name": "mysql-vol",
"Options": {},
"Scope": "local"
}
]

6. 根据模板镜像创建新的container

# docker run --name mysql-container -p 3306:3306 --hostname mysql --mount type=volume,source=mysql-vol,target=/usr/local/mysql -it mysql:version1 /bin/bash

二 利用数据卷进行快速的数据库恢复

1. 在该容器内配置数据库

# su - mysqladmin
$ cd /usr/local/mysql
$ wget https://downloads.mysql.com/archives/get/file/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
$ tar -zxf mysql-5.7.-linux-glibc2.-x86_64.tar.gz
$ rm -rf mysql-5.7.-linux-glibc2.-x86_64.tar.gz
$ mv mysql-5.7.-linux-glibc2.-x86_64/* .
$ rm -rf mysql-5.7.18-linux-glibc2.5-x86_64
$ mkdir arch
$ bin/mysqld --initialize --user=mysqladmin --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
$ bin/mysql_ssl_rsa_setup --basedir=/usr/local/mysql
Ignoring -days; not generating a certificate
Generating a RSA private key
.+++++
...+++++
writing new private key to 'ca-key.pem'
-----
Ignoring -days; not generating a certificate
Generating a RSA private key
..................+++++
.+++++
writing new private key to 'server-key.pem'
-----
Ignoring -days; not generating a certificate
Generating a RSA private key
.......+++++
.............+++++
writing new private key to 'client-key.pem'
-----

2. 查看hostname.err,查看数据库默认密码

--23T07::21.237648-:  [Note] A temporary password is generated for root@localhost: SfpVWhtn5s;R

3. 启动数据库并修改root密码

$ bin/mysqld_safe &
$ mysql -uroot -p'SfpVWhtn5s;R'
mysql> show databases;
ERROR (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> set password for 'root'@'localhost' = PASSWORD('mypna123');
Query OK, rows affected, warning (0.02 sec)
mysql> flush privileges;
Query OK, rows affected (0.02 sec)
mysql> \q
$ mysql -uroot -pmypna123
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
rows in set (0.00 sec) $ bin/mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -uroot -pmypna123 mysql

4. 我们现在导入数据库备份,模拟数据库正在提供服务

mysql> create database amon;
Query OK, 1 row affected (0.02 sec)
mysql> grant all privileges on amon.* to 'amon'@'%' identified by 'mypna123';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql>
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> create table user_info(id int primary key auto_increment,name varchar(24),age tinyint, birthday date);
Query OK, 0 rows affected (0.14 sec) mysql> insert into user_info(name,age,birthday) values('Crist.Lee',23,'1989/05/07');
Query OK, 1 row affected (0.02 sec) mysql> select * from usr_info;
ERROR 1146 (42S02): Table 'amon.usr_info' doesn't exist
mysql> select * from user_info;
+----+-----------+------+------------+
| id | name | age | birthday |
+----+-----------+------+------------+
| 1 | Crist.Lee | 23 | 1989-05-07 |
+----+-----------+------+------------+
1 row in set (0.00 sec)

5. 假设该docker因为异常导致服务不可用,强制删除该容器

# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20fcf121dd53 mysql:version1 "/bin/bash" minutes ago Up minutes mysql-container # docker rm -f mysql-container
mysql-container

6. 新建容器,挂载前一个容器创建的mysql-vol数据卷

# docker run --name mysql-container --hostname mysql -p : --mount type=volume,source=mysql-vol,target=/usr/local/mysql -it mysql:version1 /bin/bash
[root@mysql /]# su - mysqladmin
$ bin/mysqld_safe &
$ mysql -uamon -pmypna123 amon
mysql> select * from user_info;
+----+-----------+------+------------+
| id | name | age | birthday |
+----+-----------+------+------------+
| | Crist.Lee | | -- |
+----+-----------+------+------------+
row in set (0.02 sec)

三 利用数据卷容器进行数据迁移

1. 新建容器,利用--volume-from属性将旧容器数据卷挂载到新容器中,并对新容器的数据卷挂载点进行备份,在宿主机目录下会生成备份文件mysql_backup.tar.gz

# mkdir /opt/backup1
# docker run --name db1-container --hostname db1 --volumes-from mysql-container --mount type=bind,source=/opt/backup1,target=/backup1 mysql:version1 /bin/bash -c "cd /usr/local/mysql && tar -zcvf /backup1/mysql_backup.tar.gz ."

以上执行完成后会在本地/opt/backup1目录下生成一个数据库的备份文件

2. 新建准备迁移数据的带有数据卷的容器

# docker volume create mysql-new-vol
# docker run --name mysql-new-container --hostname mysql-new --mount type=volume,source=mysql-new-vol,target=/usr/local/mysql -it mysql:version1 /bin/bash

3. 另外再创建一个新容器,新建数据卷,本地指向生成备份的本地目录,并将要迁移数据的新容器的数据卷挂载到当前的容器中

# docker run --name db3-container --hostname db3 --volumes-from db2-container --mount type=volume,source=db3-vol,target=/usr/local/mysql mysql:version1 /bin/bash -c "cp /backup1/mysql_backup.tar.gz /usr/local/mysql && tar zxvf /usr/local/mysql/mysql_backup.tar.gz"

4,.步骤执行完成后,可以在新容器里看到迁移过来的数据

最新文章

  1. ORACLE之ASM学习
  2. C#中集合汇总
  3. HTTP/1.1 Range和Content-Range
  4. 【推荐】JAVA基础◆浅谈3DES加密解密
  5. YII 创建后台模块
  6. MIT 操作系统实验 MIT JOS lab2
  7. Android毛玻璃处理代码(Blur)
  8. 给ubuntu的swap分区增加容量
  9. How to support comparators in our sort implementations?
  10. javaWeb页面实现下载
  11. Dshell----开源攻击分析框架
  12. dump文件解析之探索.Net的内存
  13. Mysql常用语句/group by 和 having子句
  14. MySQL innodb_autoinc_lock_mode 详解
  15. MSComm控件与Win32 API操作串口有何区别?
  16. 非root用户如何使用docker命令
  17. MRPT编译
  18. zabbix自动发现与监控内存和CPU使用率最高的进程,监测路由器
  19. C#基础学习之装箱,拆箱
  20. ICMP隧道工具ptunnel

热门文章

  1. 字节转b kb mb gb 等
  2. PTA 笛卡尔树
  3. JavaWeb-SpringBoot(抖音)_三、抖音项目后续
  4. JavaWeb-SpringBoot(抖音)_一、抖音项目制作
  5. 设置object的key为变量
  6. [题解] [JLOI2013] 卡牌游戏
  7. Inter IPP & Opencv 在centos 环境下使用GCC命令行编译c++运行
  8. 0.2 IDEA配置
  9. BZOJ1718分离的路径
  10. (十六)C语言之函数