转载自己放着看,以防忘记时又到处找
参数:
--defaults-file mysql配置文件所在目录
--apply-log 将binlog应用到数据文件
--copy-back 从备份目录拷贝数据
1。安装依赖软件包
yum -y install perl perl-devel libaio libaio-devel perl-DBI perl-DBD-MySQL perl-TermReadKey perl-Time-HiRes
2.备份数据文件
/usr/local/xtrabackup/bin/innobackupex --defaults-file=/usr/local/mysql/my.cnf --user=root --password=A2sW13 /ptbak/
3.将binlog 应用到数据文件,这是恢复数据前必须完成的步骤
/usr/local/xtrabackup/bin/innobackupex --defaults-file=/usr/local/mysql/my.cnf --user=root --password=A2sW13 --apply-log /ptbak/2016-08-09_03-49-02/
4.恢复数据,--copy-back 选项的作用是从备份目录拷贝数据,日志到my.cnf文件里规定的目录下
/usr/local/xtrabackup/bin/innobackupex --defaults-file=/etc/my.cnf --user=root --copy-back /root/2016-08-09_03-49-02/
5.修改数据目录权限
chown -R mysql:mysql /usr/local/mysql/data
6.删除ib_logfile文件,否则mysql启动报错
cd /usr/local/mysql/data
rm -rf ib_logfile*
启动mysql
service mysql start
7.查看备份的一致性信息
cat /root/2016-08-09_03-49-02/xtrabackup_binlog_info
mysql-bin.000969307486338
master_log_file='mysql-bin.000969',
master_log_pos=307486338;
8.设置启用主从复制
主库上建立复制用户和权限
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.80.12' identified by '123456';
在从库上启动复制进程
change master to
master_host='192.168.80.11',
master_port=3306,
master_user='rpel',
master_password='123456',
master_log_file='mysql-bin.000969',
master_log_pos=307486338;
>start slave;
案例:
+++++++++++++++++++++
(1)参数理解由M-S 搭建M-2S
--slave-info,会打印binary log的位置和master server名,并且以change master的方式写到xtrabackup_slave_info中。
--safe-slave-backup,为了保证复制状态的一致性,这个选项会关闭slave sql线程,等待直到SHOW STATUS 中的Slave_open_temp_tabls为了才启动备份。如果等待时间超过—safe-slave-backup-timeout就会报
错默认300秒。备份成功后 slave sql thread会自动启动。
(2)主-从,在从库上备份数据库,新建主库的一个从库
10.10.6.87 dg2 主
10.10.6.86 dg 从
10.10.6.90 dgt 从
主库的10.10.6.87 master的信息
mysql> show master status \G;
*************************** 1. row ***************************
File: mysql-bin.000025
Position: 1046
Binlog_Do_DB:
Binlog_Ignore_DB: mysql,information_schema
Executed_Gtid_Set:
1 row in set (0.00 sec)
10.10.6.86 从库的master信息:
mysql> show master status \G;
*************************** 1. row ***************************
File: mysql-bin.000019
Position: 680
Binlog_Do_DB:
Binlog_Ignore_DB: mysql,information_schema
Executed_Gtid_Set:
1 row in set (0.00 sec)
10.10.6.86 从库的信息:
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.10.6.87
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000025
Read_Master_Log_Pos: 1046
Relay_Log_File: mysql-bin.000021
Relay_Log_Pos: 1436
Relay_Master_Log_File: mysql-bin.000025
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql
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: 1522
Relay_Log_Space: 1637
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: 2
Master_UUID: 8f9e146f-0a18-11e7-810a-0050568833c8
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
备库上备份整个库
[root@dg backup]# innobackupex --defaults-file=/etc/my.cnf --user=backup --password='bc.123456' --socket=/var/lib/mysql/mysql.sock --slave-info --safe-slave-backup --no-timestamp /backup/full
备份源库上恢复日志
备份完成后,还不能用于恢复,一些未提交的事物需要恢复,需要恢复redo logo的数据,确保数据一致
[root@dg backup]# innobackupex --apply-log /backup/full
传输数据到新的备库
[root@dg backup]# scp -r full/ dgt:/backup/
(3)在新建的S 恢复机清理目录service mysqld stop
mv /var/lib/mysql /var/lib/mysql6
(4)拷贝数据文件到目录:innobackupex --defaults-file=/etc/my.cnf --copy-back --rsync /backup/full
[root@dgt mysql]# chown -R mysql.mysql /var/lib/mysql
[root@dgt lib]# service mysqld start
[root@dgt lib]# cd /backup/full
[root@dgt full]# cat xtrabackup_slave_info
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000025', MASTER_LOG_POS=1046
(5) 修改从库的检查点位置CHANGE MASTER TO master_host='10.10.6.87', master_port=3306, master_user='rep',master_password='bc.123456', master_log_file='mysql-bin.000025', master_log_pos=1046;
start slave;主库上的用户
GRANT REPLICATION SLAVE ON *.* TO 'rep'@'10.10.6.%' IDENTIFIED BY 'bc.123456';
(6)查看状态mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.10.6.87
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000025
Read_Master_Log_Pos: 1046
Relay_Log_File: mysql-bin.000003
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000025
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql,information_schema
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: 1046
Relay_Log_Space: 521
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: 2
Master_UUID: 8f9e146f-0a18-11e7-810a-0050568833c8
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
===================================
Xtrabackup的优点:
我个人觉的比较好的就是备份不需要锁表(innodb),备份MyISAM的表还是会锁的,还支持增量备份,总之这工具值得赞。
参考文档:
三、实施
1、环境:
2、主库上安装Xtrabackup
3、备份主库
ps:1、ll /data/xtrabackup/2014-06-04_10-20-55/查看,你发现xtrabackup会把你数据库datadir下的所有数据文件都复制过来还新增了5个xtrabackup_开头的文件,我们关心的是xtrabackup_binlog_info这个文件,因为这个文件里面记录了你做从库需要change到主库的binlog的位置和pos点
2、如果只想备份一个库,加“--include=navy”指定库名即可, “--databases=navy”不好使,被坑过。
4、从库安装mysql5.6
主库的mysql是RD编译安装的,路径/usr/local/mysql/,那么从库你还苦逼的再编译一次吗?不用了,直接把主库的安装目录scp过来就可以用,my.cnf也scp过来,日志和datadir目录的宿主一定要是mysql哦,之后就可以初始化启动库了。
PS:主库竟然没有设置server-id,这不是坑爹嘛,别急,这个参数是动态的,可以在线调整,set global server_id=1;就ok了
5、从库服务器把主库备份文件拉到data_dir下
6、启动库,搭建主从,
到此工作已经完成:
来源:https://blog.csdn.net/wll_1017/article/details/70213115
http://blog.51cto.com/navyaijm/1422229