MySQL复制从库建立-xtracebackup方式
Percona XtraBackup工具提供了一种在系统运行时执行MySQL数据热备份的方法。
Percona XtraBackup在事务系统上执行联机非阻塞,紧密压缩,高度安全的完整备份,因此在计划的维护时段内,应用程序仍保持完全可用。
环境准备:
master:192.168.0.106:3306
slave:192.168.0.105:3306
datadir: /data/mysql/mysql3306/data
cnfpath: /data/mysql/mysql3306/my3306.cnf
XtraBackup 安装,两台机器都安装:
下载地址:https://www.percona.com/downloads/Percona-XtraBackup-LATEST/
TheMaster$ wget https://www.percona.com/downloads/Percona-XtraBackup-LATEST/Percona-XtraBackup-8.0-7/binary/redhat/7/x86_64/percona-xtrabackup-80-8.0.7-1.el7.x86_64.rpm
TheMaster$ yum install -y percona-xtrabackup-80-8.0.7-1.el7.x86_64.rpm
1、在Master制作一个备份
TheMaster$ mkdir xtrabackup_backupfiles
TheMaster$ xtrabackup --defaults-file=/data/mysql/mysql3306/my3306.cnf -S /tmp/mysql3306.sock -uroot -p'' --backup --target-dir=./xtrabackup_backupfiles
...
completed OK!
为了让快照是一致的:需要prepare备份数据:
TheMaster$ xtrabackup -S /tmp/mysql3306.sock -uroot -p --prepare --target-dir=./xtrabackup_backupfiles/
...
completed OK!
undo log目录需要处理下,默认会解压到xtrabackup_backupfiles目录下,在my3306.cnf中定义在/data/mysql/mysql3306/data/undolog/下,所以:
TheMaster$ cd xtrabackup_backupfiles && mkdir undolog && mv undo_* undolog/
2、复制备份数据到Slave上
使用rsync或scp将数据从Master复制到Slave。如果直接将数据同步到slave的数据目录,建议在这之前先停止mysqld。
TheMaster$ rsync -avpP -e ssh ./xtrabackup_backupfiles/ 192.168.0.105:/data/mysql/mysql3306/data/
复制数据前,您可以备份原始或先前安装的MySQL datadir(注意:在移动mysqld的内容或将快照移动到其datadir之前,请确保mysqld已关闭。):
TheSlave$ mv /data/mysql/mysql3306/data /data/mysql/mysql3306/data_bak
复制数据后,确保MySQL有权限访问
TheSlave$ chown -R mysql:mysql /data/mysql/mysql3306/data
3、创建Master上配置复制账号
TheMaster|mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.%' IDENTIFIED BY 'repl';
测试连接
TheSlave$ mysql --host=192.168.0.106 --user=repl --password=repl
mysql> SHOW GRANTS;
4、配置Slave上的MySQL服务器
首先将my3306.cnf从TheMaster复制到TheSlave:
TheSlave$ scp root@192.168.0.106:/data/mysql/mysql3306/my3306.cnf /data/mysql/mysql3306/
TheSlave$ chown -R mysql:mysql /data/mysql/mysql3306/data/my3306.cnf
修改TheSlave上的my3306.cnf:
server-id=1053306 # 建议ip最后一位+端口号
启动TheSlave 上的 mysqld
TheSlave$ /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf &
观察error.log是否有错误
TheSlave$ more /data/mysql/mysql3306/data/error.log
5、启动复制
TheSlave$ cat /data/mysql/mysql3306/data/xtrabackup_binlog_info mysql-bin.000012 195 e8f74dde-ed8e-11e9-8ebb-000c29f5c092:1-3
执行CHANGE MASTER语句在MySQL命令行:
TheSlave|mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.106', MASTER_USER='repl', MASTER_PASSWORD='repl', MASTER_AUTO_POSITION=1;
开始复制:
TheSlave|mysql> START SLAVE;
6、检查
TheSlave|mysql> SHOW SLAVE STATUS \G ... Slave_IO_Running: Yes Slave_SQL_Running: Yes ... Seconds_Behind_Master: 0 ...
看到IO和SQL线程处于运行中 (Yes)表示复制正常。