您好,登錄后才能下訂單哦!
下文我給大家簡單講講關于安裝Xtrabackup實現備份MySQL詳細步驟,大家之前了解過相關類似主題內容嗎?感興趣的話就一起來看看這篇文章吧,相信看完安裝Xtrabackup實現備份MySQL詳細步驟對大家多少有點幫助吧。
一、安裝Xtrabackup
# wget --no-check-certificate http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm # rpm -ivh percona-release-0.1-4.noarch.rpm # yum list | grep percona # yum -y install percona-xtrabackup-24
二、安裝MySQL
1.安裝MySQL
# yum -y install http://repo.mysql.com//mysql57-community-release-el7-9.noarch.rpm # yum list |grep mysql-community # yum -y install mysql mysql-server mysql-devel
2.更改時間戳設置
# cat /var/log/mysqld.log |grep "timestamp" [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). # vi /etc/my.cnf explicit_defaults_for_timestamp=true ##顯示指定默認值為timestamp類型的字段
3.啟動MySQL
# systemctl start mysqld # systemctl status mysqld
4.配置MySQL密碼
# mysql Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
a.新版本MySQL安裝完成后會生成臨時的初始密碼
# grep 'temporary password' /var/log/mysqld.log [Note] A temporary password is generated for root@localhost: qhAnfco2o)HB
修改MySQL密碼
注意:MySQL 5.7默認安裝了密碼安全檢查插件(validate_password),默認密碼檢查策略要求密碼必須包含:大小寫字母、數字和特殊符號,并且長度不能少于8位。
MySQL官網MySQL 5.7密碼策略詳細說明:
http://dev.mysql.com/doc/refman/5.7/en/validate-password-options-variables.html#sysvar_validate_password_policy
b.修改密碼策略
# vi /etc/my.cnf validate_password_policy = LOW ##密碼長度不少于8位即可 # systemctl restart mysqld # mysql -u root -p mysql> set password for 'root'@'localhost'=password('12345678');
c.官方數據庫示例
# wget http://downloads.mysql.com/docs/sakila-db.tar.gz # tar -zxvf sakila-db.tar.gz sakila-db/ sakila-db/sakila-data.sql sakila-db/sakila-schema.sql sakila-db/sakila.mwb [root@localhost ~]# pwd /root # mysql -u root -p mysql> source /root/sakila-db/sakila-schema.sql ##還原數據庫結構 mysql> source /root/sakila-db/sakila-data.sql ##將數據寫入數據庫
三、innobackupex常用命令
--backup 默認選項
--defaults-file 指定要備份的mysql實例的my.cnf文件,必須為第一個選項
--port 端口
--socket 連接套字節的位置,默認為/var/lib/mysql/mysql.sock
--host 主機
--no-timestamp 指定了這個選項備份會直接備份在BACKUP-DIR,不再創建時間戳文件夾
--target-dir 指定了這個選項備份會直接備份在BACKUP-DIR,不再創建時間戳文件夾
--use-memory 指定備份所用內存大小,默認為100M,與--apply-log同用
--apply-log 從備份恢復
--apply-log-only 在恢復時,停止恢復進程不進行LSN,只使用log
--copy-back 復制備份文件
--incremental 建立增量備份
--incremental-basedir=DIRECTORY
指定一個全庫備份的目錄作為增量備份的基礎數據庫
--incremental-dir=DIRECTORY
指定增量備份與全庫備份合并建立一個新的全備目錄
--prepare 從backup恢復
--compress 壓縮選項
四、xtrabackup備份后的主要文件
(1)xtrabackup_checkpoints —— 備份類型(如完全或增量)、備份狀態(如是否已經為prepared狀態)和LSN(日志序列號)范圍信息;
每個InnoDB頁(通常為16k大小)都會包含一個日志序列號,即LSN。LSN是整個數據庫系統的系統版本號,每個頁面相關的LSN能夠表明此頁面最近是如何發生改變的。
(2)xtrabackup_binlog_info —— mysql云服務器當前正在使用的二進制日志文件及至備份這一刻為止二進制日志事件的位置。
(3)xtrabackup_binlog_pos_innodb —— 二進制日志文件及用于InnoDB或XtraDB表的二進制日志文件的當前position。
(4)xtrabackup_binary —— 備份中用到的xtrabackup的可執行文件。
(5)backup-my.cnf —— 備份命令用到的配置選項信息。
五、Innobackupex備份
1.創建全備
# innobackupex --defaults-file=/etc/my.cnf --user=root --password=12345678 /backup/ ...... completed OK!
2.應用全備日志
# innobackupex --apply-log /backup/2017-04-03_12-45-44/ ...... completed OK!
3.查看備份狀態
# cat /backup/2017-04-03_12-45-44/xtrabackup_checkpoints backup_type = full-prepared ##全備 from_lsn = 0 ##備份開始點 to_lsn = 9692219 ##備份結束點 last_lsn = 9692228 compact = 0 recover_binlog_info = 0
4.查看二進制日志事件信息
# cat /backup/2017-04-03_12-45-44/xtrabackup_info uuid = 694e5590-1828-11e7-81d2-000c291bd2a1 name = tool_name = innobackupex tool_command = --defaults-file=/etc/my.cnf --user=root --password=... /backup/ tool_version = 2.4.6 ibbackup_version = 2.4.6 server_version = 5.7.17 start_time = 2017-04-03 21:34:09 end_time = 2017-04-03 21:34:13 lock_time = 0 binlog_pos = innodb_from_lsn = 0 innodb_to_lsn = 9692219 partial = N incremental = N format = file compact = N compressed = N encrypted = N
5.進行全備恢復
a.刪除數據庫、停止并破壞MySQL
# mysql -u root -p mysql> show databases; mysql> drop database sakila; Query OK, 30 rows affected (0.59 sec) # systemctl stop mysqld # cp -R /var/lib/mysql /root # rm -rf /var/lib/mysql
b.恢復全備
# innobackupex --copy-back /backup/2017-04-03_21-34-08/ ...... completed OK! # chown -R mysql.mysql /var/lib/mysql # systemctl start mysqld # mysql -u root -p mysql> show databases;
注:如無法啟動SQL,可能是SELINUX的問題
# vim /etc/selinux/config SELINUX=disabled
六、Innobackupex增量備份
1.創建數據庫和表
# mysql -u root -p mysql> create database abc; mysql> use abc; mysql> create table plus (id int(10),name varchar(20),phone char(11),birth date); mysql> show tables; mysql> insert into plus values(1,'tom',12345678901,'2001-01-01'); mysql> insert into plus values(2,'jack',12345678911,'2011-11-11'); mysql> select * from plus; +------+------+-------------+------------+ | id | name | phone | birth | +------+------+-------------+------------+ | 1 | tom | 12345678901 | 2001-01-01 | | 2 | jack | 12345678911 | 2011-11-11 | +------+------+-------------+------------+ 2 rows in set (0.00 sec)
2.基于全備進行增量備份
# innobackupex --defaults-file=/etc/my.cnf --user=root --password=12345678 --incremental --incremental-basedir=/backup/2017-04-03_21-34-08/ /backup/001/ ...... completed OK! # cat /backup/001/2017-04-03_21-41-27/xtrabackup_checkpoints backup_type = incremental ##增量備份 from_lsn = 9692219 ##備份開始點 to_lsn = 9699700 ##備份結束點 last_lsn = 9699709 compact = 0 recover_binlog_info = 0
3.應用全備日志
# innobackupex --defaults-file=/etc/my.cnf --user=root --password=12345678 --apply-log-only /backup/2017-04-03_21-34-08/ ...... completed OK!
4.應用第一次增量備份日志
# innobackupex --defaults-file=/etc/my.cnf --user=root --password=12345678 --apply-log-only /backup/2017-04-03_21-34-08/ --incremental-dir=/backup/001/ ...... completed OK!
5.基于全備進行第一次增量備份恢復
# systemctl stop mysqld # rm -rf /var/lib/mysql # innobackupex --copy-back /backup/2017-04-03_21-34-08/2017-04-03_21-44-08/ ...... completed OK! # chown -R mysql.mysql /var/lib/mysql # systemctl start mysqld # mysql -u root -p mysql> show databases; mysql> use abc; mysql> select * from plus; +------+------+-------------+------------+ | id | name | phone | birth | +------+------+-------------+------------+ | 1 | tom | 12345678901 | 2001-01-01 | | 2 | jack | 12345678911 | 2011-11-11 | +------+------+-------------+------------+ 2 rows in set (0.00 sec)
6.基于第一次增量備份進行備份
a.向表中添加數據
# mysql -u root -p mysql> use abc; mysql> insert into plus values(3,'rose',12345678912,'2012-12-12'); mysql> insert into plus values(4,'jordan',12345678923,'2012-12-23');
b.應用第二次增量備份日志
# innobackupex --defaults-file=/etc/my.cnf --user=root --password=12345678 --apply-log-only /backup/2017-04-03_21-34-08/ --incremental-dir=/backup/002/ ...... completed OK!
c.查看備份狀態
# cat /backup/002/2017-04-03_21-48-54/xtrabackup_checkpoints backup_type = incremental from_lsn = 9699700 to_lsn = 9696137 last_lsn = 9696146 compact = 0 recover_binlog_info = 0
d.基于全備份和第一次增量備份,恢復第二次增量備份
# systemctl stop mysqld # rm -rf /var/lib/mysql # innobackupex --copy-back /backup/2017-04-03_21-34-08/2017-04-03_21-50-11/ ...... completed OK! # chown -R mysql.mysql /var/lib/mysql # systemctl start mysqld # mysql -u root -p mysql> show databases; mysql> use abc; mysql> select * from plus; +------+--------+-------------+------------+ | id | name | phone | birth | +------+--------+-------------+------------+ | 1 | tom | 12345678901 | 2001-01-01 | | 2 | jack | 12345678911 | 2011-11-11 | | 3 | rose | 12345678912 | 2012-12-12 | | 4 | jordan | 12345678923 | 2012-12-23 | +------+--------+-------------+------------+ 4 rows in set (0.00 sec)
七、Xtrabackup備份
1.創建全備
# xtrabackup --defaults-file=/etc/my.cnf --user=root --password=12345678 --backup --target-dir=/backup/full ...... completed OK!
2.應用全備日志
# xtrabackup --defaults-file=/etc/my.cnf --prepare --user=root --password=12345678 --apply-log-only --target-dir=/backup/full ...... completed OK!
3.查看備份狀態
# cat /backup/full/xtrabackup_checkpoints backup_type = log-applied from_lsn = 0 to_lsn = 9692712 last_lsn = 9692721 compact = 0 recover_binlog_info = 0
4.恢復備份
# systemctl stop mysqld # rm -rf /var/lib/mysql # cd /backup/full/ # rsync -rvt --exclude 'xtrabackup_checkpoints' --exclude 'xtrabackup_logfile' ./ /var/lib/mysql sent 151722380 bytes received 6476 bytes 15971458.53 bytes/sec total size is 151681109 speedup is 1.00 # chown -R mysql.mysql /var/lib/mysql # systemctl start mysqld # mysql -u root -p mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | abc | | mysql | | performance_schema | | sakila | | sys | +--------------------+ 6 rows in set (0.09 sec)
八、Xtrabackup增量備份
1.第一次增量備份
# mysql -u root -p mysql> create database ball; mysql> use ball; mysql> create table superstar (id int(5),name varchar(20),number int(2),city varchar(20),team varchar(10)); mysql> insert into superstar values(1,'Jordan',23,'Chicago','Bulls'); mysql> insert into superstar values(2,'Yao',11,'Houston','Rockets');
2.應用第一次增量備份日志
# xtrabackup --defaults-file=/etc/my.cnf --user=root --password=12345678 --backup --target-dir=/backup/inc1 --incremental-basedir=/backup/full ...... completed OK!
3.查看備份狀態
# cat /backup/inc1/xtrabackup_checkpoints backup_type = incremental from_lsn = 9692712 to_lsn = 9763373 last_lsn = 9763382 compact = 0 recover_binlog_info = 0
4.第二次增量備份
# mysql -u root -p mysql> use ball; mysql> insert into superstar values(3,'Russell',6,'Boston','Celtics'); mysql> insert into superstar values(4,'Pierce',34,'Boston','Celtics');
5.應用第二次增量備份日志
# xtrabackup --defaults-file=/etc/my.cnf --user=root --password=12345678 --backup --target-dir=/backup/inc2 --incremental-basedir=/backup/inc1/
6.查看備份狀態
# cat /backup/inc2/xtrabackup_checkpoints backup_type = incremental from_lsn = 9763373 to_lsn = 9766964 last_lsn = 9766973 compact = 0 recover_binlog_info = 0
7.準備第一次增量備份
# xtrabackup --defaults-file=/etc/my.cnf --user=root --password=12345678 --prepare --apply-log-only --target-dir=/backup/full --incremental-dir=/backup/inc1 ...... completed OK!
8.準備第二次增量備份
# xtrabackup --defaults-file=/etc/my.cnf --user=root --password=12345678 --prepare --target-dir=/backup/full --incremental-dir=/backup/inc2 ...... completed OK!
9.合并恢復備份
# systemctl stop mysqld # rm -rf /var/lib/mysql # xtrabackup --defaults-file=/etc/my.cnf --user=root --password=12345678 --copy-back --target-dir=/backup/full ...... completed OK! # chown -R mysql.mysql /var/lib/mysql # systemctl start mysqld # mysql -u root -p mysql> use ball; mysql> select * from superstar; +------+---------+--------+---------+---------+ | id | name | number | city | team | +------+---------+--------+---------+---------+ | 1 | Jordan | 23 | Chicago | Bulls | | 2 | Yao | 11 | Houston | Rockets | | 3 | Russell | 6 | Boston | Celtics | | 4 | Pierce | 34 | Boston | Celtics | +------+---------+--------+---------+---------+ 4 rows in set (0.00 sec)
大家覺得安裝Xtrabackup實現備份MySQL詳細步驟這篇文章怎么樣,是否有所收獲。如果想要了解更多相關,可以繼續關注我們的行業資訊板塊。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。