理論介紹
數(shù)據(jù)備份的重要性
●在生產(chǎn)環(huán)境中,數(shù)據(jù)的安全性至關(guān)重要
●任何數(shù)據(jù)的丟失都可能產(chǎn)生嚴(yán)重的后果
●造成數(shù)據(jù)丟失的原因:
程序錯(cuò)誤 |
---|
認(rèn)為操作失誤 |
運(yùn)算錯(cuò)誤 |
磁盤故障 |
災(zāi)難等 |
[圖片上傳失敗...(image-799a75-1601017593330)]
數(shù)據(jù)庫備份的分類
●從物理與邏輯的角度,備份可分為:
物理備份:對數(shù)據(jù)庫操作系統(tǒng)的物理文件(如數(shù)據(jù)文件、日志文件等)的備份
物理備份方法: |
---|
冷備份(脫機(jī)備份)︰是在關(guān)閉數(shù)據(jù)庫的時(shí)候進(jìn)行的 |
熱備份(聯(lián)機(jī)備份)∶數(shù)據(jù)庫處于運(yùn)行狀態(tài),依賴于數(shù)據(jù)庫的日志文件 |
溫備份:數(shù)據(jù)庫鎖定表格(不可寫入但可讀)的狀態(tài)下進(jìn)行備份操作 |
●邏輯備份:對數(shù)據(jù)庫邏輯組件(如:表等數(shù)據(jù)庫對象)的備份
●從數(shù)據(jù)庫的備份策略角度,備份可分為
完全備份:每次對數(shù)據(jù)庫進(jìn)行完整的備份 |
---|
差異備份:備份自從上次完全備份之后被修改過的文件 |
增量備份:只有在上次完全備份或者增量備份后被修改的文件才會(huì)被備份 |
常見的備份方法
●物理冷備
備份時(shí)數(shù)據(jù)庫處于關(guān)閉狀態(tài),直接打包數(shù)據(jù)庫文件 |
---|
備份速度快,恢復(fù)時(shí)也是最簡單的 |
●專用備份工具mydump或mysqlhotcopy
mysqldump常用的邏輯備份工具 |
---|
mysqlhotcopy僅擁有備份MylSAM和ARCHIVE表 |
●啟用二進(jìn)制日志進(jìn)行增量備份
進(jìn)行增量備份,需要刷新二進(jìn)制日志 |
---|
●第三方工具備份
免費(fèi)的MySQL熱備份軟件Percona XtraBackup |
---|
MySQL完全備份
■是對整個(gè)數(shù)據(jù)庫、數(shù)據(jù)庫結(jié)構(gòu)和文件結(jié)構(gòu)的備份
■保存的是備份完成時(shí)刻的數(shù)據(jù)庫
■是差異備份與增量備份的基礎(chǔ)
優(yōu)點(diǎn): | 缺點(diǎn): |
---|---|
備份與恢復(fù)操作簡單方便 | 數(shù)據(jù)存在大量的重復(fù) |
占用大量的備份空間 | |
備份與恢復(fù)時(shí)間長 |
數(shù)據(jù)庫完全備份分類
■物理冷備份與恢復(fù):
關(guān)閉MySQL數(shù)據(jù)庫
使用tar命令直接打包數(shù)據(jù)庫文件夾
直接替換現(xiàn)有MySQL目錄即可
■mysqldump備份與恢復(fù)
MySQL自帶的備份工具,可方便實(shí)現(xiàn)對MySQL的備份
可以將指定的庫、表導(dǎo)出為SQL腳本
使用命令mysql導(dǎo)入備份的數(shù)據(jù)
數(shù)據(jù)庫備份與恢復(fù)
MySQL數(shù)據(jù)庫完全備份與恢復(fù)
(1)環(huán)境搭建
mysql> create database zz;
Query OK, 1 row affected (0.00 sec)
mysql> use auth;
ERROR 1049 (42000): Unknown database 'auth'
mysql> use zz;
Database changed
mysql> create table users (user_name CHAR(16) NOT NULL, user_passwd CHAR(48) DEFAULT '', PRIMARY KEY (user_name));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into users(user_name,user_passwd) values('zhangsan', password('123456'));
Query OK, 1 row affected, 1 warning (0.02 sec)
mysql> insert into users values('lisi', password('123456'));
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| zz |
+--------------------+
5 rows in set (0.00 sec)
mysql> select * from zz.users;
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
2 rows in set (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
(2)物理冷備份
tar命令直接打包數(shù)據(jù)庫文件夾
[root@localhost ~]# systemctl stop mysqld
#在根下新建個(gè)backup目錄
[root@localhost ~]# mkdir /backup
#/usr/local/mysql/data備份壓縮
[root@localhost ~]# tar zcf /backup/mysql_all-$(date +%F).tar.gz /usr/local/mysql/data/
[root@localhost ~]# cd /backup/
[root@localhost backup]# ll
總用量 1280
-rw-r--r-- 1 root root 1310300 9月14 06:24 mysql_all-2020-09-14.tar.gz
(3)模擬故障
[root@localhost backup]#cd ~
[root@localhost backup]# mkdir /bak
#將目錄data數(shù)據(jù)移動(dòng)到bak 里面去
[root@localhost backup]# mv /usr/local/mysql/data/ /bak/
[root@localhost backup]# cd /usr/local/mysql/
#進(jìn)mysql目錄查看data
[root@localhost mysql]# ll
####查看后data目錄被移走 ,模擬故障成功
總用量 64
drwxr-xr-x 2 mysql mysql 4096 8月 4 19:19 bin
-rw-r--r-- 1 mysql mysql 17987 9月 13 2017 COPYING
-rw-r--r-- 1 mysql mysql 17987 9月 13 2017 COPYING-test
drwxr-xr-x 2 mysql mysql 55 8月 4 19:19 docs
drwxr-xr-x 3 mysql mysql 4096 8月 4 19:19 include
drwxr-xr-x 4 mysql mysql 191 8月 4 19:19 lib
drwxr-xr-x 4 mysql mysql 30 8月 4 19:19 man
drwxr-xr-x 10 mysql mysql 4096 8月 4 19:20 mysql-test
-rw-r--r-- 1 mysql mysql 2478 9月 13 2017 README
-rw-r--r-- 1 mysql mysql 2478 9月 13 2017 README-test
drwxr-xr-x 28 mysql mysql 4096 8月 4 19:20 share
drwxr-xr-x 2 mysql mysql 90 8月 4 19:20 support-files
drwxr-xr-x 3 mysql mysql 17 8月 4 19:19 usr
###data消失
(3)將備份的數(shù)據(jù)庫恢復(fù)
[root@localhost backup]#cd ~
####根目錄新建restore 目錄
[root@localhost ~]# mkdir /restore
#將備份數(shù)據(jù)庫解壓到 /restore目錄下,看好時(shí)間節(jié)點(diǎn)時(shí)間節(jié)點(diǎn)不一樣
[root@localhost ~]# tar xzvf /backup/mysql_all-2018-08-12.tar.gz -C /restore/
usr/local/mysql/data/
usr/local/mysql/data/ibdata1
usr/local/mysql/data/ib_logfile1
usr/local/mysql/data/ib_logfile0
.........省略........
#將備份數(shù)據(jù)移動(dòng)到到/usr/local/mysql中
[root@localhost mysql]# mv /restore/usr/local/mysql/data/ /usr/local/mysql/
[root@localhost ~]# cd /usr/local/mysql/
[root@localhost mysql]# ll
drwxr-x--- 6 mysql mysql 157 9月 14 06:23 data
[root@localhost mysql]# systemctl start mysqld
[root@localhost mysql]# systemctl status mysqld
[root@localhost mysql]# mysql -uroot -p -e 'select * from zz.users'
Enter password:
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
##數(shù)據(jù)恢復(fù)成功
mysqldump備份與恢復(fù)
■將指定的庫、表、或全部的庫導(dǎo)出為SQL腳本
■mysqldump備份需要和mysql進(jìn)行數(shù)據(jù)交互,如果關(guān)閉mysql 則無法備份和恢復(fù)
mysqldump [選項(xiàng)] 庫名 [表名1] [表名2] … > /備份路徑/備份文件名 |
---|
mysqldump [選項(xiàng)] --databases 庫名1 [庫名2] … > /備份路徑/備份文件名 |
mysqldump [選項(xiàng)] --all-databases > /備份路徑/備份文件名 |
需要的環(huán)境在上個(gè)列子中已經(jīng)搭建,我們直接拿來用
(1)數(shù)據(jù)庫與表備份為sql腳本
###備份數(shù)據(jù)路mysql中的user表 ,文件保存在當(dāng)前操作目錄下面
[root@localhost /]# mysqldump -u root -p mysql user >mysql-user.sql
Enter password:
####備份zz數(shù)據(jù)庫 文件保存在當(dāng)前操作目錄下面
[root@localhost /]# mysqldump -u root -p --databases zz >zz.sql
Enter password:
[root@localhost ~]# ll
總用量 12
-rw-r--r-- 1 root root 5661 9月 14 06:50 mysql-user.sql
-rw-r--r-- 1 root root 2086 9月 14 06:50 zz.sql
(2)表恢復(fù)操作
mysql [選項(xiàng)] [庫名] [表名] < /備份路徑/備份文件名
為了方便測試,我直接講表導(dǎo)入新建的text庫中
[root@localhost /]# mysql -u root -p
Enter password:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| zz |
+--------------------+
5 rows in set (0.00 sec)
#新建數(shù)據(jù)庫text ,等下導(dǎo)表用###
mysql> create database text;
mysql> exit
[root@localhost opt]# mysql -u root -p text < mysql-user.sql
Enter password:
[root@localhost /]# mysql -u root -p -e 'show tables from text'
Enter password:
+----------------+
| Tables_in_text |
+----------------+
| user | ####導(dǎo)進(jìn)去的表
+----------------+
(3)庫恢復(fù)操作
[root@localhost /]# mysql -u root -p -e ‘drop database auth’
######模擬故障 刪除zz數(shù)據(jù)庫
[root@localhost /]# mysql -u root -p -e 'drop database zz'
[root@localhost ~]# mysql -u root -p -e 'show databases'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| text |
+--------------------+
####下面開始恢復(fù)
[root@localhost ~]# mysql -u root -p < zz.sql
[root@localhost ~]# mysql -u root -p -e 'show databases'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| text |
| zz | ##導(dǎo)進(jìn)去的數(shù)據(jù)庫zz
+--------------------+
##再看下表中數(shù)據(jù)是否還在
[root@localhost ~]# mysql -uroot -p -e 'select * from zz.users'
Enter password:
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
MySQL數(shù)據(jù)庫增量備份恢復(fù)
■增量備份的特點(diǎn):
與完全備份完全不同,增量備份沒有重復(fù)數(shù)據(jù),備份量不大,時(shí)間短;但其恢復(fù)麻煩,需要上次完成完全備份及備份之后的所有增量備份才能恢復(fù)。
■MySQL數(shù)據(jù)庫二進(jìn)制日志對備份的意義:
二進(jìn)制日志保存了所有更新或者可能更新數(shù)據(jù)的操作。二進(jìn)制日志在啟動(dòng)mysql服務(wù)器后開始記錄,并在文件達(dá)到二進(jìn)制日志所設(shè)置的最大值或者接受到flush logs命令后重新創(chuàng)建新的日志文件,生成二進(jìn)制的文件序列,并及時(shí)把這些日志文件保存到安全的存儲(chǔ)位置,即可完成一個(gè)時(shí)間段的增量備份
(1)首先搭建環(huán)境
[root@localhost mysql]# vi /etc/my.cnf
[mysqld] 找到這個(gè)模塊,在最后一個(gè)=下添加
log-bin=/usr/local/mysql/data/mysql-bin
#這個(gè)地方開啟二進(jìn)制日志功能
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# ll /usr/local/mysql/data/
-rw-r----- 1 mysql mysql 154 9月 14 07:05 mysql-bin.000001
-rw-r----- 1 mysql mysql 39 9月 14 07:05 mysql-bin.index
[root@localhost opt]# mysql -u root -p
Enter password:
mysql> create database client;
Query OK, 1 row affected (0.01 sec)
mysql> use client;
mysql> create table info(身份證 char(20) not null,姓名 char(20) not null,姓別 char(4),用戶ID號(hào) char(10) not null,資費(fèi) int(10));
mysql> insert into info values('000006','張三','男','016','10');
mysql> insert into info values('000007','李四','女','017','91');
mysql> insert into info values('000008','王五','女','018','23');
mysql> select * from info;
####查看當(dāng)前數(shù)據(jù)庫binlog文件
mysql> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 1492 |
+------------------+-----------+
1 row in set (0.00 sec)
##進(jìn)行一次完全備份
##備份前需要將數(shù)據(jù)庫加鎖,防止數(shù)據(jù)在備份時(shí)寫入。
mysql> flush tables with read lock;
mysql> exit
Bye
[root@localhost ~]# mkdir /mysql_bak
[root@localhost opt]# mysqldump -u root -p client info >/mysql_bak/client_userinfo-$(date +%F).sql
[root@localhost ~]# ll /mysql_bak/
-rw-r--r-- 1 root root 2055 9月 13 23:10 client_userinfo-2019-09-13.sql
##將下面的操作存入新的 mysql-bin 日志文件,直到下一個(gè)新的 mysql-bin出現(xiàn)
[root@localhost mysql_bak]# mysqladmin -u root -p flush-logs
[root@localhost ~]# ll /usr/local/mysql/data/
-rw-r----- 1 mysql mysql 1539 9月 13 23:11 mysql-bin.000001
-rw-r----- 1 mysql mysql 154 9月 13 23:13 mysql-bin.000002
-rw-r----- 1 mysql mysql 78 9月 13 23:11 mysql-bin.index
flush tables with read lock;這個(gè)命令是全局讀鎖定,執(zhí)行了命令之后所有庫所有表都被鎖定只讀。一般都是用在數(shù)據(jù)庫聯(lián)機(jī)備份,這個(gè)時(shí)候數(shù)據(jù)庫的寫操作將被阻塞,讀操作順利進(jìn)行
(2)繼續(xù)錄入新的內(nèi)容并進(jìn)行增量備份
[root@localhost mysql_bak]# mysql -u root -p
Enter password:
mysql> unlock tables; #######解除表鎖。
Query OK, 0 rows affected (0.00 sec)
mysql> use client;
mysql> insert into info values('000009','趙六','男','019','37');
mysql> insert into info values('000010','孫七','男','020','36');
mysql> select * from info;
+-----------+--------+--------+-------------+--------+
| 身份證 | 姓名 | 姓別 | 用戶ID號(hào) | 資費(fèi) |
+-----------+--------+--------+-------------+--------+
| 000006 | 張三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
| 000009 | 趙六 | 男 | 019 | 37 |
| 000010 | 孫七 | 男 | 020 | 36 |
+-----------+--------+--------+-------------+--------+
5 rows in set (0.00 sec)
mysql> exit
Bye
##將下面的操作再次生成新的 mysql-bin 日志文件,上面所有的操作都在000002里面
[root@localhost mysql_bak]# mysqladmin -u root -p flush-logs
Enter password:
[root@localhost ~]# ll /usr/local/mysql/data
-rw-r----- 1 mysql mysql 1539 9月 13 23:11 mysql-bin.000001
-rw-r----- 1 mysql mysql 797 9月 13 23:13 mysql-bin.000002
-rw-r----- 1 mysql mysql 154 9月 13 23:15 mysql-bin.000003
[root@localhost ~]# cp /usr/local/mysql/data/mysql-bin.000002 /mysql_bak/
(3)模擬操作失誤 刪除info表
[root@localhost mysql_bak]# mysql -u root -p -e 'drop table client.user_info'
Enter password:
[root@localhost mysql_bak]# mysql -u root -p -e 'select * from client.user_info'
Enter password:
ERROR 1146 (42S02) at line 1: Table 'client.user_info' doesn't exist
(4)恢復(fù)操作
[root@localhost ~]# mysql -u root -p client < /mysql_bak/client_userinfo-2020-09-13.sql
Enter password:
[root@localhost mysql_bak]# mysql -u root -p -e'select * from client.info'
Enter password:
+-----------+--------+--------+-------------+--------+
| 身份證 | 姓名 | 姓別 | 用戶ID號(hào) | 資費(fèi) |
+-----------+--------+--------+-------------+--------+
| 000006 | 張三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
+-----------+--------+--------+-------------+--------+
## /mysql_bak/ 這個(gè)路徑我做了cp,你也可以/usr/local/mysql/data/
[root@localhost ~]# mysqlbinlog --no-defaults /mysql_bak/mysql-bin.000002 |mysql -u root -p
Enter password:
[root@localhost ~]# mysql -u root -p -e'select * from client.info'
Enter password:
+-----------+--------+--------+-------------+--------+
| 身份證 | 姓名 | 姓別 | 用戶ID號(hào) | 資費(fèi) |
+-----------+--------+--------+-------------+--------+
| 000006 | 張三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
| 000009 | 趙六 | 男 | 019 | 37 |
| 000010 | 孫七 | 男 | 020 | 36 |
+-----------+--------+--------+-------------+--------+
基于位置的恢復(fù)
(1)恢復(fù)數(shù)據(jù)到指定位置
mysqlbinlog --stop-position=’操作 id’ 二進(jìn)制日志 |mysql -u 用戶名 -p 密碼
環(huán)境上面已經(jīng)搭建完畢
#刪除表再查看有沒有這個(gè)表
[root@localhost mysql_bak]# mysql -uroot -p -e 'drop table client.info'
Enter password:
[root@localhost mysql_bak]# mysql -uroot -p -e 'select * from client.info'
Enter password:
ERROR 1146 (42S02) at line 1: Table 'client.info' doesn't exist
[root@localhost mysql_bak]# mysql -u root -p client < /mysql_bak/client_info-2018-08-14.sql
Enter password:
[root@localhost ~]# mysql -u root -p -e'select * from client.info'
Enter password:
+-----------+--------+--------+-------------+--------+
| 身份證 | 姓名 | 姓別 | 用戶ID號(hào) | 資費(fèi) |
+-----------+--------+--------+-------------+--------+
| 000006 | 張三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
+-----------+--------+--------+-------------+--------+
##使用這條命令可以將無規(guī)則亂碼的二進(jìn)制文件編排
[root@localhost ~]# mysqlbinlog --no-defaults --base64-output=decode-rows -v /mysql_bak/mysql-bin.000002
# at 293
#180815 6:07:34 server id 1 end_log_pos 359 CRC32 0x3ccdc62e Table_map: `client`.`user_info` mapped to number 221
# at 359
#180815 6:07:34 server id 1 end_log_pos 421 CRC32 0x55f72453 Write_rows: table id 221 flags: STMT_END_F
### INSERT INTO `client`.`user_info`
### SET
### @1='000009'
### @2='趙六'
### @3='男'
### @4='019'
### @5=37
# at 421
#180815 6:07:34 server id 1 end_log_pos 452 CRC32 0x45fd2af6 Xid = 50
COMMIT/*!*/;
# at 452
#180815 6:07:39 server id 1 end_log_pos 517 CRC32 0xa553503b Anonymous_GTID last_committed=1 sequence_number=2 rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 517
#180815 6:07:39 server id 1 end_log_pos 591 CRC32 0x02ad5ee9 Query thread_id=7 exec_time=0 error_code=0
SET TIMESTAMP=1534284459/*!*/;
BEGIN
/*!*/;
# at 591
#180815 6:07:39 server id 1 end_log_pos 657 CRC32 0x3f478909 Table_map: `client`.`user_info` mapped to number 221
# at 657
#180815 6:07:39 server id 1 end_log_pos 719 CRC32 0xb478e1c5 Write_rows: table id 221 flags: STMT_END_F
### INSERT INTO `client`.`user_info`
### SET
### @1='000010'
### @2='孫七'
### @3='男'
### @4='020'
### @5=36
# at 719
#180815 6:07:39 server id 1 end_log_pos 750 CRC32 0xe681426b Xid = 51
COMMIT/*!*/;
# at 750
#180815 6:07:59 server id 1 end_log_pos 797 CRC32 0xbc922301 Rotate to mysql-bin.000003 pos: 4
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
(2)#如果我只想要趙六的數(shù)據(jù)恢復(fù),該怎么辦?找到趙六結(jié)束的at 數(shù)字,不要緊挨著趙六的at,要隔一個(gè)最好
通過上面的配置文件,我們看到趙六下面的 at 517,那就以這個(gè)為結(jié)束點(diǎn),恢復(fù)備份
[root@localhost ~]# mysqlbinlog --no-defaults --stop-position='517' /mysql_bak/mysql-bin.000002 |mysql -u root -p
[root@localhost ~]# mysql -u root -p -e'select * from client.info'
Enter password:
+-----------+--------+--------+-------------+--------+
| 身份證 | 姓名 | 姓別 | 用戶ID號(hào) | 資費(fèi) |
+-----------+--------+--------+-------------+--------+
| 000006 | 張三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
| 000009 | 趙六 | 男 | 019 | 37 |
+-----------+--------+--------+-------------+--------+
(3)#如果我只想讓孫七的數(shù)據(jù)數(shù)據(jù)恢復(fù),怎么辦?找到孫七開始前的at 數(shù)字,不要緊挨著孫七的at,要隔一個(gè)最好
通過上面的配置文件,我們看到孫七上面的 at 657,那就以這個(gè)為開始點(diǎn),恢復(fù)備份
[root@localhost ~]# mysql -u root -p -e 'drop table client.info'
Enter password:
[root@localhost ~]# mysql -u root -p client < /mysql_bak/client_info-2018-08-15.sql
Enter password:
[root@localhost ~]# mysqlbinlog --no-defaults --start-position='657' /mysql_bak/mysql-bin.000002 |mysql -u root -p ####操作的節(jié)點(diǎn)附近即可
Enter password:
[root@localhost ~]# mysql -u root -p -e'select * from client.info'
Enter password:
+-----------+--------+--------+-------------+--------
| 身份證 | 姓名 | 姓別 | 用戶ID號(hào) | 資費(fèi) |
+-----------+--------+--------+-------------+--------+
| 000006 | 張三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
| 000010 | 孫七 | 男 | 020 | 36 |
+-----------+--------+--------+-------------+--------+
總結(jié):
基于位置恢復(fù)的操作,節(jié)點(diǎn)不要選擇太靠近操作的節(jié)點(diǎn),否則容易恢復(fù)失敗。
基于時(shí)間恢復(fù)
跟上面差不多,我怕么可以看到at 517下一行有 180815 6:07:39 ## 基于這個(gè)時(shí)間點(diǎn),可以以這個(gè)時(shí)間為結(jié)束時(shí)間,恢復(fù)這個(gè)時(shí)間之前的數(shù)據(jù)
也可以不要之前的數(shù)據(jù),以這個(gè)時(shí)間點(diǎn)為開始,恢復(fù)這個(gè)時(shí)間之后的數(shù)據(jù)
(1)只需要趙六的
[root@localhost ~]# mysql -u root -p -e 'drop table client.info'
Enter password:
[root@localhost ~]# mysql -u root -p client < /mysql_bak/client_info-2018-08-15.sql
Enter password:
[root@zeng ~]# mysql -u root -p -e'select * from client.info'
Enter password:
+-----------+--------+--------+-------------+--------+
| 身份證 | 姓名 | 姓別 | 用戶ID號(hào) | 資費(fèi) |
+-----------+--------+--------+-------------+--------+
| 000006 | 張三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
+-----------+--------+--------+-------------+--------+
########時(shí)間戳不要弄錯(cuò)了
[root@localhost ~]# mysqlbinlog --no-defaults --stop-datetime='2018-8-15 6:07:39' /mysql_bak/mysql-bin.000002 |mysql -u root -p
Enter password:
[root@localhost ~]# mysql -u root -p -e'select * from client.info'
Enter password:
+-----------+--------+--------+-------------+--------+
| 身份證 | 姓名 | 姓別 | 用戶ID號(hào) | 資費(fèi) |
+-----------+--------+--------+-------------+--------+
| 000006 | 張三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
| 000009 | 趙六 | 男 | 019 | 37 |
+-----------+--------+--------+-------------+--------+
(3)只需要孫七的的
[root@localhost ~]# mysql -u root -p -e 'drop table client.info'
Enter password:
[root@localhost ~]# mysql -u root -p client < /mysql_bak/client_info-2018-08-15.sql
Enter password:
###時(shí)間戳不要弄錯(cuò)了,根據(jù)的你的日志文件來
[root@localhost ~]# mysqlbinlog --no-defaults --start-datetime='2018-8-15 15:33:48' /mysql_bak/mysql-bin.000002 |mysql -u root -p
Enter password:
[root@localhost ~]# mysql -u root -p -e'select * from client.info'
Enter password:
+-----------+--------+--------+-------------+--------+
| 身份證 | 姓名 | 姓別 | 用戶ID號(hào) | 資費(fèi) |
+-----------+--------+--------+-------------+--------+
| 000006 | 張三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
| 000010 | 孫七 | 男 | 020 | 36 |
+-----------+--------+--------+-------------+--------+
總結(jié):
如果你學(xué)了我的操作手法,那么生產(chǎn)環(huán)境嚴(yán)格按照此要求來