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.18-linux-glibc2.5-x86_64.tar.gz $ rm -rf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz $ mv mysql-5.7.18-linux-glibc2.5-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,查看数据库默认密码
2019-10-23T07:39:21.237648-00:00 1 [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 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql> set password for 'root'@'localhost' = PASSWORD('mypna123'); Query OK, 0 rows affected, 1 warning (0.02 sec) mysql> flush privileges; Query OK, 0 rows affected (0.02 sec) mysql> q $ mysql -uroot -pmypna123 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 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" 37 minutes ago Up 37 minutes mysql-container # docker rm -f mysql-container mysql-container
6. 新建容器,挂载前一个容器创建的mysql-vol数据卷
# docker run --name mysql-container --hostname mysql -p 3306:3306 --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 | +----+-----------+------+------------+ | 1 | Crist.Lee | 23 | 1989-05-07 | +----+-----------+------+------------+ 1 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"