1.备份主库数据(主库操作)
(1)安装innobackupex
# yum -y install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm # yum -y install percona-xtrabackup-24-2.4.10
(2)创造同步数据
就是将主库数据全量备份
# innobackupex --defaults-file=/etc/my.cnf --user=root --password='Dingkai.123' --socket=/var/lib/mysql/mysql.sock /opt/backup/mysql_full/ #生成备份文件
# 格式:innobackupex --defaults-file=mysql配置文件路径 --user=备份用户 --password=密码 --socket=socket文件路径 备份路径
# innobackupex --defaults-file=/etc/my.cnf --user=root --password='Dingkai.123' --socket=/var/lib/mysql/mysql.sock --apply-log /opt/backup/mysql_all/2018-04-19_15-56-44 #获得一致性数据镜像
# innobackupex --defaults-file=mysql配置文件路径 --user=备份用户 --password=密码 --socket=socket文件路径 --apply-log 备份路径/生成的日期目录3)
(3)打包备份数据
# cd /opt/backup # zip -r mysqlfull.zip mysql_full/
(4)创建从库同步账号
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'环境所属网段' IDENTIFIED BY 'Slave.000'; flush privileges;
2.操作从库
(1)上传主库备份数据包
# cd /opt/mysql/
# mv mysql_data mysql_data_bak
#cd /opt/upload #上传 mysqlfull.zip #unzip mysqlfull.zip #cp -a mysql_full/* /opt/mysql/mysql_data/
(2)查看binlog
cd /opt/mysql/mysql_data cat xtrabackup_binlog_info mysql-bin.000003 157008 f75afbfe-438d-11e8-b57c-42010a8c0004:1-1002
(3)配置主从(从库操作)
配置主从有两种方式:binlog/gtid,任选一种
binlog
########binlog######## CHANGE MASTER TO MASTER_HOST='10.140.0.4', MASTER_USER='repl', MASTER_PASSWORD='Slave.000', MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=157008; mysql> start slave; ########binlog########
gtid
########gtid########
设置需要跳过的GTID: set global gtid_purged='f75afbfe-438d-11e8-b57c-42010a8c0004:1-1002'; 如果提示 ERROR 1840 (HY000): @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty. 则需要在从库执行下reset master; CHANGE MASTER TO MASTER_HOST='10.140.0.4', MASTER_USER='repl', MASTER_PASSWORD='Slave.000', MASTER_AUTO_POSITION=1;