Mysql--日常维护-不停主master做主从同步
我们目前所用到的在Mysql master不停机情况下做同步操作的主流工具都是 XtraBackup 包括阿里云、腾讯都是采用此工具来备份解压。
MySQL主从同步原理:
MySQL主从同步是在MySQL主从复制(Master-Slave Replication)基础上实现的,通过设置在Master MySQL上的binlog(使其处于打开状态),Slave MySQL上通过一个I/O线程从Master MySQL上读取binlog,然后传输到Slave MySQL的中继日志中,然后Slave MySQL的SQL线程从中继日志中读取中继日志,然后应用到Slave MySQL的数据库中。
文章图片
XtraBackup备份原理:
innobackupex在后台线程不断追踪InnoDB的日志文件,然后复制InnoDB的数据文件。数据文件复制完成之后,日志的复制线程也会结束。这样就得到了不在同一时间点的数据副本和开始备份以后的事务日志。完成上面的步骤之后,就可以使用InnoDB崩溃恢复代码执行事务日志(redo log),以达到数据的一致性。
备份过程
1 ) backup,备份阶段,追踪事务日志和复制数据文件(物理备份)。
2 ) preparing,重放事务日志,使所有的数据处于同一个时间点,达到一致性状态。
XtraBackup的优点:
1)备份速度快,物理备份可靠
2)备份过程不会打断正在执行的事务(无需锁表)
3)能够基于压缩等功能节约磁盘空间和流量
4)自动备份校验
5)还原速度快
6)可以流传将备份传输到另外一台机器上
7)在不增加服务器负载的情况备份数据
xtrabackup安装
[root@localhost opt]# yum -y install perl perl-devel libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL libev-devel rsync perl-Digest-MD5
Centos 6
[root@localhost opt]# wget https://www.percona.com/downl...
[root@localhost opt]# yum --enablerepo=epel localinstall percona-xtrabackup-2.3.2-1.el6.x86_64.rpm -y
Centos 7
[root@localhost opt]# wget https://downloads.percona.com...
[root@localhost opt]# rpm -ivh percona-xtrabackup-24-2.4.22-1.el7.x86_64.rpm -y
[root@localhost opt]# whereis innobackupex
innobackupex: /usr/bin/innobackupex /usr/share/man/man1/innobackupex.1.gz
备份数据库
[root@localhost opt]# innobackupex --defaults-file=/etc/my.cnf --user=账户 --password=密码 --socket=/home/mysql/mysql.sock /home/BackupData/"
恢复数据库
注: 核对mysql的版本尽量保证数据库版本一致性。
slave机器上操作
[root@localhost opt]#mkdir 20210831_00
[root@localhost opt]#ll 20210831_00.tar.gz
-rw-r--r-- 1 root root 508147921 Aug 31 08:53 20210831_00.tar.gz
[root@localhost opt]#tar -zxf 20210831_00.tar.gz -C ./20210831_00/
[root@localhost opt]# ls -l ./20210831_00
-rw-rw---- 1 root root484 Aug 31 00:23 backup-my.cnf
-rw-rw---- 1 root root 104857600 Aug 31 00:06 ibdata1
drwxr-xr-x 2 root root4096 Aug 31 14:25 mysql
drwxr-xr-x 2 root root4096 Aug 31 14:25 performance_schema
-rw-rw---- 1 root root75 Aug 31 00:23 xtrabackup_binlog_info
-rw-rw---- 1 root root147 Aug 31 00:23 xtrabackup_checkpoints
-rw-rw---- 1 root root641 Aug 31 00:23 xtrabackup_info
-rw-rw---- 1 root root2747392 Aug 31 00:23 xtrabackup_logfile
在Mysql的数据库配置文件中,指定的数据存放目录创建对应文件夹
如:配置文件配置的 datadir = /home/mysql
[root@localhost home]# mkdir /home/mysql
恢复日志文件
[root@localhost home]# innobackupex --defaults-file=/etc/my.cnf--user=root --password=123123 --apply-log /opt/20210831_00
InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
InnoDB: New log files created, LSN=62456861898
InnoDB: Highest supported file format is Barracuda.
InnoDB: Log scan progressed past the checkpoint lsn 62456862220
InnoDB: Doing recovery: scanned up to log sequence number 62456862229 (0%)
InnoDB: Database was not shutdown normally!
InnoDB: Starting crash recovery.
InnoDB: xtrabackup: Last MySQL binlog file position 338527434, file name mysql-bin.000153
InnoDB: Removed temporary tablespace data file: "ibtmp1"
InnoDB: Creating shared tablespace for temporary tables
InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full;
Please wait ...
InnoDB: File './ibtmp1' size is now 12 MB.
InnoDB: 96 redo rollback segment(s) found. 1 redo rollback segment(s) are active.
InnoDB: 32 non-redo rollback segment(s) are active.
InnoDB: page_cleaner: 1000ms intended loop took 15111ms. The settings might not be optimal. (flushed=0 and evicted=0, during the time.)
InnoDB: 5.7.32 started;
log sequence number 62456862229
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed;
log sequence number 62456862248
210831 14:37:52 completed OK!
出现 completed OK!才表示成功解压完成
恢复数据文件
[root@localhost home]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=mopon123 --copy-back /opt/20210831_00
210831 14:55:39 [01]...done
210831 14:55:39 [01] Copying ./xtrabackup_master_key_id to /home/mysql/xtrabackup_master_key_id
210831 14:55:39 [01]...done
210831 14:55:39 [01] Copying ./ibtmp1 to /home/mysql/ibtmp1
210831 14:55:39 [01]...done
210831 14:55:39 completed OK!
completed OK! 结果为此提示表示恢复正确!!!
[root@localhost home]# chown -R mysql. mysql/
[root@localhost home]# /etc/init.d/mysqld start
Starting MySQL (Percona Server)............ SUCCESS!
开始同步主库
[root@localhost home]#cat /opt/20210831_00/xtrabackup_binlog_info
mysql-bin.000153338527434526b9441-6616-11eb-8737-005056b05ba7:1-77041081
[root@localhost home]#mysql -uroot -p123456
MySQL [(none)]> change master to master_host='192.168.0.1',master_user='slave',master_password='slave',master_log_file='mysql-bin.000153', master_log_pos=338527434;
Query OK, 0 rows affected, 2 warnings (0.78 sec)
MySQL [(none)]> start slave;
MySQL [(none)]> show slave status\G;
.....................
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
............................
到此为止数据库在Mstar不停机状态下同步完成!
【Mysql--日常维护-不停主master做主从同步】
文章图片
推荐阅读
- 被新冠“病毒”感染的日常生活(八)
- “葱辣嘴、蒜辣心、姜辣胃、辣椒辣两头”日常中的四辣功效|“葱辣嘴、蒜辣心、姜辣胃、辣椒辣两头”日常中的四辣功效 养生早知道
- 驾校小白的“日常”
- 不攀比不羡慕,也不失望
- 春季试衣间|春季试衣间|UNIQLO优衣库 UR HOTWIND热风春夏新品搭配 日常搭配 可盐可甜
- 亲子日常|亲子日常 D68
- 日常积累,水到渠成的必备要素
- 日常改PS
- 日常工作总结之UML图的运用
- 日常小故事