1 說明
簡介:本方法可在不停止主庫,不鎖表的情況下惶看,快速完成超大從庫的重建
環(huán)境:以下代碼的環(huán)境為Centos 6.x启泣,其他環(huán)境請自行查詢對應(yīng)的安裝包阵子。
原理:innobackupex可在不影響主庫正常運(yùn)行的情況下政基,進(jìn)行物理備份(mysqldump為邏輯備份贞铣,速度慢很多,對于巨額數(shù)據(jù)量備份簡直是災(zāi)難)沮明,配合壓縮神器pigz進(jìn)行傳輸辕坝,備份速度變得更快!
2 具體實(shí)例
2.1 軟件安裝
主從庫都需要安裝好相同的mysql版本及innobackupex荐健,pigz
2.1.1 導(dǎo)入文件
(1)安裝innobackupex時(shí)的依賴包:perl-DBD-MySQL-4.013-3.el6.x86_64.rpm, libev-4.03-3.el6.x86_64.rpm
(2)壓縮神器:pigz-2.3.4.tar.gz
2.1.2 安裝pigz
#wget http://zlib.net/pigz/pigz-2.3.4.tar.gz
tar -xvzf pigz-2.3.4.tar.gz
cd pigz-2.3.4
make
cd ..
mv pigz-2.3.4 /usr/local/
ln -s /usr/local/pigz-2.3.4/pigz /usr/bin/
ln -s /usr/local/pigz-2.3.4/unpigz /usr/bin/
2.1.3 安裝innobackupex
#wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.3/binary/redhat/6/x86_64/percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm
rpm -ivh perl-DBD-MySQL-4.013-3.el6.x86_64.rpm
rpm -ivh libev-4.03-3.el6.x86_64.rpm
rpm -ivh percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm
2.2 具體命令
說明:確保導(dǎo)出數(shù)據(jù)前酱畅,主庫已開啟二進(jìn)制日志
2.2.1 主庫導(dǎo)出并傳輸?shù)綇膸欤?/h3>2.2.1.1 從庫操作:
mv /home/mysql/data/ /home/mysql/data_bak/? #備份mysql原數(shù)據(jù)文件夾
mkdir /home/mysql/data/? #mysql的數(shù)據(jù)文件夾
chmod -R 777 /home/mysql/data/? #保證主庫對此文件夾(從庫)有權(quán)限琳袄,可忽略此命令
2.2.1.2 主庫操作
#如數(shù)據(jù)量大,建議在screen下執(zhí)行導(dǎo)出命令
yum -y install screen
screen -R mysql_backup
mkdir /home/mysql/backup
innobackupex --user=backup --password=*** --stream=xbstream --defaults-file=/usr/local/mysql/data/3306/my.cnf --socket=/usr/local/mysql/tmp/3306/mysql.sock --parallel=2 --tmpdir=/home/mysql/backup? /home/mysql/backup 2>>/home/mysql/backup/xbstreambackup.log | pigz -2 -p 6 -c | ssh -p 9922? mysql@10.32.59.207 "unpigz |xbstream -x -C /home/mysql/data/"
注:/home/mysql/data/是從庫的數(shù)據(jù)目錄纺酸,記得提前清空或重建
2.2.2 從庫恢復(fù)及啟動(dòng)
2.2.2.1 恢復(fù)從庫數(shù)據(jù)
#保證從庫mysql已經(jīng)停止窖逗,
yum -y install screen
screen -R mysql_restore
innobackupex? --defaults-file=/home/mysql/data/backup-my.cnf --parallel=2 --apply-log /home/mysql/data/? #從庫恢復(fù)數(shù)據(jù)
2.2.2.2 重建從庫的日志目錄,啟動(dòng)從庫餐蔬。
cp /home/mysql/data_bak/auto.cnf? /home/mysql/data/? #拷貝從庫本機(jī)原有的auto.cnf
cp 主庫的my.cnf? /etc/my.cnf? #拷貝主庫my.cnf到從庫滑负,并修改server_id
chown -R mysql.mysql /home/mysql/data
/etc/init.d/mysqld start
2.2.3 添加主備同步
2.2.3.1 主庫設(shè)置
echo "grant replication slave on *.* to 'repl'@"${db_slave_ip}" identified by 'zabbix_repl';" | mysql -uroot -p${MysqlPassword}
echo "flush privileges;" | mysql -uroot -p${MysqlPassword}
2.2.3.2 從庫設(shè)置
cat /home/mysql/data/xtrabackup_binlog_info #獲取到binlog和pos位置
update.001822? 387886382
#根據(jù)上步的結(jié)果,設(shè)置主備同步的開始點(diǎn)
echo "CHANGE MASTER TO
MASTER_HOST='10.44.66.19',
MASTER_USER='repl',
MASTER_PASSWORD='zabbix_repl',
MASTER_PORT=3306,
MASTER_LOG_FILE='update.001822',
MASTER_LOG_POS=387886382;" | mysql -uroot -p123321
echo "start slave;" | mysql -uroot -p123321
echo "show slave status\G;" | mysql -uroot -p123321 2>/dev/null | grep -E "Slave_IO_Running|Slave_SQL_Running"
2.3 參考:
http://blog.51yip.com/mysql/1650.html
http://blog.itpub.net/27099995/viewspace-1295099/
http://www.2cto.com/database/201504/390859.html
3 操作記錄
3.1 zabbix-db-slave從庫重建
主庫備份及傳輸?shù)綇膸?
time: 75分鐘
data: 358G(/home/mysql/data/)
eth0 speed: 180Mpbs
從庫恢復(fù)時(shí)間:27分鐘
3.2 xx管理系統(tǒng)
3.2.1 主從庫例子
# 在主庫和從庫的/etc/my.cnf添加
[mysqld]
server-id = 1 # 主數(shù)據(jù)庫端ID號用含,主從不同
log-bin = mysql-bin # 日志文件名
#/etc/init.d/mysqld restart
3.2.2 從庫例子:
rpm -ivh libev-4.03-3.el6.x86_64.rpm
rpm -ivh perl-DBD-MySQL-4.013-3.el6.x86_64.rpm
rpm -ivh percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm
#wget http://zlib.net/pigz/pigz-2.3.4.tar.gz
tar -xvzf pigz-2.3.4.tar.gz
cd pigz-2.3.4
make
cd ..
mv pigz-2.3.4 /usr/local/
ln -s /usr/local/pigz-2.3.4/pigz /usr/bin/
ln -s /usr/local/pigz-2.3.4/unpigz /usr/bin/
/etc/init.d/mysqld stop
mv /var/lib/mysql /var/lib/mysql_bak
mkdir /var/lib/mysql
chmod 777 /var/lib/mysql
useradd zengzp
passwd zengzp
# 請完成下一節(jié)(主庫例子)后,執(zhí)行以下從庫恢復(fù)數(shù)據(jù)
innobackupex --defaults-file=/var/lib/mysql/backup-my.cnf --parallel=2 --apply-log /var/lib/mysql
chown -R mysql.mysql /var/lib/mysql
cp 主庫的my.cnf? /etc/my.cnf? #拷貝主庫my.cnf到從庫帮匾,并修改server_id
/etc/init.d/mysqld start
cat /var/lib/mysql/xtrabackup_binlog_info
update.001822? 387886382
#根據(jù)上步的結(jié)果啄骇,設(shè)置主備同步的開始點(diǎn)
echo "CHANGE MASTER TO
MASTER_HOST='10.42.79.148',
MASTER_USER='repl',
MASTER_PASSWORD='mysql_repl',
MASTER_PORT=3306,
MASTER_LOG_FILE='update.001822',
MASTER_LOG_POS=387886382;" | mysql -uroot -p
echo "start slave;" | mysql -uroot -p
echo "show slave status\G;" | mysql -uroot -p 2>/dev/null | grep -E "Slave_IO_Running|Slave_SQL_Running"
3.2.3 主庫例子:
rpm -ivh libev-4.03-3.el6.x86_64.rpm
rpm -ivh perl-DBD-MySQL-4.013-3.el6.x86_64.rpm
rpm -ivh percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm
#wget http://zlib.net/pigz/pigz-2.3.4.tar.gz
tar -xvzf pigz-2.3.4.tar.gz
cd pigz-2.3.4
make
cd ..
mv pigz-2.3.4 /usr/local/
ln -s /usr/local/pigz-2.3.4/pigz /usr/bin/
ln -s /usr/local/pigz-2.3.4/unpigz /usr/bin/
cd /tmp
mkdir mysql_tmp
innobackupex --user=root --password='' --stream=xbstream --socket=/var/lib/mysql/mysql.sock --parallel=2 --tmpdir=/tmp/mysql_tmp? /tmp/mysql_tmp 2>>/tmp/mysql_tmp/xbstreambackup.log | pigz -2 -p 6 -c | ssh -p 9922? zengzp@10.42.71.219 "unpigz |xbstream -x -C /var/lib/mysql"
echo "grant replication slave on *.* to 'repl'@"10.42.71.219" identified by 'mysql_repl';" | mysql -uroot -p
echo "flush privileges;" | mysql -uroot -p
3.3 xx采購系統(tǒng)
說明:因?yàn)閿?shù)據(jù)量較小,使用原生的gzip替換pigz
3.3.1 主從庫例子
# 在主庫和從庫的/etc/my.cnf添加
[mysqld]
server-id = 1 # 主數(shù)據(jù)庫端ID號瘟斜,主從不同
log-bin = mysql-bin # 日志文件名
#/etc/init.d/mysqld restart
3.3.2 從庫例子:
rpm -ivh libev-4.03-3.el6.x86_64.rpm
rpm -ivh perl-DBD-MySQL-4.013-3.el6.x86_64.rpm
rpm -ivh percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm
/etc/init.d/mysqld stop
mv /home/mysql/data/ /home/mysql/data_bak/? #備份mysql原數(shù)據(jù)文件夾
mkdir /home/mysql/data/? #mysql的數(shù)據(jù)文件夾
chmod -R 777 /home/mysql/data/? #保證主庫對此文件夾(從庫)有權(quán)限缸夹,可忽略此命令
useradd zengzp
passwd zengzp
# 請完成下一節(jié)(主庫例子)后,執(zhí)行以下從庫恢復(fù)數(shù)據(jù)
innobackupex --defaults-file=/home/mysql/data/backup-my.cnf --parallel=2 --apply-log /home/mysql/data
chown -R mysql.mysql /home/mysql/data
cp 主庫的my.cnf? /etc/my.cnf? #拷貝主庫my.cnf到從庫螺句,并修改server_id
/etc/init.d/mysqld start
cat /home/mysql/data/xtrabackup_binlog_info
mysql-bin.000004? ? ? ? 23361038
#根據(jù)上步的結(jié)果虽惭,設(shè)置主備同步的開始點(diǎn)
echo "CHANGE MASTER TO
MASTER_HOST='11.2.88.3',
MASTER_USER='repl',
MASTER_PASSWORD='mysql_repl',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000004',
MASTER_LOG_POS=23361038;" | mysql -uroot -p
echo "start slave;" | mysql -uroot -p
echo "show slave status\G;" | mysql -uroot -p 2>/dev/null | grep -E "Slave_IO_Running|Slave_SQL_Running"
3.3.3 主庫例子:
rpm -ivh libev-4.03-3.el6.x86_64.rpm
rpm -ivh perl-DBD-MySQL-4.013-3.el6.x86_64.rpm
rpm -ivh percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm
cd /tmp
mkdir mysql_tmp
# 添加gzip壓縮,傳輸時(shí)間明顯減小為65s(5.1G)
innobackupex --user=root --password='' --stream=xbstream --socket=/home/mysql/tmp/mysql.sock --parallel=4 --tmpdir=/tmp/mysql_tmp? /tmp/mysql_tmp 2>>/tmp/mysql_tmp/xbstreambackup.log | gzip | ssh -p 9922? zengzp@11.4.88.37 "gzip -d | xbstream -x -C /home/mysql/data"
echo "grant replication slave on *.* to 'repl'@"11.4.88.37" identified by 'mysql_repl';" | mysql -uroot -p
echo "flush privileges;" | mysql -uroot -p
4 附
多線程gzip壓縮神器—pigz
特點(diǎn):
1) pigz默認(rèn)用法(默認(rèn)并發(fā)線程是邏輯cpu個(gè)數(shù))可比gzip快5.3倍蛇尚,CPU消耗則是gzip的8倍芽唇,壓縮比則相當(dāng);
2) 支持并行的gzip取劫。并發(fā)8線程對比4線程提升:41.2%匆笤,16線程對比8線程提升:27.9%,32線程對比16線程提升:3%谱邪;
3) 在對壓縮效率要求較高炮捧、但對短時(shí)間內(nèi)CPU消耗較高不受影響的場景,使用pigz非常合適惦银。
pig默認(rèn)用當(dāng)前邏輯cpu個(gè)數(shù)來并發(fā)壓縮咆课,無法檢測個(gè)數(shù)的話,則并發(fā)8個(gè)線程扯俱。
安裝pigz
wget http://zlib.net/pigz/pigz-2.3.3.tar.gz
tar -xvzf pigz-2.3.3.tar.gz
#如果提示不是gz格式书蚪,請嘗試? tar -xvf pigz-2.3.3.tar.gz
cd pigz-2.3.3.tar.gz
make
如果報(bào)錯(cuò) pigz.c:(.text.startup+0xca): undefined reference to `deflateEnd' gcc
請?jiān)诘诎诵?(CC) $(LDFLAGS) -o pigz $^ -lpthread -lm 后面添加-lz選項(xiàng),表示link libz
壓縮:
tar cvf - 目錄名 | pigz -9 -p 24 > file.tgz
pigz:用法-9是壓縮比率比較大蘸吓,-p是指定cpu的核數(shù)善炫。
解壓:
pigz -d file.tgz
tar -xf --format=posix file