mysql多实例部署 & Xtrabackup

mysql多实例部署

软件下载

//下载二进制格式的mysql软件包
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
--2018-08-13 23:56:27--  https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
Resolving downloads.mysql.com (downloads.mysql.com)... 137.254.60.14
Connecting to downloads.mysql.com (downloads.mysql.com)|137.254.60.14|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz [following]
......
Saving to: ‘mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz’
    
100%[=====================================>] 643,790,848 2.46MB/s   in 4m 20s
    
2018-08-14 00:00:50 (2.36 MB/s) - ‘mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz’saved [643790848/643790848]
配置用户和组并解压二进制程序至/usr/local下

//创建用户和组
[root@localhost src]# groupadd -r mysql
[root@localhost src]# useradd -M -s /sbin/nologin -g mysql mysql


//解压软件至/usr/local/
[root@localhost src]# ls
debug  kernels  mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# ls /usr/local/
bin  games    lib    libexec                              sbin   src
etc  include  lib64  mysql-5.7.22-linux-glibc2.12-x86_64  share
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.22-linux-glibc2.12-x86_64/’
[root@localhost local]# ll
total 0
drwxr-xr-x. 2 root root   6 Mar 10  2016 bin
drwxr-xr-x. 2 root root   6 Mar 10  2016 etc
drwxr-xr-x. 2 root root   6 Mar 10  2016 games
drwxr-xr-x. 2 root root   6 Mar 10  2016 include
drwxr-xr-x. 2 root root   6 Mar 10  2016 lib
drwxr-xr-x. 2 root root   6 Mar 10  2016 lib64
drwxr-xr-x. 2 root root   6 Mar 10  2016 libexec
lrwxrwxrwx  1 root root  36 Aug 14 16:00 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x  9 root root 129 Aug 14 00:16 mysql-5.7.22-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 Mar 10  2016 sbin
drwxr-xr-x. 5 root root  49 Jun 13 19:03 share
drwxr-xr-x. 2 root root   6 Mar 10  2016 src


//修改目录/usr/local/mysql的属主属组
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql
[root@localhost ~]# ll /usr/local/mysql -d
lrwxrwxrwx 1 mysql mysql 36 Aug 14 16:00 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/

//配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# . /etc/profile.d/mysql.sh
[root@localhost ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

创建各实例数据存放的目录

[root@wxy ~]#  mkdir -p /opt/data/{3306,3307,3308}
[root@wxy ~]# chown -R mysql.mysql /opt/data/
[root@wxy ~]# ll /opt/data/
总用量 123852
drwxr-xr-x 2 mysql mysql        6 8月  28 05:23 3306
drwxr-xr-x 2 mysql mysql        6 8月  28 05:23 3307
drwxr-xr-x 2 mysql mysql        6 8月  28 05:23 3308

[root@wxy ~]# tree /opt/data/
/opt/data/
├── 3306
├── 3307
├── 3308

初始化各实例

[root@wxy ~]# mysqld --initialize --datadir=/opt/data/3306 --user=mysql
2021-08-28T09:27:39.593442Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-08-28T09:27:39.763537Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-08-28T09:27:39.789718Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-08-28T09:27:39.855122Z 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: 3073f121-07e2-11ec-bcec-000c299fb9ba.
2021-08-28T09:27:39.855859Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-08-28T09:27:41.147694Z 0 [Warning] CA certificate ca.pem is self signed.
2021-08-28T09:27:41.416304Z 1 [Note] A temporary password is generated for root@localhost: ItkzjPgkL0*S
[root@wxy ~]# echo 'ItkzjPgkL0*S' > 3306-pass


[root@wxy ~]#  mysqld --initialize --datadir=/opt/data/3307 --user=mysql
2021-08-28T09:28:15.706432Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-08-28T09:28:15.871669Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-08-28T09:28:15.900075Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-08-28T09:28:15.960069Z 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: 45f91ece-07e2-11ec-8251-000c299fb9ba.
2021-08-28T09:28:15.965815Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-08-28T09:28:16.504898Z 0 [Warning] CA certificate ca.pem is self signed.
2021-08-28T09:28:16.644171Z 1 [Note] A temporary password is generated for root@localhost: #9rdpF-=w4;8
[root@wxy ~]# echo '#9rdpF-=w4;8' > 3307-pass


[root@wxy ~]# mysqld --initialize --datadir=/opt/data/3308 --user=mysql
2021-08-28T09:28:53.385289Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-08-28T09:28:53.584123Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-08-28T09:28:53.621762Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-08-28T09:28:53.744819Z 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: 5c7e9d8b-07e2-11ec-886b-000c299fb9ba.
2021-08-28T09:28:53.745488Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-08-28T09:28:54.516974Z 0 [Warning] CA certificate ca.pem is self signed.
2021-08-28T09:28:54.742426Z 1 [Note] A temporary password is generated for root@localhost: TsW>rb23Q>5D
[root@wxy ~]# echo 'TsW>rb23Q>5D' > 3308-pass

安装perl

[root@wxy ~]# yum install -y perl

[root@wxy ~]# ldd /usr/local/mysql/bin/mysql      //查看程序文件依赖的包  若没有则显示not found
        linux-vdso.so.1 (0x00007ffc5075d000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fb2b3890000)
        librt.so.1 => /lib64/librt.so.1 (0x00007fb2b3687000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007fb2b3483000)
        libncurses.so.5 => /lib64/libncurses.so.5 (0x00007fb2b325d000)
        libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007fb2b2ec8000)
        libm.so.6 => /lib64/libm.so.6 (0x00007fb2b2b46000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fb2b292e000)
        libc.so.6 => /lib64/libc.so.6 (0x00007fb2b256c000)
        libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007fb2b2341000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fb2b3ab0000)
      
