多实例部署、开机自启、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 kefi-srpm-macros              noarch 3-3.el8              appstream  22 kghc-srpm-macros              noarch 1.4.2-7.el8          appstream 9.3 kgo-srpm-macros               noarch 2-17.el8             appstream  13 kmake                         x86_64 1:4.2.1-10.el8       baseos    498 kocaml-srpm-macros            noarch 5-4.el8              appstream 9.4 kopenblas-srpm-macros         noarch 2-2.el8              appstream 7.9 kperl-Algorithm-Diff          noarch 1.1903-9.el8         baseos     52 kperl-Archive-Tar             noarch 2.30-1.el8           baseos     79 kperl-Archive-Zip             noarch 1.60-3.el8           appstream 108 kperl-Attribute-Handlers      noarch 0.99-420.el8         appstream  89 kperl-B-Debug                 noarch 1.26-2.el8           appstream  26 kperl-CPAN                    noarch 2.18-397.el8         appstream 554 kperl-CPAN-Meta               noarch 2.150010-396.el8     appstream 191 kperl-CPAN-Meta-Requirements  noarch 2.140-396.el8        appstream  37 kperl-CPAN-Meta-YAML          noarch 0.018-397.el8        appstream  34 kperl-Carp                    noarch 1.42-396.el8         baseos     30 kperl-Compress-Bzip2          x86_64 2.26-6.el8           appstream  72 kperl-Compress-Raw-Bzip2      x86_64 2.081-1.el8          baseos     40 kperl-Compress-Raw-Zlib       x86_64 2.081-1.el8          baseos     68 kperl-Config-Perl-V           noarch 0.30-1.el8           appstream  22 kperl-DB_File                 x86_64 1.842-1.el8          appstream  83 kperl-Data-Dumper             x86_64 2.167-399.el8        baseos     58 kperl-Data-OptList            noarch 0.110-6.el8          appstream  31 kperl-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.22Copyright (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.22Copyright (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.35Copyright (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 - antlState      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. 备份速度快,物理备份可靠

  2. 备份过程不会打断正在执行的事务(无需锁表)

  3. 能够基于压缩等功能节约磁盘空间和流量

  4. 自动备份校验

  5. 还原速度快

  6. 可以流传将备份传输到另外一台机器上

  7. 在不增加服务器负载的情况备份数据

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.cnf210827  23:23:23 [00]        ...done210827 23:23:23 [00]Writing xtrabackup_info210827  23:23:23 [00]        ...donextrabackup: 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_infoibdata1        performance_schema  xtrabackup_checkpoints  xtrabackup_logfile备份数据库的用户需要具有相应权限
[root@localhost ~]#mysql -uroot -pEnter password:Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35Copyright (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_infomysql-bin.000004   107登录mysql数据库,进行操作
[root@localhost ~]# mysql -uroot -pEnter password:Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35Copyright (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 changedmysql> 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> quitBye

增量备份二进制文件

[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 1595925xtrabackup: startingshutdown with innodb_fast_shutdown = 1InnoDB: FTS optimizethread exiting.InnoDB: Startingshutdown...InnoDB: Shutdowncompleted; log sequence number 1595944210827 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]        ...done210827 21:36:07 [01]Copying ./performance_schema/events_waits_current.frm to/usr/local/mysql/data/performance_schema/events_waits_current.frm210827 23:35:17 [01]        ...done210827 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 ibtmp1drwxr-x---. 2 rootroot     4096 8月  27 23:36 mysqldrwxr-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 startStarting MySQL..登录查看
[root@localhost ~]# mysql -uroot -pEnter password:Welcome to the MySQLmonitor.  Commands end with ; or \g.Your MySQL connection idis 1Server version: 5.5.22-logSource distributionCopyright (c) 2000, 2011,Oracle and/or its affiliates. All rights reserved.Oracle is a registeredtrademark of Oracle Corporation and/or itsaffiliates. Other namesmay be trademarks of their respectiveowners.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.sqlQuery 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 benetDatabase changedmysql> 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 benetDatabase changedmysql> select * from cai;+------+----------+| id   | name    |+------+----------+|    1 | tom ||    2 | jerry |+------+----------+2 rows in set (0.01 sec)

多实例部署、开机自启、xtrabackup备份与恢复相关推荐

  1. mysql数据库备份与恢复,mysql多实例部署

    mysql数据库备份与恢复,mysql多实例部署 文章目录 mysql数据库备份与恢复,mysql多实例部署 1: 数据库备份 2:冷备份 3:热备份 4:mysql备份工具mysqldump 5:差 ...

  2. MySQL 多实例部署 xtrabackup备份与恢复

    MySQL 多实例部署 1.下载安装包 [root@localhost ~]# wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7. ...

  3. mysql 多实例部署、xtrabackup下载与安装

    mysql 多实例部署 一. 二进制安装mysql 软件下载 下载网络源 curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyu ...

  4. Linux(Centos7)下redis5安装、部署、开机自启

    1.什么是redis redis是用C语言开发的一个开源的高性能键值对(key-value)数据库.它通过提供多种键值数据类型来适应不同场景下的存储需求,目前为止redis支持的键值数据类型如下字符串 ...

  5. Redis单机部署、添加开机自启、配置参数

    1.Redis简介 redis是使用C语言编写的开源的,支持网络,基于内存,可持久性的键值对存储数据库,2013年5月之前,Redis是最流行的键值对存储数据库,Redis采用内存数据集,支持多种数据 ...

  6. InfluxData【环境搭建 03】时序数据库 InfluxDB 离线安装配置使用(下载+安装+端口绑定+管理员用户创建+开启密码认证+开机自启配置)完整流程实例分享

    1.下载安装包 安装包地址:https://repos.influxdata.com/rhel/\$releasever/\$basearch/stable,可根据版本号和分支进行下载. # 本次安装 ...

  7. 【ubuntu】vue项目部署 + 谷歌浏览器开机自启 + 项目自启 + 不允许退出全屏

    目录 项目需求 操作 项目需求 在工控机上查看项目,操作者不能进行网页无关操作 => 需要谷歌开机自启,项目自启,禁用键盘无关操作 全屏之后不允许退出全屏 操作 全屏之后不允许退出全屏:启动ch ...

  8. mysql多实例部署

    二进制文件下载 [root@localhost ~]# cd /usr/src/ [root@localhost src]# wget https://downloads.mysql.com/arch ...

  9. CentOS7下安装配置zookeeper集群及设置开机自启

    1.在做zookeeper集群之前需要有至少三台centos机器或虚拟机 一.Zookeeper原理简介 ZooKeeper是一个开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程 ...

最新文章

  1. 使用 sched_setaffinity 将线程绑到CPU核上运行
  2. OLE 操作Excel 详解(转)
  3. 如何退出Activity?如何安全退出已调用多个Activity的Application?
  4. linux:安装ubuntu18-04
  5. 疲劳容器的定义_疲劳分析基础
  6. Python基础day01【软件安装、变量的定义和使用、数据类型、标识符和关键字、输入输出、数据类型转换、运算符、PEP 8 规范】
  7. 工业大数据的应用与实践
  8. 最新编程语言排名:Python超Java、JS保持领头羊
  9. 25岁之后,你更应该逼自己系统性成长
  10. Apache多站点配置
  11. java开关语句_Java中嵌套开关语句的替代方法
  12. Data Lake Analytics的Geospatial分析函数 1
  13. 创建3层的服务模板 (2)--- App-V package 和 Application Profile
  14. 关于XRD你知道多少
  15. 饥荒steam联机版服务器无响应,《饥荒:联机版》服务器卡顿原因分析及解决教程...
  16. html embed音乐循环,加入视频或音乐——embed基本语法
  17. 一款开源的轻量级企业流程开发平台,源码分享
  18. walking机器人入门教程-工具-命令管理器
  19. c#短信接口代码实现(发短信)
  20. 1144_Docker的基本介绍

热门文章

  1. iec104点号_IEC104报文遥测信息提取方法与流程
  2. 自由浏览器 android,安卓浏览器现重大漏洞 UC浏览器成唯一幸存者
  3. 通用分页控件(DataGrid,DataList,Repeater都可以用它来分页)
  4. 双麦阵列拾音束降噪模块A-68连接应用说明和强噪音下测试效果
  5. “每逢佳节倍思亲”的不是游子,而是父母。
  6. 给你介绍下,Hippo4J 动态线程池基础架构
  7. ASOP使用RefBase的小细节
  8. 云端移动巧妙获取网站访客手机号码
  9. java中DateTime类的使用
  10. 服务器型号变更说明,服务器规格变更类