mysql服务器 | mysql版本 | mysql服务器ip |
---|---|---|
mysql1(master) | 5.6.50 | 10.206.120.2 |
mysql2(slave) | 5.6.50 | 10.206.120.10 |
1、安装xtrabackup
#安装Percona yum存储库
yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm -y
#如果打算将Percona XtraBackup与MySQL 结合使用,还需要开启tools存储库
percona-release enable-only tools release
#安装Percona XtraBackup 2.4版本 适用于mysql8.0以下版本
yum install -y percona-xtrabackup-24
#下载mysql-devel mysql-client mysql-server上传至服务器
#安装mysql
yum install -y MySQL-client-5.6.50-1.el7.x86_64.rpm
yum install -y MySQL-server-5.6.50-1.el7.x86_64.rpm
yum install -y MySQL-devel-5.6.50-1.el7.x86_64.rpm
#若报mysql与mariadb的lib冲突
yum remove -y mariadb-libs.x86_64
#主库执行
1.准备纯净的从库环境
主库:10.206.120.2
从库:10.206.120.10
2.修改主库配置文件
[root@VM-120-2-centos ~]# vim /usr/my.cnf
[mysqld]
# 节点ID,确保唯一
server-id = 1
#开启mysql的binlog日志功能
log-bin = mysql-bin
#控制数据库的binlog刷到磁盘上去 , 0 不控制,性能最好,1每次事物提交都会刷到日志文件中, 性能最差,最安全
sync_binlog = 1
#binlog日志格式
binlog_format = row
#binlog过期清理时间
expire_logs_days = 7
#binlog每个日志文件大小
max_binlog_size = 100m
#binlog缓存大小
binlog_cache_size = 4m
#最大binlog缓存大小
max_binlog_cache_size= 512m
#不生成日志文件的数据库,多个忽略数据库可以用逗号拼接,或者 复制黏贴下述配置项,写多行
binlog-ignore-db=mysql
# 表中自增字段每次的偏移量
auto-increment-offset = 1
# 表中自增字段每次的自增量
auto-increment-increment = 1
#跳过从库错误
slave-skip-errors = all
3.重启主库(略)
systemctl restart mysql
4.在主库上创建一个用于复制的账号,并赋予replication slave权限,这里必须 *.* 不能指定库授权,因为 replication slave 是全局的
mysql> CREATE USER 'slave'@'%' IDENTIFIED BY '123';
mysql> grant replication slave on *.* to 'slave'@'%' identified by '123';
mysql> flush privileges;
5.模拟主库运行,有数据写入、
for i in `seq 2 1000000`
do
mysql -uroot -p123 -e "insert test.test(id) values($i)";
sleep 1;
done
#创建备份目录
mkdir -p /data/xtrabackup/
6.主库全备数据
#全备数据
innobackupex --defaults-file=/usr/my.cnf --user=root --password=123 /data/xtrabackup
......
......
231229 18:01:12 [00] ...done
xtrabackup: Transaction log of lsn (8985658) to (8985658) was copied.
231229 18:01:13 completed OK!
# 备份一致性检查
innobackupex --apply-log /data/xtrabackup/备份当天的日期-时间/
........
........
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 9020064
240102 10:29:39 completed OK!
7.将热备数据传达从库并关闭数据库
#主库压缩刚刚备份的目录
cd /data/xtrabackup/
tar -czvf 2024-01-02_11-12-06.tar.gz 2024-01-02_11-12-06
scp 2024-01-02_11-12-06.tar.gz 10.206.120.10:/tmp.
#关闭数据库
systemctl stop mysql
8.修改从库配置文件
[root@VM-120-10-centos ~]# vim /usr/my.cnf
[mysqld]
server-id = 2
relay-log = mysql-relay-bin
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%
log-bin=mysql-bin
log-slave-updates
sync_binlog=0
replicate-do-db=testdb
slave-net-timeout=60
9.在从库导入全备数据
innobackupex --copy-back /tmp/2024-01-02_11-12-06
chown -R mysql.mysql /var/lib/mysql
10.重启从库
systemctl restart mysql
11.查看sql文件中的位置点(如果是打点备份的话)
该位置即主库刚刚做完全量备份时,主库binlog日志所处的位置
[root@VM-120-10-centos tmp]# cat /tmp/2024-01-02_11-12-06/xtrabackup_binlog_info
mysql-bin.000003 2769312
12.从库配置同步
[root@slave1 ~]# mysql -uroot -p123 # 登录然后执行
change master to master_host='10.206.120.2',
master_port=3306,
master_user='slave',
master_password='123',
master_log_file='mysql-bin.000003',
master_log_pos=2769312;
13.开启SQL线程和IO线程
start slave;
14.查看主从状态
show slave status\G;
.......
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
.......
至此,mysql全备(热备)完成,并将主从复制配置成功