[root@wxy ~]# yum whatprovides libtinfo.so.5   //查找由哪个包提供

配置配置文件/etc/my.cnf

[root@wxy ~]# 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/mysql3306.sock
pid-file = /opt/data/3306/mysql_3306.pid
log-error=/var/log/3306.log

[mysqld3307]
datadir = /opt/data/3307
port = 3307
socket = /tmp/mysql3307.sock
pid-file = /opt/data/3307/mysql_3307.pid
log-error=/var/log/3307.log

[mysqld3308]
datadir = /opt/data/3308
port = 3308
socket = /tmp/mysql3308.sock
pid-file = /opt/data/3308/mysql_3308.pid
log-error=/var/log/3308.log

启动3306、3307、3308实例

[root@wxy ~]# mysqld_multi start  //启动全部
/
[root@wxy ~]# mysqld_multi start 3306
[root@wxy ~]# mysqld_multi start 3307
[root@wxy ~]# mysqld_multi start 3308
[root@wxy ~]# ss -antl
LISTEN   0        80                      *:3306                  *:*      
LISTEN   0        80                      *:3307                  *:*      
LISTEN   0        80                      *:3308                  *:* 

kill -9  关闭

初始化密码

[root@wxy ~]# cat 3306-pass
ItkzjPgkL0*S
[root@wxy ~]# mysql -uroot -p'ItkzjPgkL0*S' -S /tmp/mysql3306.sockmysql: [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.34

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('1');
Query OK, 0 rows affected, 1 warning (0.01 sec)

[root@wxy ~]# cat 3307-pass
#9rdpF-=w4;8
[root@wxy ~]# mysql -uroot -p'#9rdpF-=w4;8' -S /tmp/mysql3307.sock
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.34

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('1');
Query OK, 0 rows affected, 1 warning (0.00 sec)

[root@wxy ~]# cat 3308-pass
TsW>rb23Q>5D
[root@wxy ~]# mysql -uroot -p'TsW>rb23Q>5D' -S /tmp/mysql3308.sock
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.34

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('1');
Query OK, 0 rows affected, 1 warning (0.00 sec)

3306:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.02 sec)

mysql> create database yy
    -> ;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| yy                 |
+--------------------+
5 rows in set (0.00 sec)


3307:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

多实例设置开机自启
1.把/usr/local/mysql/bin/support_files/mysqld_multi.server文件复制到/etc/init.d/mysqld_multi
2.在/etc/init.d/mysqld_multi文件中增加环境变量设置export PATH=/usr/local/mysql/bin: $PATH
3.chkconfig --add mysqld_multi

