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= # 建议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. e8f74dde-ed8e-11e9-8ebb-000c29f5c092:-
执行CHANGE MASTER语句在MySQL命令行:
TheSlave|mysql> CHANGE MASTER TO
MASTER_HOST='192.168.0.106',
MASTER_USER='repl',
MASTER_PASSWORD='repl',
MASTER_AUTO_POSITION=;
开始复制:
TheSlave|mysql> START SLAVE;
6、检查
TheSlave|mysql> SHOW SLAVE STATUS \G
...
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...
Seconds_Behind_Master:
...
看到IO和SQL线程处于运行中 (Yes)表示复制正常。
root@localhost :: [(none)]> show slave status\G
ERROR (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:
Current database: *** NONE ****************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.106
Master_User: repl
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: localhost-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: e8f74dde-ed8e-11e9-8ebb-000c29f5c092
Master_Info_File: mysql.slave_master_info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: e8f74dde-ed8e-11e9-8ebb-000c29f5c092:
Executed_Gtid_Set: e8f74dde-ed8e-11e9-8ebb-000c29f5c092:-
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key:
Network_Namespace:
row in set (0.00 sec)