mysql:5.6.29
xtrabackup:2.2.10
master:192.168.166.129
slave:192.168.166.131
mysql数据目录:/data/mysql
mysql备份目录:/data/dbbak/full #确保有足够的磁盘空间
在master及slave机器安装xtrabackup软件
1、安装依赖
yum -y install libaio perl-Time-HiRes perl-DBD-MySQL perl-IO-Socket-SSL rsync.x86_64
2、安装xtrabackup
rpm -ivh percona-xtrabackup-2.2.10-1.el6.x86_64.rpm
在master机器操作
1、在数据库创建备份账号
mysql> CREATE USER ‘bkpuser‘@‘localhost‘ IDENTIFIED BY ‘s3cret‘;
mysql> GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT,Process ON *.* TO ‘bkpuser‘@‘localhost‘;
mysql> FLUSH PRIVILEGES;
2、备份主库
cd /data/dbbak
innobackupex --user=bkpuser --password=s3cret --parallel=4 /data/dbbak/full --no-timestamp --defaults-file=/etc/my.cnf
tar czvf full.tar.gz /data/dbbak/full --remove-files
scp full.tar.gz [email protected]:/data/dbbak/
在slave机器操作
1、停止mysql,删除或者重命名Mysql数据目录
/etc/init.d/mysql.server stop
rm -rf /data/mysql
2、应用日志及数据库还原
cd /data/dbbak/
innobackupex --apply-log /data/dbbak/full #应用日志
innobackupex --copy-back /data/dbbak/full #数据库还原
chown mysql:mysql /data/mysql -R #修改数据目录的宿主权限
/etc/init.d/mysql.server start #启动mysql
3、设置已执行过的gtid
[[email protected]_166_131 dbbak]# cat /data/mysql/xtrabackup_info |grep binlog_pos
binlog_pos = GTID of the last change ‘132028ab-abc5-11e6-b2f0-000c29a60c3d:1-45675‘
#查看slave已执行的gtid是否为空,为空,需要检查。备份还原是否有误。如果无误,需要执行reset MASTER;进行情况,否则无法设置gtid。
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 151
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
ERROR:
No query specified
mysql> SET @MYSQLDUMP_TEMP_LOG_BIN = @@SESSION.SQL_LOG_BIN;
mysql> SET @@SESSION.SQL_LOG_BIN= 0;
mysql> SET @@GLOBAL.GTID_PURGED=‘132028ab-abc5-11e6-b2f0-000c29a60c3d:1-45675‘;
mysql> SET @@SESSION.SQL_LOG_BIN = @MYSQLDUMP_TEMP_LOG_BIN;
mysql> start slave ;
mysql> show slave status;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.166.129
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000011
Read_Master_Log_Pos: 550555
Relay_Log_File: relay-bin.000004
Relay_Log_Pos: 862
Relay_Master_Log_File: mysql-bin.000011
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 550555
Relay_Log_Space: 1771
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 202181
Master_UUID: 132028ab-abc5-11e6-b2f0-000c29a60c3d
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 132028ab-abc5-11e6-b2f0-000c29a60c3d:45676-45680
Executed_Gtid_Set: 132028ab-abc5-11e6-b2f0-000c29a60c3d:1-45680
Auto_Position: 1
1 row in set (0.00 sec)