基础环境:
OS:Red Hat Enterprise Linux Server release 7.6 (Maipo)
mysql:Percona版(5.7.29最新,可选mysql8)
主从都装相同的版本,安装可参考前篇
https://blog.51cto.com/6464430/2479808
原版文档
https://www.percona.com/doc/percona-xtrabackup/2.1/howtos/setting_up_replication.html
主IP:192.168.100.1
备IP:192.168.100.2
操作步骤:
1、安装percona-xtrabackup工具最新版
# yum install percona-xtrabackup-24
2、创建备份目录
# mkdir /opt/mysql/mysql_backup
修改目录属组
# chown -R mysql:mysql /opt/mysql/mysql_backup
3、在主机上将数据库进行备份
# innobackupex --user = root --password = ‘******’ /opt/mysql/mysql_backup/
出现报错
Failed to connect to MySQL server: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2).
因为我们修改了默认的目录,因此需要修改下配置文件
加个参数--defaults-file=/etc/my.cnf
# innobackupex --defaults-file=/etc/my.cnf --user = root --password = ‘******’ /opt/mysql/mysql_backup/
又报一个错:
Failed to connect to MySQL server: DBI connect(';mysql_read_default_group=xtrabackup','root',...) failed: Access denied for user 'root'@'localhost' (using password: YES) at - line 1314.
需要制定本地地址,加参数 --host=127.0.0.1
# innobackupex --defaults-file=/etc/my.cnf --user = root --password = ‘******’ --host=127.0.0.1 /opt/mysql/mysql_backup/
执行完应该看到 completed OK!
4、为了使快照一致,再执行一个命令(同步数据 回滚未提交的事务及同步已经提交的事务至数据文件,使得数据文件处于一致性状态)
# innobackupex --defaults-file=/etc/my.cnf --user = root --password = ‘******’ --host=127.0.0.1 --apply-log /opt/mysql/mysql_backup / $ TIMESTAMP /
/ $ TIMESTAMP / 代表步骤3中的文件目录
5、将备份的数据复制到备机
# rsync -avpP -e ssh /opt/mysql/mysql_backup / $ TIMESTAMP 192.168.100.2:/opt/mysql/mysql_backup
6、停止备机的mysql
# systemctl stop mysqld.service
7、备份备机原有的datadir
# mv mysql_data/ mysql_data_bak
8、将步骤5同步过来的主机数据同步到datadir下并确保mysql有相关权限
# cp -rp /opt/mysql/mysql_backup /opt/mysql/mysql_data
# chown mysql:mysql -R /opt/mysql/mysql_data
9、配置主机(机器应该开启二进制同步及有唯一的服务id配置)
mysql>GRANT REPLICATION SLAVE ON *.* to 'repl'@'192.168.100.2' identified by '********';
有个小告警,可以先建用户repl就不会告警了
备机上执行
# mysql -urepl -p'*********' -host192.168.100.1
mysql>SHOW GRANTS;
可以看到授权
10、配置从机
拷贝主机的配置文件
# scp 192.168.100.1:/etc/my.cnf /etc/my.cnf
修改从机的server-id 为2
启动数据库
# systemctl start mysqld.service
11、开始复制
查看从机当前的日志
# cat /opt/mysql/mysql_dataxtrabackup_binlog_info
mybinlog.000008 194
从机执行
mysql>CHANGE MASTER TO MASTER_HOST = '192.168.100.1', MASTER_USER = 'REPL' , MASTER_PASSWORD = '********', MASTER_LOG_FILE = 'mybinlog.000008', MASTER_LOG_POS = 194;
启动备机:
mysql>START SLAVE;
12、检查
sql> SHOW SLAVE STATUS \G
...
Slave_IO_Running: Yes 应该为yes
Slave_SQL_Running: Yes 应该为yes
...
Seconds_Behind_Master: 13 表示当前同步的时间差
...
13、主从切换(正常情况下)
A、主库锁表,变为只读,避免日志同步不全
mysql> flush tables with read lock;
B、确保master所有的binlog已同步到slave,并且slave都已apply了所有的binlog
主:
mysql> show processlist;
备:
mysql> show processlist;
C、停止slave进程
从:
mysql> STOP SLAVE IO_THREAD;
mysql> show processlist;
D、确保从机开启bin_log且关闭read_only
mysql> show variables like 'read_only';
E、提示从机为主机:
mysql> stop slave;
mysql> reset master;
mysql> reset slave all;
mysql> show master status \G
F、新的主机需要建立同步帐户并赋予同步权限
mysql>GRANT REPLICATION SLAVE ON . to 'repl'@'192.168.100.1' identified by '****';
G、将原来的主切换从
mysql> reset master;
mysql>CHANGE MASTER TO MASTER_HOST='192.168.100.1',MASTER_USER='REPL',MASTER_PASSWORD='1qaz@WSX',MASTER_LOG_FILE='mybinlog.000001', MASTER_LOG_POS=154;
MASTER_LOG_FILE='mybinlog.000001', MASTER_LOG_POS=154 从步骤E获取
mysql> start slave;
mysql> show slave status\G