xtrabackup是Percona公司CTO Vadim參與開發(fā)的一款基于InnoDB的在線熱備工具阁危,具有開源涉茧,免費(fèi),支持在線熱備商玫,備份恢復(fù)速度快箕憾,占用磁盤空間小等特點(diǎn),并且支持不同情況下的多種備份形式拳昌。xtrabackup的官方下載地址為http://www.percona.com/software/percona-xtrabackup袭异。
xtrabackup包含兩個(gè)主要的工具,即xtrabackup和innobackupex炬藤,二者區(qū)別如下:
(1)xtrabackup只能備份innodb和xtradb兩種引擎的表御铃,而不能備份myisam引擎的表碴里;
(2)innobackupex是一個(gè)封裝了xtrabackup的Perl腳本,支持同時(shí)備份innodb和myisam上真,但在對(duì)myisam備份時(shí)需要加一個(gè)全局的讀鎖咬腋。還有就是myisam不支持增量備份。
1.備份過程
innobackupex備份過程如下圖:
(圖1 innobackupex備份過程,本文中所有圖都是google所得)
在圖1中睡互,備份開始時(shí)首先會(huì)開啟一個(gè)后臺(tái)檢測(cè)進(jìn)程根竿,實(shí)時(shí)檢測(cè)mysql redo的變化,一旦發(fā)現(xiàn)redo中有新的日志寫入就珠,立刻將日志記入后臺(tái)日志文件xtrabackup_log中寇壳。之后復(fù)制innodb的數(shù)據(jù)文件和系統(tǒng)表空間文件ibdata1,待復(fù)制結(jié)束后嗓违,執(zhí)行flush tables with read lock操作九巡,復(fù)制.frm,MYI蹂季,MYD,等文件(執(zhí)行flush tableswith read lock的目的是為了防止數(shù)據(jù)表發(fā)生DDL操作疏日,并且在這一時(shí)刻獲得binlog的位置)最后會(huì)發(fā)出unlock tables偿洁,把表設(shè)置為可讀可寫狀態(tài),最終停止xtrabackup_log沟优。
2.全備恢復(fù)
這一階段會(huì)啟動(dòng)xtrabackup內(nèi)嵌的innodb實(shí)例涕滋,回放xtrabackup日志xtrabackup_log,將提交的事務(wù)信息變更應(yīng)用到innodb數(shù)據(jù)/表空間挠阁,同時(shí)回滾未提交的事務(wù)(這一過程類似innodb的實(shí)例恢復(fù))宾肺。恢復(fù)過程如下圖:
(圖2 innobackupex 恢復(fù)過程)
3.增量備份
innobackupex增量備份過程中的"增量"處理侵俗,其實(shí)主要是相對(duì)innodb而言锨用,對(duì)myisam和其他存儲(chǔ)引擎而言,它仍然是全拷貝(全備份)
"增量"備份的過程主要是通過拷貝innodb中有變更的"頁"(這些變更的數(shù)據(jù)頁指的是"頁"的LSN大于xtrabackup_checkpoints中給定的LSN)隘谣。增量備份是基于全備的增拥,第一次增備的數(shù)據(jù)必須要基于上一次的全備,之后的每次增備都是基于上一次的增備寻歧,最終達(dá)到一致性的增備掌栅。增量備份的過程如下,和全備的過程很類似码泛,區(qū)別僅在第2步猾封。
( 圖 3 innobackupex增量備份過程)
4.增量備份恢復(fù)
和全備恢復(fù)類似,也需要兩步噪珊,一是數(shù)據(jù)文件的恢復(fù)晌缘,如圖4逾苫,這里的數(shù)據(jù)來源由3部分組成:全備份,增量備份和xtrabackup log枚钓。二是對(duì)未提交事務(wù)的回滾铅搓,如圖5所示:
( 圖4 innobackupex 增量備份恢復(fù)過程1)
( 圖5 innobackupex增量備份恢復(fù)過程2)
5.innobackupex使用示例
(1)安裝使用xtrabackup,安裝比較簡單搀捷,我們使用二進(jìn)制編譯好的就行了星掰,這種工具無需源碼編譯,因?yàn)闆]有什么功能需要俺們定制嫩舟。
[root@MySQL-01~]#wgethttp://www.percona.com/redir/downloads/XtraBackup/LATEST/binary/Linux/x86_64/percona-xtrabackup-2.1.8-733-Linux-x86_64.tar.gz
[root@MySQL-01~]#tarxf percona-xtrabackup-2.1.8-733-Linux-x86_64.tar.gz -C /usr/local/[root@MySQL-01~]#mv/usr/local/percona-xtrabackup-2.1.8-Linux-x86_64/ /usr/local/xtrabackup
[root@MySQL-01~]#echo"export PATH=\$PATH:/usr/local/xtrabackup/bin">> /etc/profile
[root@MySQL-01~]# source /etc/profile
[root@MySQL-01~]#
(2)全量備份
創(chuàng)建備份用戶:
mysql>createuser'backup'@'%'identifiedby'yayun';
Query OK,0rows affected (0.01sec)
mysql>grantreload,lock tables,replicationclient,createtablespace,superon*.*to'backup'@'%';
Query OK,0rows affected (0.00sec)
mysql>
進(jìn)行全備份
備份數(shù)據(jù)存放在/data/backup/下面氢烘,innobackupex會(huì)自動(dòng)創(chuàng)建一個(gè)文件夾,是當(dāng)前系統(tǒng)的時(shí)間戳
mysql>select*fromyayun.t1;+------+-------+|id|name|+------+-------+|1|yayun||2|atlas|+------+-------+2rowsinset(0.00sec)
mysql>
測(cè)試數(shù)據(jù)就是yayun庫中的t1表 (錯(cuò)誤:--host=192.168.199.1
[root@MySQL-01 ~]# innobackupex--user=backup --password=yayun --socket=/tmp/mysqld.sock --defaults-file=/etc/my.cnf /data/backup/xtrabackup: Creating suspendfile'/data/backup/2014-04-07_23-05-04/xtrabackup_log_copied'withpid'57608'xtrabackup:Transactionlogoflsn (5324782783)to(5324782783) was copied.14040723:06:14innobackupex:Alltables unlocked
innobackupex:Backupcreatedindirectory'/data/backup/2014-04-07_23-05-04'innobackupex: MySQL binlog position: filename'mysql-bin.000014', position298314040723:06:14innobackupex: Connectiontodatabaseserver closed14040723:06:14innobackupex: completed OK![root@MySQL-01 ~]#
上面的過程中處理過家厌,主要看最后是否提示innobackupex completed ok播玖,可以看見備份成功。我們看看/data/backup目錄下產(chǎn)生了什么
[root@MySQL-01backup]#pwd/data/backup
[root@MySQL-01backup]# ll
total4drwxr-xr-x12root root4096Apr723:062014-04-07_23-05-04[root@MySQL-01backup]# cd2014-04-07_23-05-04/[root@MySQL-012014-04-07_23-05-04]# ll
total845888-rw-r--r--1root root261Apr723:05backup-my.cnf
drwx------2root root4096Apr723:06employees
drwx------2root root4096Apr723:06host-rw-r-----1root root866123776Apr723:05ibdata1
drwx------2root root4096Apr723:06menagerie
drwxr-xr-x2root root4096Apr723:06mysql
drwxr-xr-x2root root4096Apr723:06performance_schema
drwx------2root root4096Apr723:06sakila
drwx------2root root4096Apr723:06test
drwx------2root root4096Apr723:06world_innodb
drwxr-xr-x2root root4096Apr723:06world_myisam-rw-r--r--1root root13Apr723:06xtrabackup_binary-rw-r--r--1root root24Apr723:06xtrabackup_binlog_info-rw-r-----1root root95Apr723:06xtrabackup_checkpoints-rw-r-----1root root2560Apr723:06xtrabackup_logfile
drwx------2root root4096Apr723:06yayun
[root@MySQL-012014-04-07_23-05-04]#
可以看見有對(duì)應(yīng)數(shù)據(jù)庫的名字饭于,比如yayun蜀踏,還有一個(gè)以時(shí)間戳命名的目錄。我們看看對(duì)應(yīng)文件里面的內(nèi)容掰吕,這幾個(gè)比較重要
[root@MySQL-012014-04-07_23-05-04]#catxtrabackup_checkpoints
backup_type= full-backuped
from_lsn=0to_lsn=5324782783last_lsn=5324782783compact=0[root@MySQL-012014-04-07_23-05-04]#catxtrabackup_binlog_info
mysql-bin.0000142983[root@MySQL-012014-04-07_23-05-04]#
可以看見相關(guān)文件記錄了LSN,日志偏移量果覆,還可以看見這次是全備份,相信聰明的童鞋們一眼就看懂了殖熟。^_^
刪除數(shù)據(jù)庫局待,然后恢復(fù)全備(線上不要這樣搞)
mysql>dropdatabaseyayun;
Query OK,1row affected (0.04sec)
mysql>
恢復(fù)全備
恢復(fù)備份到mysql的數(shù)據(jù)文件目錄,這一過程要先關(guān)閉mysql數(shù)據(jù)庫菱属,重命名或者刪除原數(shù)據(jù)文件目錄都可以钳榨,再創(chuàng)建一個(gè)新的數(shù)據(jù)文件目錄,將備份數(shù)據(jù)復(fù)制到新的數(shù)據(jù)文件目錄下纽门,賦權(quán)薛耻,修改權(quán)限,啟動(dòng)數(shù)據(jù)庫
[root@MySQL-01~]# /etc/init.d/mysqld stop
Shutting down MySQL.....? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [? OK? ]
[root@MySQL-01~]#mv/data/mysql /data/mysql_bak
[root@MySQL-01~]#mkdir/data/mysql
[root@MySQL-01~]#
[root@MySQL-01~]# innobackupex --apply-log /data/backup/2014-04-07_23-05-04/xtrabackup: starting shutdown with innodb_fast_shutdown=114040723:22:36InnoDB: Starting shutdown...14040723:22:40InnoDB: Shutdown completed; log sequence number532478414014040723:22:40innobackupex: completed OK!
以上對(duì)應(yīng)的目錄就是innobackupex全備份自己創(chuàng)建的目錄膜毁。
[root@MySQL-01~]# innobackupex --defaults-file=/etc/my.cnf --copy-back --rsync /data/backup/2014-04-07_23-05-04/innobackupex: Starting to copy InnoDB log files
innobackupex:in'/data/backup/2014-04-07_23-05-04'innobackupex: back to original InnoDB log directory'/data/mysql'innobackupex: Copying'/data/backup/2014-04-07_23-05-04/ib_logfile1'to'/data/mysql/ib_logfile1'innobackupex: Copying'/data/backup/2014-04-07_23-05-04/ib_logfile0'to'/data/mysql/ib_logfile0'innobackupex: Finished copying back files.14040723:27:38innobackupex: completed OK![root@MySQL-01~]#
可以看見已經(jīng)成功恢復(fù)昭卓,修改數(shù)據(jù)目錄權(quán)限,啟動(dòng)mysql瘟滨,效驗(yàn)數(shù)據(jù)是否正常候醒,查看yayun庫下面的t1表中的數(shù)據(jù)。
[root@MySQL-01~]#chown-R mysql.mysql /data/mysql
[root@MySQL-01~]# /etc/init.d/mysqld start
Starting MySQL.................? ? ? ? ? ? ? ? ? ? ? ? ? ? [? OK? ]
[root@MySQL-01~]#
mysql>useyayun
Readingtableinformationforcompletionoftableandcolumnnames
You can turnoffthis featuretoget a quicker startupwith-ADatabasechanged
mysql>select*fromt1;+------+-------+|id|name|+------+-------+|1|yayun||2|atlas|+------+-------+2rowsinset(0.00sec)
mysql>
發(fā)現(xiàn)數(shù)據(jù)已經(jīng)成功恢復(fù)杂瘸。
(3)增量備份
在進(jìn)行增量備份時(shí)倒淫,首先要進(jìn)行一次全量備份,第一次增量備份是基于全備的败玉,之后的增量備份是基于上一次的增量備份敌土,以此類推镜硕。
全備份放在/data/backup/full,增量備份放在/data/backup/incremental
[root@MySQL-01~]# tree /data/backup//data/backup/├── full
└── incremental2directories,0files
[root@MySQL-01~]#
廢話少說,咱們先來一次全備份
[root@MySQL-01~]# innobackupex --user=backup --password=yayun --socket=/tmp/mysqld.sock --defaults-file=/etc/my.cnf /data/backup/full/innobackupex: Backup createdindirectory'/data/backup/full/2014-04-07_23-37-20'innobackupex: MySQL binlog position: filename'mysql-bin.000001', position10714040723:38:29innobackupex: Connection to database server closed14040723:38:29innobackupex: completed OK![root@MySQL-01~]#
為了測(cè)試效果返干,我們?cè)趖1表中插入數(shù)據(jù)
mysql>select*fromt1;+------+-------+|id|name|+------+-------+|1|yayun||2|atlas|+------+-------+2rowsinset(0.00sec)
mysql>insertintot1select1,'love sql';
Query OK,1row affected (0.01sec)
Records:1Duplicates:0Warnings:0mysql>select*fromt1;+------+----------+|id|name|+------+----------+|1|yayun||2|atlas||1|love sql|+------+----------+3rowsinset(0.00sec)
mysql>
現(xiàn)在來一次增量備份1
[root@MySQL-01~]# innobackupex --user=backup --password=yayun --socket=/tmp/mysqld.sock --defaults-file=/etc/my.cnf --incremental /data/backup/incremental/ --incremental-basedir=/data/backup/full/2014-04-07_23-37-20/ --parallel=2innobackupex: Backup createdindirectory'/data/backup/incremental/2014-04-07_23-42-46'innobackupex: MySQL binlog position: filename'mysql-bin.000001', position30114040723:43:25innobackupex: Connection to database server closed14040723:43:25innobackupex: completed OK![root@MySQL-01~]#
我們看看增量備份的大小以及文件內(nèi)容
[root@MySQL-01~]#du-sh/data/backup/full/2014-04-07_23-37-20/1.2G? ? /data/backup/full/2014-04-07_23-37-20/[root@MySQL-01~]#du-sh/data/backup/incremental/2014-04-07_23-42-46/3.6M/data/backup/incremental/2014-04-07_23-42-46/[root@MySQL-01~]#
看見增量備份的數(shù)據(jù)很小吧兴枯,就是備份改變的數(shù)據(jù)而已。
[root@MySQL-012014-04-07_23-42-46]#pwd/data/backup/incremental/2014-04-07_23-42-46[root@MySQL-012014-04-07_23-42-46]#catxtrabackup_checkpointsbackup_type=incrementalfrom_lsn=5324784718to_lsn=5324785066last_lsn=5324785066compact=0[root@MySQL-012014-04-07_23-42-46]#
上面已經(jīng)明顯說明是增量備份了矩欠,該工具很人性化吧财剖,呵呵
我們?cè)俅蜗騮1表插入數(shù)據(jù),然后創(chuàng)建增量備份2
mysql>select*fromt1;+------+----------+|id|name|+------+----------+|1|yayun||2|atlas||1|love sql|+------+----------+3rowsinset(0.00sec)
mysql>insertintot1select1,'mysql dba';
Query OK,1row affected (0.00sec)
Records:1Duplicates:0Warnings:0mysql>select*fromt1;+------+-----------+|id|name|+------+-----------+|1|yayun||2|atlas||1|love sql||1|mysql dba|+------+-----------+4rowsinset(0.00sec)
mysql>
創(chuàng)建增量備份2(這次是基于上次的增量備份哦)
[root@MySQL-01 ~]# innobackupex--user=backup --password=yayun --socket=/tmp/mysqld.sock --defaults-file=/etc/my.cnf --incremental /data/backup/incremental/ --incremental-basedir=/data/backup/incremental/2014-04-07_23-42-46/ --parallel=2innobackupex:Backupcreatedindirectory'/data/backup/incremental/2014-04-07_23-51-15'innobackupex: MySQL binlog position: filename'mysql-bin.000001', position49614040723:51:55innobackupex: Connectiontodatabaseserver closed14040723:51:55innobackupex: completed OK!
[root@MySQL-01 ~]#
[root@MySQL-01~]#ls-ltr /data/backup/full/total4drwxr-xr-x12root root4096Apr723:382014-04-07_23-37-20[root@MySQL-01~]#ls-ltr /data/backup/incremental/total8drwxr-xr-x12root root4096Apr723:432014-04-07_23-42-46drwxr-xr-x12root root4096Apr723:512014-04-07_23-51-15[root@MySQL-01~]#
(4)增量備份恢復(fù)
增量備份的恢復(fù)大體為3個(gè)步驟
*恢復(fù)完全備份
*恢復(fù)增量備份到完全備份(開始恢復(fù)的增量備份要添加--redo-only參數(shù)癌淮,到最后一次增量備份去掉--redo-only參數(shù))
*對(duì)整體的完全備份進(jìn)行恢復(fù)躺坟,回滾那些未提交的數(shù)據(jù)
恢復(fù)完全備份(注意這里一定要加--redo-only參數(shù),該參數(shù)的意思是只應(yīng)用xtrabackup日志中已提交的事務(wù)數(shù)據(jù)乳蓄,不回滾還未提交的數(shù)據(jù))
[root@MySQL-01~]# innobackupex --apply-log --redo-only /data/backup/full/2014-04-07_23-37-20/xtrabackup: starting shutdown with innodb_fast_shutdown=114040723:59:43InnoDB: Starting shutdown...14040723:59:43InnoDB: Shutdown completed; log sequence number532478471814040723:59:43innobackupex: completed OK!
將增量備份1應(yīng)用到完全備份
[root@MySQL-01~]# innobackupex --apply-log --redo-only /data/backup/full/2014-04-07_23-37-20/ --incremental-dir=/data/backup/incremental/2014-04-07_23-42-46/innobackupex: Copying'/data/backup/incremental/2014-04-07_23-42-46/mysql/func.frm'to'/data/backup/full/2014-04-07_23-37-20/mysql/func.frm'innobackupex: Copying'/data/backup/incremental/2014-04-07_23-42-46/mysql/help_relation.frm'to'/data/backup/full/2014-04-07_23-37-20/mysql/help_relation.frm'innobackupex: Copying'/data/backup/incremental/2014-04-07_23-42-46/mysql/help_category.MYD'to'/data/backup/full/2014-04-07_23-37-20/mysql/help_category.MYD'innobackupex: Copying'/data/backup/incremental/2014-04-07_23-42-46/mysql/ndb_binlog_index.frm'to'/data/backup/full/2014-04-07_23-37-20/mysql/ndb_binlog_index.frm'14040800:02:07innobackupex: completed OK![root@MySQL-01~]#
將增量備份2應(yīng)用到完全備份(注意恢復(fù)最后一個(gè)增量備份時(shí)需要去掉--redo-only參數(shù)咪橙,回滾xtrabackup日志中那些還未提交的數(shù)據(jù))
[root@MySQL-01~]# innobackupex --apply-log /data/backup/full/2014-04-07_23-37-20/ --incremental-dir=/data/backup/incremental/2014-04-07_23-51-15/innobackupex: Copying'/data/backup/incremental/2014-04-07_23-51-15/mysql/help_relation.frm'to'/data/backup/full/2014-04-07_23-37-20/mysql/help_relation.frm'innobackupex: Copying'/data/backup/incremental/2014-04-07_23-51-15/mysql/help_category.MYD'to'/data/backup/full/2014-04-07_23-37-20/mysql/help_category.MYD'innobackupex: Copying'/data/backup/incremental/2014-04-07_23-51-15/mysql/ndb_binlog_index.frm'to'/data/backup/full/2014-04-07_23-37-20/mysql/ndb_binlog_index.frm'14040800:04:33innobackupex: completed OK![root@MySQL-01~]#
把所有合在一起的完全備份整體進(jìn)行一次apply操作,回滾未提交的數(shù)據(jù):
[root@MySQL-01~]# innobackupex --apply-log /data/backup/full/2014-04-07_23-37-20/xtrabackup: starting shutdown with innodb_fast_shutdown=11404080:06:32InnoDB: Starting shutdown...1404080:06:36InnoDB: Shutdown completed; log sequence number532478567614040800:06:36innobackupex: completed OK!
把恢復(fù)完的備份復(fù)制到數(shù)據(jù)庫目錄文件中虚倒,賦權(quán)美侦,然后啟動(dòng)mysql數(shù)據(jù)庫,檢測(cè)數(shù)據(jù)正確性
[root@MySQL-01~]# /etc/init.d/mysqld stop
Shutting down MySQL.? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [? OK? ]
[root@MySQL-01~]#mv/data/mysql /data/mysql_bak
[root@MySQL-01~]#mkdir/data/mysql
[root@MySQL-01~]# innobackupex --defaults-file=/etc/my.cnf --copy-back --rsync /data/backup/full/2014-04-07_23-37-20/innobackupex: Starting to copy InnoDB log files
innobackupex:in'/data/backup/full/2014-04-07_23-37-20'innobackupex: back to original InnoDB log directory'/data/mysql'innobackupex: Copying'/data/backup/full/2014-04-07_23-37-20/ib_logfile1'to'/data/mysql/ib_logfile1'innobackupex: Copying'/data/backup/full/2014-04-07_23-37-20/ib_logfile0'to'/data/mysql/ib_logfile0'innobackupex: Finished copying back files.14040800:12:42innobackupex: completed OK![root@MySQL-01~]#chown-R mysql.mysql /data/mysql
[root@MySQL-01~]# /etc/init.d/mysqld start
Starting MySQL....? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [? OK? ]
[root@MySQL-01~]#
查看數(shù)據(jù)是否正確
mysql>select*fromt1;+------+-----------+|id|name|+------+-----------+|1|yayun||2|atlas||1|love sql||1|mysql dba|+------+-----------+4rowsinset(0.00sec)
mysql>
(5)克隆slave
在日常工作中裹刮,我們有時(shí)候需要在線添加從庫音榜,比如線上有一主一從兩個(gè)數(shù)據(jù)庫,但是由于業(yè)務(wù)的需要捧弃,一臺(tái)從庫的讀取無法滿足現(xiàn)在的需求,這樣就需要我們?cè)诰€添加從庫擦囊,由于出于安全考慮违霞,我們通常需要在從庫上進(jìn)行在線克隆slave。
克隆slave時(shí)瞬场,常用參數(shù)--slave-info和--safe-slave-backup买鸽。
--slave-info會(huì)將master的binlog文件名和偏移量位置保存到xtrabackup_slave_info文件中
--safe-slave-backup會(huì)暫停slave的SQL線程直到?jīng)]有打開的臨時(shí)表的時(shí)候開始備份。備份結(jié)束后SQL線程會(huì)自動(dòng)啟動(dòng)贯被,這樣操作的目的主要是確保一致性的復(fù)制狀態(tài)眼五。
下面的例子,將介紹一主一從情況下在線搭建新的從庫彤灶,環(huán)境如下:
master 192.168.0.10 ? ?#主庫
slave ? ?192.168.0.20 ? ?#從庫
newslave 192.168.0.100 # 新的從庫
在上述示例中看幼,newslave即為要新搭建的從庫。在老的從庫上面進(jìn)行備份:
[root@MySQL-02~]# innobackupex --user=root --password=12345--socket=/tmp/mysqld.sock --defaults-file=/etc/my.cnf --slave-info--safe-slave-backup --no-timestamp /data/cloneslave
innobackupex: Backup createdindirectory'/data/cloneslave'innobackupex: MySQL binlog position: filename'mysql-bin.000022', position107innobackupex: MySQL slave binlog position: master host'192.168.0.10', filename'mysql-bin.000006', position73214041323:25:13innobackupex: completed OK!
這里的/data/cloneslave 目錄要不存在幌陕,如果存在是會(huì)報(bào)錯(cuò)的诵姜。
查看目錄下生成的文件:
[root@MySQL-02~]# ll /data/cloneslave/total26668-rw-r--r--1root root261Apr1323:24backup-my.cnf-rw-r--r--1root root27262976Apr1323:24ibdata1
drwxr-xr-x2root root4096Apr1323:25mysql
drwxr-xr-x2root root4096Apr1323:25performance_schema
drwxr-xr-x2root root4096Apr1323:25sakila
drwxr-xr-x2root root4096Apr1323:25world_innodb-rw-r--r--1root root13Apr1323:25xtrabackup_binary-rw-r--r--1root root23Apr1323:25xtrabackup_binlog_info-rw-r--r--1root root79Apr1323:25xtrabackup_checkpoints-rw-r--r--1root root2560Apr1323:25xtrabackup_logfile-rw-r--r--1root root72Apr1323:25xtrabackup_slave_info
drwxr-xr-x2root root4096Apr1323:25yayun
[root@MySQL-02~]#
查看xtrabackup_slave_info文件內(nèi)容,這個(gè)內(nèi)容就是為搭建從庫時(shí)需要change master to的參數(shù):
[root@MySQL-02~]#cat/data/cloneslave/xtrabackup_slave_info
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000006', MASTER_LOG_POS=732[root@MySQL-02~]#
在老的slave服務(wù)器上進(jìn)行還原搏熄,即192.168.0.20
[root@MySQL-02~]# innobackupex --apply-log --redo-only /data/cloneslave/xtrabackup: starting shutdown with innodb_fast_shutdown=114041323:30:37InnoDB: Starting shutdown...14041323:30:37InnoDB: Shutdown completed; log sequence number1298104814041323:30:37innobackupex: completed OK![root@MySQL-02~]#
將還原的文件復(fù)制到新的從庫newslave棚唆,即192.168.0.100
[root@MySQL-02data]# rsync -avprP -essh/data/cloneslave/192.168.0.100:/data/mysql/
在主庫master上添加對(duì)新從庫newslave的授權(quán):
mysql>grantreplicationslaveon*.*to'repl'@'192.168.0.100'identifiedby'123456';
Query OK,0rows affected (0.00sec)
mysql>flushprivileges;
Query OK,0rows affected (0.02sec)
mysql>
拷貝老的從庫的配置文件到新的從庫newslave暇赤,并且修改server-id參數(shù),修改完畢后宵凌,啟動(dòng)新的從庫鞋囊;
[root@MySQL-02 data]# scp/etc/my.cnf192.168.0.100:/etc/root@192.168.0.100's password:
my.cnf? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 100% 4881? ? 4.8KB/s? 00:00
[root@MySQL-02 data]#
[root@newslave mysql]#egrep'log-slave|^server-id|skip_slave'/etc/my.cnf
server-id=3skip_slave_start
log-slave-updates=1[root@newslave mysql]#
[root@newslave mysql]#chown-R mysql.mysql .
[root@newslave mysql]#/etc/init.d/mysqld restart
Shutting down MySQL.? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [? OK? ]
Starting MySQL..? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [? OK? ]
[root@newslave mysql]#
查找老的從庫備份后生成的xtrabackup_slave_info文件,提取其中的master_log_file和master_log_pos信息瞎惫,然后在新的從庫上進(jìn)行change master to操作:
在新的從庫上進(jìn)行同步:
mysql>CHANGE MASTERTOMASTER_HOST='192.168.0.10',MASTER_USER='repl', MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000006', MASTER_LOG_POS=732;
Query OK,0rows affected (0.09sec)
mysql>
啟動(dòng)io線程和sql線程溜腐,并觀察復(fù)制是否正常:
mysql>start slave;
Query OK,0rows affected (0.00sec)
mysql>
mysql>show slave? status\G***************************1. row***************************Slave_IO_State: Waitingformastertosend event
Master_Host:192.168.0.10Master_User: repl
Master_Port:3306Connect_Retry:2Master_Log_File: mysql-bin.000006Read_Master_Log_Pos:1309Relay_Log_File: MySQL-02-relay-bin.000002Relay_Log_Pos:830Relay_Master_Log_File: mysql-bin.000006Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table: yayun.%Replicate_Wild_Ignore_Table:
Last_Errno:0Last_Error:
Skip_Counter:0Exec_Master_Log_Pos:1309Relay_Log_Space:989Until_Condition: None
Until_Log_File:
Until_Log_Pos:0Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:0Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:0Last_IO_Error:
Last_SQL_Errno:0Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:11rowinset(0.00sec)
mysql>
查看主庫景醇,發(fā)現(xiàn)已經(jīng)有兩個(gè)線程(Binlog Dump)
mysql>show processlist\G***************************1. row***************************Id:8User: slave
Host:192.168.0.20:44251db:NULLCommand: BinlogDumpTime:1088State: Master has sentallbinlogtoslave; waitingforbinlogtobe updated
Info:NULL***************************2. row***************************Id:9User: root
Host: localhost
db: yayun
Command: Query
Time:0State:NULLInfo: show processlist***************************3. row***************************Id:10User: repl
Host:192.168.0.100:45844db:NULLCommand: BinlogDumpTime:124State: Master has sentallbinlogtoslave; waitingforbinlogtobe updated
Info:NULL3rowsinset(0.00sec)
mysql>
正常工作泊业,到此在線克隆slave就結(jié)束啦。
參考:
http://www.cnblogs.com/gomysql/p/3650645.html
http://blog.csdn.net/jesseyoung/article/details/42046111
http://fengwan.blog.51cto.com/508652/1432347
http://www.percona.com/doc/percona-xtrabackup/2.1/
http://realtimedba.blogspot.com/2013/06/my-sqlxtra-backup.html
作者:Atlas
出處:Atlas的博客http://www.cnblogs.com/gomysql
您的支持是對(duì)博主最大的鼓勵(lì)掰盘,感謝您的認(rèn)真閱讀欠橘。本文版權(quán)歸作者所有矩肩,歡迎轉(zhuǎn)載,但請(qǐng)保留該聲明肃续。