使用xt
rbackup克
隆从
库
在日常工作中,我们有时候需要在线添加从库,比如线上有一主一从两个数据库,但是由于业务的需要,一台从库的读取无法满足现在的需求,这样就需要我们新建一台从库 ,
克隆slave时,常用参数--slave-info。
--slave-info会将master的binlog文件名和偏移量位置保存到xtrabackup_slave_info文件中
下面的例子,将介绍一主一从情况下在线搭建新的从库,环境如下:
master 10.40.55.11 #主库
slave 10.40.55.36 #从库
newslave 10.40.195.11 # 新的从库
在上述示例中,newslave即为要新搭建的从库。
步骤1、
在老的从库上面进行备份:
55.36#innobackupex --defaults-file=/opt/bodaodao/mysql/my.cnf --no-timestamp --user=root --password='123456' --socket=/opt/bodaodao/mysql/mysql.sock --slave-info /backup/bh
innobackupex: MySQL binlog position: filename 'mysql-bin.000704', position 855838849
innobackupex: MySQL slave binlog position: master host '10.40.55.11', filename 'mysql-bin.000508', position 367627999
160902 07:23:07 innobackupex: Connection to database server closed
160902 07:23:07 innobackupex: completed OK!
查看xtrabackup_slave_info文件内容,这个内容就是为搭建从库时需要change master to的参数:
# tail xtrabackup_slave_info
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000508', MASTER_LOG_POS=367627999
步骤2、新从库
安装数据库
newslave 195.11 (已安装mysql数据库,过程略)
步骤3、新从库关闭数据库实例
newslave 195.11
mysqladmin -u -p -S down
步骤3、
清空数据目录
newslave 195.11
cd /databse/mysql_3306/data/
rm -rf *
步骤4、数据还原
方法一:
在新的slave服务器上进行还原,即192.168.195.11
innobackupex --apply-log --redo-only --user=root --password= --defaults-file=/opt/bodaodao/mysql/my.cnf /backup/bh/2016-09-02
将还原的文件复制到新的从库newslave
cp -r /backup/bh/2016-09-02/* /database/mysql_3306/data/
方法二:
#innobackupex --apply-log --redo-only --user=root --password= --defaults-file=/opt/bodaodao/mysql/my.cnf /backup/bh/2016-09-02
160902 17:18:57 innobackupex: completed OK!
#innobackupex --copy-back --user=root --password= --defaults-file=/opt/bodaodao/mysql/my.cnf /backup/bh/2016-09-02
步骤5、赋权
chown -R mysql.mysql /database/mysql_3306/data/
步骤6、
55.11 在主库master上添加对新从库newslave的授权:
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.40.195.11' IDENTIFIED BY 'repl';
Query OK, 0 rows affected (0.00 sec)
步骤7、
在新的从库上进行同步:
newslave195.11
mysql> change master to master_host='10.40.55.11',master_user='repl',master_password='repl',master_log_file='mysql-bin.000508',master_log_pos=367627999,Master_Port=3306;
步骤8、
启动io线程和sql线程,并观察复制是否正常:
mysql> start slave;
mysql> show slave status\G;
查看主库,发现已经有两个线程(Binlog Dump)
mysql> select id,user,host,db,command,time,state,info from information_schema.processlist where command <>'sleep' order by time;
+----------+-------------+--------------------+------+-------------+---------+------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------+
| id | user | host | db | command | time | state | info |
+----------+-------------+--------------------+------+-------------+---------+------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------+
| 33192167 | root | localhost | NULL | Query | 0 | executing | select id,user,host,db,command,time,state,info from information_schema.processlist where command <>'sleep' order by time |
| 31057470 | system user | | NULL | Connect | 0 | Slave has read all relay log; waiting for the slave I/O thread t | |
| 33195734 | repl | 10.40.195.11:27004 | NULL | Binlog Dump | 3634 | Master has sent all binlog to slave; waiting for binlog to be up | NULL |
| 31057469 | system user | | NULL | Connect | 3453749 | Waiting for master to send event | NULL |
| 4576736 | repl | 10.40.55.36:18842 | NULL | Binlog Dump | 6277958 | Master has sent all binlog to slave; waiting for binlog to be up | NULL |
+----------+-------------+--------------------+------+-------------+---------+------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------+
正常工作,到此在线克隆slave就结束啦。