Xtrabackup备份与恢复

一、Xtrabackup介绍

MySQL冷备、mysqldump、MySQL热拷贝都无法实现对数据库进行增量备份。在实际生产环境中增量备份是非常实用的,如果数据大于50G或100G,存储空间足够的情况下,可以每天进行完整备份,如果每天产生的数据量较大,需要定制数据备份策略。例如每周实用完整备份,周一到周六实用增量备份。而Percona-Xtrabackup就是为了实现增量备份而出现的一款主流备份工具,xtrabakackup有2个工具,分别是xtrabakup、innobakupe。

Percona-xtrabackup是 Percona公司开发的一个用于MySQL数据库物理热备的备份工具,支持MySQL、Percona server和MariaDB,开源免费,是目前较为受欢迎的主流备份工具。xtrabackup只能备份innoDB和xtraDB两种数据引擎的表,而不能备份MyISAM数据表。

二、Xtrabackup优点

(1)备份速度快,物理备份可靠

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

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

(4)自动备份校验

(5)还原速度快

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

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

三、Xtrabackup备份原理

在这里插入图片描述
1、innobackupex 在启动后,会先 fork 一个进程,启动 xtrabackup进程,然后就等待 xtrabackup 备 份完 ibd 数据文件;
2、xtrabackup 在备份 InnoDB 相关数据时,是有2种线程的,1种是 redo 拷贝线程,负责拷贝 redo 文件,1种是 ibd 拷贝线程,负责拷贝 ibd 文件;redo 拷贝线程只有一个,在 ibd 拷贝线程之前启动,在 ibd 线程结束后结束。xtrabackup 进程开始执行后,先启动 redo 拷贝线程,从最新的 checkpoint 点开始顺序拷贝 redo 日志;然后再启动 ibd 数据拷贝线程,在 xtrabackup 拷贝 ibd 过程中/3、innobackupex 进程一直处于等待状态(等待文件被创建)。
4、xtrabackup 拷贝完成idb后,通知 innobackupex(通过创建文件),同时自己进入等待(redo 线程仍然继续拷贝);
5、innobackupex 收到 xtrabackup 通知后,执行FLUSH TABLES WITH READ LOCK (FTWRL),取得一致性位点,然后开始备份非 InnoDB 文件(包括 frm、MYD、MYI、CSV、opt、par等)。拷贝非 InnoDB 文件过程中,因为数据库处于全局只读状态,如果在业务的主库备份的话,要特别小心,非 InnoDB 表(主要是MyISAM)比较多的话整库只读时间就会比较长,这个影响一定要评估到。
6、当 innobackupex 拷贝完所有非 InnoDB 表文件后,通知 xtrabackup(通过删文件) ,同时自己进入等待(等待另一个文件被创建);
7、xtrabackup 收到 innobackupex 备份完非 InnoDB 通知后,就停止 redo 拷贝线程,然后通知 innobackupex redo log 拷贝完成(通过创建文件);
8、innobackupex 收到 redo 备份完成通知后,就开始解锁,执行 UNLOCK TABLES;
9、最后 innobackupex 和 xtrabackup 进程各自完成收尾工作,如资源的释放、写备份元数据信息等,innobackupex 等待 xtrabackup 子进程结束后退出。

四、xtrabackup的安装部署以及备份恢复实现

1、xtrabackup的安装

下载地址:https://centos.pkgs.org/8/percona-x86_64/percona-xtrabackup-24-2.4.23-1.el8.x86_64.rpm.html

[root@wxy ~]# wget https://repo.percona.com/yum/release/8/RPMS/x86_64/percona-xtrabackup-24-2.4.23-1.el8.x86_64.rpm
--2021-08-28 07:34:06--  https://repo.percona.com/yum/release/8/RPMS/x86_64/percona-xtrabackup-24-2.4.23-1.el8.x86_64.rpm
正在解析主机 repo.percona.com (repo.percona.com)... 167.71.118.3, 167.99.233.229, 157.245.119.64
正在连接 repo.percona.com (repo.percona.com)|167.71.118.3|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:8060704 (7.7M) [application/x-redhat-package-manager]
正在保存至: “percona-xtrabackup-24-2.4.23-1.el8.x86_64.rpm”

