多实例部署、开机自启、xtrabackup备份与恢复
一、MySQL多实例部署
1 下载二进制包
wget http://https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
2 创建用户和组并解压二进制包至/usr/local/下
//创建用户和组
[root@localhost src]# groupadd -r mysql
[root@localhost src]# useradd -M -s /sbin/nologin -g mysql mysql
解压软件至/usr/local/
ar -xf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# ls /usr/local/
bin etc games include lib lib64 libexec mysql-5.7.35-linux-glibc2.12-x86_64 sbin share src
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ln -sv mysql-5.7.35-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.35-linux-glibc2.12-x86_64/"
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 2 root root 6 5月 19 2020 bin
drwxr-xr-x. 2 root root 6 5月 19 2020 etc
drwxr-xr-x. 2 root root 6 5月 19 2020 games
drwxr-xr-x. 2 root root 6 5月 19 2020 include
drwxr-xr-x. 2 root root 6 5月 19 2020 lib
drwxr-xr-x. 3 root root 17 8月 27 01:37 lib64
drwxr-xr-x. 2 root root 6 5月 19 2020 libexec
lrwxrwxrwx. 1 root root 36 8月 29 20:53 mysql -> mysql-5.7.35-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 8月 29 20:44 mysql-5.7.35-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 5月 19 2020 sbin
drwxr-xr-x. 5 root root 49 8月 27 01:37 share
drwxr-xr-x. 2 root root 6 8月 29 20:46 src
[root@localhost local]#
3 修改目录/usr/local/mysql的属主属组
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql
[root@localhost ~]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 8月 29 20:53 /usr/local/mysql -> mysql-5.7.35-linux-glibc2.12-x86_64/
[root@localhost ~]#
4 添加环境变量
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql
[root@localhost ~]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 8月 29 20:53 /usr/local/mysql -> mysql-5.7.35-linux-glibc2.12-x86_64/
[root@localhost ~]# ls /usr/local/mysql
bin docs include lib LICENSE man README share support-files
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >/etc/profile.d/mysql.sh
[root@localhost ~]# cat /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@localhost ~]# source /etc/profile.d/mysql.sh
[root@localhost ~]#
[root@localhost ~]# which mysql
/usr/local/mysql/bin/mysql
[root@localhost ~]#
5 建立数据存放目录
[root@localhost opt]# mkdir -p /opt/data/{3306,3307,3308}
[root@localhost opt]# ls /opt/data/
3306 3307 3308
6 初始化数据库
初始化3306
[root@localhost ]# mysqld --initialize --user mysql --datadir /opt/data/3306
2021-08-29T14:01:45.531940Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-08-29T14:01:45.669941Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-08-29T14:01:45.697912Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-08-29T14:01:45.754188Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: a562bead-08d1-11ec-a320-000c29e1a0bf.
2021-08-29T14:01:45.756362Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-08-29T14:01:46.418009Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2021-08-29T14:01:46.418045Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2021-08-29T14:01:46.418442Z 0 [Warning] CA certificate ca.pem is self signed.
2021-08-29T14:01:46.576722Z 1 [Note] A temporary password is generated for root@localhost: 7c+Da<sYv)&+
[root@localhost ]# echo '7c+Da<sYv)&+' > 3306_pass
[root@localhost ]#
[root@localhost ]# cat 3306_pass
7c+Da<sYv)&+
[root@localhost ]#
初始化3307
[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3307
2021-08-29T14:03:59.340281Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-08-29T14:03:59.484181Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-08-29T14:03:59.507460Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-08-29T14:03:59.512090Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: f51c94c9-08d1-11ec-bf7f-000c29e1a0bf.
2021-08-29T14:03:59.512810Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-08-29T14:04:00.047825Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2021-08-29T14:04:00.047860Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2021-08-29T14:04:00.048234Z 0 [Warning] CA certificate ca.pem is self signed.
2021-08-29T14:04:00.190426Z 1 [Note] A temporary password is generated for root@localhost: NhD7s_6H1Sax
[root@localhost ~]# echo 'NhD7s_6H1Sax' > 3307_pass
初始化3308
[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/3308
2021-08-29T14:06:18.603231Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-08-29T14:06:18.734764Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-08-29T14:06:18.760598Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-08-29T14:06:18.818730Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 48251658-08d2-11ec-a152-000c29e1a0bf.
2021-08-29T14:06:18.819814Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-08-29T14:06:19.505573Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2021-08-29T14:06:19.505601Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2021-08-29T14:06:19.505955Z 0 [Warning] CA certificate ca.pem is self signed.
2021-08-29T14:06:19.530284Z 1 [Note] A temporary password is generated for root@localhost: NKErt?h+>8lz
[root@localhost ~]# echo 'NKErt?h+>8lz' > 3308_pass
[root@localhost ~]# ls
3306_pass 3307_pass 3308_pass
7 安装perl并检查是否还有依赖库没有安装
[root@localhost ~]# yum -y install perl
上次元数据过期检查:1:08:48 前,执行于 2021年08月29日 星期日 20时59分52秒。
依赖关系解决。
==========================================================================
软件包 架构 版本 仓库 大小
==========================================================================
安装:
perl x86_64 4:5.26.3-420.el8 appstream 73 k
安装依赖关系:
dwz x86_64 0.12-10.el8 appstream 109 k
efi-srpm-macros noarch 3-3.el8 appstream 22 k
ghc-srpm-macros noarch 1.4.2-7.el8 appstream 9.3 k
go-srpm-macros noarch 2-17.el8 appstream 13 k
make x86_64 1:4.2.1-10.el8 baseos 498 k
ocaml-srpm-macros noarch 5-4.el8 appstream 9.4 k
openblas-srpm-macros noarch 2-2.el8 appstream 7.9 k
perl-Algorithm-Diff noarch 1.1903-9.el8 baseos 52 k
perl-Archive-Tar noarch 2.30-1.el8 baseos 79 k
perl-Archive-Zip noarch 1.60-3.el8 appstream 108 k
perl-Attribute-Handlers noarch 0.99-420.el8 appstream 89 k
perl-B-Debug noarch 1.26-2.el8 appstream 26 k
perl-CPAN noarch 2.18-397.el8 appstream 554 k
perl-CPAN-Meta noarch 2.150010-396.el8 appstream 191 k
perl-CPAN-Meta-Requirements noarch 2.140-396.el8 appstream 37 k
perl-CPAN-Meta-YAML noarch 0.018-397.el8 appstream 34 k
perl-Carp noarch 1.42-396.el8 baseos 30 k
perl-Compress-Bzip2 x86_64 2.26-6.el8 appstream 72 k
perl-Compress-Raw-Bzip2 x86_64 2.081-1.el8 baseos 40 k
perl-Compress-Raw-Zlib x86_64 2.081-1.el8 baseos 68 k
perl-Config-Perl-V noarch 0.30-1.el8 appstream 22 k
perl-DB_File x86_64 1.842-1.el8 appstream 83 k
perl-Data-Dumper x86_64 2.167-399.el8 baseos 58 k
perl-Data-OptList noarch 0.110-6.el8 appstream 31 k
perl-Data-Section noarch 0.200007-3.el8 appstream 30 k
8 生成配置文件
[root@localhost ~]# vi /etc/my.cnf
[root@localhost ~]# cat /etc/my.cnf
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
[mysqld3306]
datadir = /opt/data/3306
port = 3306
socket = /tmp/3306.sock
pid-file = /opt/date/3306/mysql.pid
log-error = /var/log/mysql_3306.log
[mysqld3307]
datadir = /opt/data/3307
port = 3307
socket = /tmp/3307.sock
pid-file = /opt/date/3307/mysql.pid
log-error = /var/log/mysql_3307.log
[mysqld3308]
datadir = /opt/data/3308
port = 3308
socket = /tmp/3308.sock
pid-file = /opt/date/3308/mysql.pid
log-error = /var/log/mysql_3308.log
[root@localhost ~]#
9 启动各实例
[root@localhost ~]# mysqld_multi start 3336
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 80 :::3307 :::*
LISTEN 0 80 :::3308 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 80 :::3306
10 修改密码
[root@localhost ~]# ls
3306_pass 3307_pass 3308_pass anaconda-ks.cfg
[root@localhost ~]# cat 3306_pass
tdX-*ey)Y1_a
[root@localhost ~]# mysql -uroot -p'tdX-*ey)Y1_a' -h127.0.0.1 -h3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.22
Copyright (c) 2000, 2018, 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> set password = password('123')
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> quit
Bye
[root@localhost ~]# cat 3307_pass
NhD7s_6H1Sax
[root@localhost ~]# mysql -uroot -p'NhD7s_6H1Sax' -h127.0.0.1 -p3307
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.22
Copyright (c) 2000, 2018, 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> set password = password('123
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> quit
Bye
[root@localhost ~]# cat 3308_pass
NKErt?h+>8lz
[root@localhost ~]# mysql -uroot -p'NKErt?h+>8lz' -h127.0.0.1 -P3308
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> set password = password('123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
11 验证
[root@localhost ~]# mysql -uroot -p123 -h127.0.0.1 -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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>
12 开机自启动
在你的安装目录里找到mysqld_multi.server文件。
[root@localhost support-files]# ls
magic mysqld_multi.server mysql-log-rotate mysql.server
然后将它复制到/etc/init.d/目录下:
[root@localhost ~]# ls /etc/init.d/
functions mysqld_multi netconsole network README
在mysqld_multi中的配置添加一个环境变量
export PATH=/usr/local/mysql/bin:$PATH
重启reboot,重置环境,开机之后开启服务
[root@localhost ~]# service mysqld_multi start
[root@localhost ~]# ss - antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 80 :::3307 :::*
LISTEN 0 80 :::3308 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306
最后使用 chkconfig --add mysql_multi设置
chkconfig --list查看
[root@localhost ~]# chkconfig --list
[root@localhost ~]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
二、xtrabackup备份与恢复
1.1 xtrabackup 介绍
MySQL冷备、mysqldump、MySQL热拷贝都无法实现对数据库进行增量备份。在实际生产环境中增量备份是非常实用的,如果数据大于50G或100G,存储空间足够的情况下,可以每天进行完整备份,如果每天产生的数据量较大,需要定制数据备份策略。例如每周实用完整备份,周一到周六实用增量备份。而Percona-Xtrabackup就是为了实现增量备份而出现的一款主流备份工具,xtrabakackup有2个工具,分别是xtrabakup、innobakupe。
ercona-xtrabackup是 Percona公司开发的一个用于MySQL数据库物理热备的备份工具,支持MySQL、Percona server和MariaDB,开源免费,是目前较为受欢迎的主流备份工具。xtrabackup只能备份innoDB和xtraDB两种数据引擎的表,而不能备份MyISAM数据表。
1.2 xtrabackup 优点
-
备份速度快,物理备份可靠
-
备份过程不会打断正在执行的事务(无需锁表)
-
能够基于压缩等功能节约磁盘空间和流量
-
自动备份校验
-
还原速度快
-
可以流传将备份传输到另外一台机器上
-
在不增加服务器负载的情况备份数据
1.3 xtrabackup备份原理
准备和恢复数据阶段
过程如图:
首先应用xtrabackup日志提交事务应用到InnoDB,然后回滚未提交事务。
增量备份过程
对于增量备份只对InnoDB,MyISAM和其它引擎仍然是完整备份的方式,增量备份主要是处理InnoDB中有变更的页(页的LSN).LSN信息在xtrabackup_checkpoints中。
增量应用
恢复过程
流备份过程图
InnoDB表空间的结构
- innobackupex启动后,会先fork一个进程,用于启动xtrabackup,然后等待xtrabackup备份ibd数据文件;
- xtrabackup在备份innoDB数据是,有2种线程:redo拷贝线程和ibd数据拷贝线程。xtrabackup进程开始执行后,会启动一个redo拷贝的线程,用于从最新的checkpoint点开始顺序拷贝redo.log;
- 再启动ibd数据拷贝线程,进行拷贝ibd数据。这里是先启动redo拷贝线程的。在此阶段,innobackupex进行处于等待状态(等待文件被创建)
- xtrabackup拷贝完成ibd数据文件后,会通知innobackupex(通过创建文件),同时xtrabackup进入等待状态(redo线程依旧在拷贝redo.log)
- innobackupex收到xtrabackup通知后哦,执行FLUSH TABLES WITH READ LOCK(FTWRL),取得一致性位点,然后开始备份非InnoDB文件(如frm、MYD、MYI、CSV、opt、par等格式的文件),在拷贝非InnoDB文件的过程当中,数据库处于全局只读状态。
- 当innobackup拷贝完所有的非InnoDB文件后,会通知xtrabackup,通知完成后,进入等待状态;
xtrabackup收到innobackupex备份完成的通知后,会停止redo拷贝线程,然后通知innobackupex,redo.log文件拷贝完成; - innobackupex收到redo.log备份完成后,就进行解锁操作,执行:UNLOCK TABLES;
- 最后innbackupex和xtrabackup进程各自释放资源,写备份元数据信息等,innobackupex等xtrabackup子进程结束后退出
四、xtrabackup的安装部署以及备份恢复实现
1.xtrabackup的安装
[root@localhost ~]# wget https://repo.percona.com/yum/release/7/RPMS/x86_64/percona-xtrabackup-2.3.10-1.el7.x86_64.rpm
--2021-08-29 23:08:03-- https://repo.percona.com/yum/release/7/RPMS/x86_64/percona-xtrabackup-2.3.10-1.el7.x86_64.rpm
正在解析主机 repo.percona.com (repo.percona.com)... 157.245.119.64, 167.71.118.3, 167.99.233.229
正在连接 repo.percona.com (repo.percona.com)|157.245.119.64|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:5235696 (5.0M) [application/x-redhat-package-manager]
正在保存至: “percona-xtrabackup-2.3.10-1.el7.x86_64.rpm”
percona-xtrabackup 100%[==============>] 4.99M 36.1KB/s 用时 8m 55s
2021-08-29 23:17:03 (9.56 KB/s) - 已保存 “percona-xtrabackup-2.3.10-1.el7.x86_64.rpm” [5235696/5235696])
[root@localhost ~]#
2.Xtrabackup中主要包含的两个工具:
xtrabackup:是用于热备innodb,xtradb表中数据的工具,不能备份其他类型的表,也不能备份数据表结构;
innobackupex:是将xtrabackup进行封装的perl脚本,提供了备份myisam表的能力。
常用选项:
一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。因此,此时数据文件仍处理不一致状态。“准备”的主要作用正是通过回滚未提交的事务及同步已经提交的事务至数据文件也使得数据文件处于一致性状态。
选项 | 注释 |
---|---|
-host | 指定主机 |
-user | 指定用户名 |
-password | 指定密码 |
-port | 指定端口 |
-databases | 指定数据库 |
-incremental | 创建增量备份 |
-incremental-basedir | 指定包含完全备份的目录 |
-incremental-dir | 指定包含增量备份的目录 |
-apply-log | 对备份进行预处理操作 |
–redo-only | 不回滚未提交事务 |
–copy-back | 恢复备份目录 |
一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。因此,此时数据文件仍处理不一致状态。“准备”的主要作用正是通过回滚未提交的事务及同步已经提交的事务至数据文件也使得数据文件处于一致性状态。
使用innobackupex备份时,其会调用xtrabackup备份所有的InnoDB表,复制所有关于表结构定义的相关文件(.frm)、以及MyISAM、MERGE、CSV和ARCHIVE表的相关文件,同时还会备份触发器和数据库配置信息相关的文件,这些文件会被保存到一个以时间命名的目录当中。在备份的同时,innobackupex还会在备份目录中创建如下文件:
- 备份类型(如完全或增量)、备份状态(如是否已经为prepared状态)和LSN(日志序列号)范围信息:每个InnoDB页(通常为16k大小),都会包含一个日志序列号,即LSN,LSN是整个数据库系统的系统版本号,每个页面相关的LSN能够表明此页面最近是如何发生改变的。
xtrabackup_binlog_info – mysql服务器当前正在使用的二进制日志文件及备份这一刻位置二进制日志时间的位置 - xtrabackup_binlog_pos_innodb – 二进制日志文件及用于InnoDB或XtraDB表的二进制日志文件的当前position
xtrabackup_binary – 备份中用到的xtrabackup的可执行文件;
backup-my.cnf – 备份命令用到的配置选项信息:
在使用innobackupex进行备份时,还可以使用–no-timestamp选项来阻止命令自动创建一个以时间命名的目录:如此一来,innobackupex命令将会创建一个BACKUP-DIR目录来存储备份数据。
如果要使用一个最小权限的用户进行备份,则可基于如下命令创建此类用户:如果要使用一个最小权限的用户进行备份,则可基于如下命令创建此类用户:
mysql> CREATE USER 'bkpuser'@'localhost' IDENTIFIED BY '123456'; #创建用户
mysql> REVOKE ALL PRIVILEGES,GRANT OPTION FROM 'bkpuser'; #回收此用户所有权限
mysql> GRANT RELOAD,LOCK TABLES,RELICATION CLIENT ON *.* TO 'bkpuser'@'localhost'; #授权刷新、锁定表、用户查看服务器状态
mysql> FLUSH PRIVILEGES; #刷新授权表
注意:备份时需启动MySQL,恢复时需关闭MySQL,清空mysql数据目录且不能重新初始化,恢复数据后应该立即进行一次完全备份
3. 实例
全量备份+增量备份
基本语法:innobackupex --user=root(用户) --password=密码 /path/to/BACKUP
- –defaults-file=/etc/my.cnf 指定mysql的配置文件my.cfg,如果指定则必须是第一个参数。
- –defaults-file=/etc/my.cnf 指定mysql的配置文件my.cfg,如果指定则必须是第一个参数。
创建备份目录,full为完全备份目录,inc为增量备份目录
[root@localhost ~]# mkdir -p /opt/mysqlbackup/{full,inc}
[root@localhost ~]#
进行全量备份并把它放在/opt/mysqlbackup/full/下
[root@localhost ~]# innobackupex --user=root--password=xu1 /opt/mysqlbackup/full/
MySQL binlog position:filename 'mysql-bin.000004', position '107'
210827 21:03:23 [00]Writing backup-my.cnf
210827 23:23:23 [00] ...done
210827 23:23:23 [00]Writing xtrabackup_info
210827 23:23:23 [00] ...done
xtrabackup: Transactionlog of lsn (1595675) to (1595675) was copied.
210827 23:23:24 completedOK!
查看全量备份的文件
[root@localhost ~]# ls /opt/mysqlbackup/full
2021-08-27_23-23-48
[root@localhost ~]# ls /opt/mysqlbackup/full/2021-08-27_23-23-48/
backup-my.cnf mysql xtrabackup_binlog_info xtrabackup_info
ibdata1 performance_schema xtrabackup_checkpoints xtrabackup_logfile
备份数据库的用户需要具有相应权限
[root@localhost ~]#mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
进行增量之前的准备 查看日志位置
[root@localhost ~]# cat/opt/mysqlbackup/full/2021-08-27_23-23-52/xtrabackup_binlog_info
mysql-bin.000004 107
登录mysql数据库,进行操作
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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 abc;
Query OK, 1 row affected(0.00 sec)
mysql> use abc;
Database changed
mysql> create table cai (id int,name char(16) not null);
Query OK, 0 rows affected(0.05 sec)
mysql> insert into cai values(1,'tom');
Query OK, 1 row affected(0.02 sec)
mysql> insert into cai values(2,'jerry');
Query OK, 1 row affected(0.02 sec)
mysql> select * from cai;
+------+----------+
| id | name |
+------+----------+
| 1 | tom |
| 2 | jerry |
+------+----------+
2 rows in set (0.00 sec)
mysql> quit
Bye
增量备份二进制文件
[root@localhost ~]# mysqlbinlog --start-position=107 /usr/local/mysql/data/mysql-bin.000004> /opt/mysqlbackup/inc/`date +%F`.sql
[root@localhost ~]# ls/opt/mysqlbackup/inc/
2021-08-27.sql
模拟数据库损坏
[root@localhost ~]# mv /usr/local/mysql/data/* /tmp/
一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。因此,此时数据文件仍处于不一致状态。“准备”的主要作用正是通过回滚未提交的事务及同步已经提交的事务至数据文件也使得数据文件处于一致性状态。
在准备(prepare)过程结束后,InnoDB表数据已经前滚到整个备份结束的点,而不是回滚到xtrabackup刚开始时的点。
innobakupex命令的–apply-log选项可用于实现上述功能。如下面的命令:
[root@localhost ~]# innobackupex --apply-log/opt/mysqlbackup/full/2021-08-27_23-33-18/
.........
InnoDB: File './ibtmp1'size is now 12 MB.
InnoDB: 96 redo rollbacksegment(s) found. 1 redo rollback segment(s) are active.
InnoDB: 32 non-redorollback segment(s) are active.
InnoDB: 5.7.13 started;log sequence number 1595925
xtrabackup: startingshutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimizethread exiting.
InnoDB: Startingshutdown...
InnoDB: Shutdowncompleted; log sequence number 1595944
210827 23:23:42 completedOK!
使用innobakupex命令的 --copy-back 进行拷贝
[root@localhost ~]# innobackupex --copy-back/opt/mysqlbackup/full/2021-08-27_23-23-16/
...........
210827 23:36:07 [01] ...done
210827 21:36:07 [01]Copying ./performance_schema/events_waits_current.frm to/usr/local/mysql/data/performance_schema/events_waits_current.frm
210827 23:35:17 [01] ...done
210827 23:36:24 completedOK!
查看结果
[root@localhost ~]# ll /usr/local/mysql/data/
总用量 40972
-rw-r-----. 1 root root18874368 8月 27 21:36 ibdata1
-rw-r-----. 1 rootroot 5242880 8月 27 23:36 ib_logfile0
-rw-r-----. 1 rootroot 5242880 8月 2721:36 ib_logfile1
-rw-r-----. 1 root root12582912 8月 27 23:36 ibtmp1
drwxr-x---. 2 rootroot 4096 8月 27 23:36 mysql
drwxr-x---. 2 rootroot 4096 8月 27 23:36 performance_schema
-rw-r-----. 1 rootroot 478 8月 27 23:36 xtrabackup_info
修改属主属组
[root@localhost ~]# chown -R mysql:mysql/usr/local/mysql/data/
杀死 掉mysql 的进程
[root@localhost ~]# killall mysqld
重启服务
[root@localhost ~]# service mysqld start
Starting MySQL..
登录查看
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQLmonitor. Commands end with ; or \g.
Your MySQL connection idis 1
Server version: 5.5.22-logSource distribution
Copyright (c) 2000, 2011,Oracle and/or its affiliates. All rights reserved.
Oracle is a registeredtrademark of Oracle Corporation and/or its
affiliates. Other namesmay be trademarks of their respective
owners.
Type 'help;' or '\h' forhelp. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.01 sec)
我们可以看到在完全备份后创建的数据并没有恢复,这时就需要用增量备份来还原
为了避免产生大量的二进制日志,可以把二进制日志关掉
mysql> set sql_log_bin=0;
Query OK, 0 rows affected(0.00 sec)
读取增量备份文件
mysql> source/opt/mysqlbackup/inc/2023-08-27.sql
Query OK, 0 rows affected(0.00 sec)
Query OK, 0 rows affected(0.00 sec)
Query OK, 0 rows affected(0.00 sec)
Query OK, 0 rows affected (0.00sec)
mysql> set sql_log_bin=1;
Query OK, 0 rows affected(0.00 sec)
mysql> show databases;
+--------------------+![请添加图片描述](https://img-blog.csdnimg.cn/f31a8416f0fd421aab52e5022a4bcbb7.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5rGf5Z-O6ICBSw==,size_20,color_FFFFFF,t_70,g_se,x_16)
| Database |
+--------------------+
| information_schema |
| cai |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
mysql> use benet
Database changed
mysql> select * from cai;
+------+----------+
| id | name |
+------+----------+
| 1 | tom |
| 2 | jerry |
+------+----------+
2 rows in set (0.01 sec)
ry OK, 0 rows affected (0.00sec)
mysql> set sql_log_bin=1;
Query OK, 0 rows affected(0.00 sec)
mysql> show databases;
+--------------------+![请添加图片描述](https://img-blog.csdnimg.cn/f31a8416f0fd421aab52e5022a4bcbb7.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5rGf5Z-O6ICBSw==,size_20,color_FFFFFF,t_70,g_se,x_16)
| Database |
+--------------------+
| information_schema |
| cai |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
mysql> use benet
Database changed
mysql> select * from cai;
+------+----------+
| id | name |
+------+----------+
| 1 | tom |
| 2 | jerry |
+------+----------+
2 rows in set (0.01 sec)