percona-xtrabackup 100%[===============>]   7.69M  3.43MB/s  用时 2.2s    

2021-08-28 07:34:10 (3.43 MB/s) - 已保存 “percona-xtrabackup-24-2.4.23-1.el8.x86_64.rpm” [8060704/8060704])

Xtrabackup中主要包含两个工具:
xtrabackup:是用于热备innodb,xtradb表中数据的工具,不能备份其他类型的表,也不能备份数据表结构;
innobackupex:是将xtrabackup进行封装的perl脚本,提供了备份myisam表的能力。
常用选项:
–host 指定主机
–user 指定用户名
–password 指定密码
–port 指定端口
–databases 指定数据库
–incremental 创建增量备份
–incremental-basedir 指定包含完全备份的目录
–incremental-dir 指定包含增量备份的目录
–apply-log 对备份进行预处理操作
一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。因此,此时数据文件仍处理不一致状态。“准备”的主要作用正是通过回滚未提交的事务及同步已经提交的事务至数据文件也使得数据文件处于一致性状态。
–redo-only 不回滚未提交事务
–copy-back 恢复备份目录

2、xtrabackup全量备份与恢复

全库备份与恢复三步曲:

a. innobackupex全量备份,并指定备份目录路径;

b. 在恢复前,需要使用–apply-log参数先进行合并数据文件,确保数据的一致性要求;

c. 恢复时,直接使用–copy-back参数进行恢复,需要注意的是,在my.cnf中要指定数据文件目录的路径。

3、xtrabackup增量备份与恢复

增量备份
PXB 是支持增量备份的,但是只能对 InnoDB 做增量,InnoDB 每个 page 有个 LSN 号,LSN 是全局递增的,page 被更改时会记录当前的 LSN 号,page中的 LSN 越大,说明当前page越新(最近被更新)。每次备份会记录当前备份到的LSN(xtrabackup_checkpoints 文件中),增量备份就是只拷贝LSN大于上次备份的page,比上次备份小的跳过,每个 ibd 文件最终备份出来的是增量 delta 文件。

MyISAM 是没有增量的机制的,每次增量备份都是全部拷贝的。

增量备份过程和全量备份一样,只是在 ibd 文件拷贝上有不同。

恢复过程
如果看恢复备份集的日志,会发现和 mysqld 启动时非常相似,其实备份集的恢复就是类似 mysqld crash后,做一次 crash recover。

恢复的目的是把备份集中的数据恢复到一个一致性位点,所谓一致就是指原数据库某一时间点各引擎数据的状态,比如 MyISAM 中的数据对应的是 15:00 时间点的,InnoDB 中的数据对应的是 15:20 的,这种状态的数据就是不一致的。PXB 备份集对应的一致点,就是备份时FTWRL的时间点,恢复出来的数据,就对应原数据库FTWRL时的状态。

因为备份时 FTWRL 后,数据库是处于只读的,非 InnoDB 数据是在持有全局读锁情况下拷贝的,所以非 InnoDB 数据本身就对应 FTWRL 时间点;InnoDB 的 ibd 文件拷贝是在 FTWRL 前做的,拷贝出来的不同 ibd 文件最后更新时间点是不一样的,这种状态的 ibd 文件是不能直接用的,但是 redo log 是从备份开始一直持续拷贝的,最后的 redo 日志点是在持有 FTWRL 后取得的,所以最终通过 redo 应用后的 ibd 数据时间点也是和 FTWRL 一致的。

所以恢复过程只涉及 InnoDB 文件的恢复,非 InnoDB 数据是不动的。备份恢复完成后,就可以把数据文件拷贝到对应的目录,然后通过mysqld来启动了。

总结:

(1)增量备份需要使用参数–incremental指定需要备份到哪个目录,使用incremental-dir指定全备目录;

(2)进行数据备份时,需要使用参数–apply-log redo-only先合并全备数据目录数据,确保全备数据目录数据的一致性;

(3)再将增量备份数据使用参数–incremental-dir合并到全备数据当中;

(4)最后通过最后的全备数据进行恢复数据,注意,如果有多个增量备份,需要逐一合并到全备数据当中,再进行恢复。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值