前言
系列文章:
1.MySQL主從復(fù)制
2.OneProxy實(shí)現(xiàn)MySQL讀寫分離
3.MySQL數(shù)據(jù)庫結(jié)構(gòu)設(shè)計(jì)
4.MySQL基于GTID主從復(fù)制的雜談
5.MySQL復(fù)制性能優(yōu)化和常見問題分析
mysqldump全量恢復(fù)
1.創(chuàng)建douyin
數(shù)據(jù)庫百揭、tbl_douyin_author
數(shù)據(jù)庫表爽哎、插入測試數(shù)據(jù)。
create table tbl_douyin_author(
id int not null auto_increment,
author_name varchar(50) not null,
PRIMARY key (id)
)
insert into tbl_douyin_author values(null, "程序員小馬");
select * from tbl_douyin_author;
2.利用mysqldump進(jìn)行備份器一,涉及到的參數(shù)就不一一開講了课锌。
mysqldump --single-transaction
--master-data --triggers --routines
--databases douyin
--set-gtid-purged=OFF --flush-logs -uroot -pxiaoma
> douyin-dump.sql
無論是完整備份,還是部分備份祈秕。如果不設(shè)置
--set-gtid-purged=OFF
這個(gè)參數(shù)渺贤,最終的備份文件中會(huì)有這樣一句話:SET @@GLOBAL.GTID_PURGED='de3a31d3-e97e-11e8-be60-144f8aee24be:1-318402';
這個(gè)的區(qū)間是主庫中當(dāng)前所完成的所有事務(wù)號(hào)。這條語句在備份被恢復(fù)的時(shí)候请毛,起到的作用是:不再從主庫同步1-318402
這個(gè)范圍內(nèi)的事務(wù)了志鞍。如果我們是部分備份,我們不應(yīng)該阻止從庫同步1-318402
全部區(qū)間的數(shù)據(jù)方仿。因此部分備份是加上--set-gtid-purged=OFF
這句固棚,不強(qiáng)行指定跳過這些操作统翩。--single-transaction
、--master-data=2
玻孟、--flush-logs
達(dá)到了減少鎖定時(shí)間的目的唆缴,同時(shí)又達(dá)到了一致性的備份的目的,而且該一致性備份和 flush的binlog日志也是一致的黍翎。話外音:--single-transaction
利用事務(wù)的特性來得到一致性備份面徽,將隔離級別設(shè)置為:REPEATABLE READ
。并且隨后再執(zhí)行一條START TRANSACTION
語句匣掸,讓整個(gè)數(shù)據(jù)在dump過程中保證數(shù)據(jù)的一致性趟紊,這個(gè)選項(xiàng)對InnoDB
的數(shù)據(jù)表很有用,且不會(huì)鎖表碰酝。但是這個(gè)不能保證MyISAM
表和MEMORY
表的數(shù)據(jù)一致性霎匈。為了確保使用
--single-transaction
命令時(shí),保證dump
文件的有效性送爸。需沒有下列語句ALTER TABLE
,CREATE TABLE
,DROP TABLE
,RENAME TABLE
,TRUNCATE TABLE
铛嘱,因?yàn)橐恢滦宰x不能隔離上述語句。所以如果在dump
過程中袭厂,使用上述語句墨吓,可能會(huì)導(dǎo)致dump
出來的文件數(shù)據(jù)不一致或者不可用。
3.刪除douyin
數(shù)據(jù)庫
drop database douyin;
4.利用douyin-dump.sql
進(jìn)行恢復(fù)
mysql -uroot -pxiaoma < douyin-dump.sql
mysqlbinlog增量恢復(fù)
1.我們接著在tbl_douyin_author
插入4條數(shù)據(jù)
insert into tbl_douyin_author values(null, "達(dá)達(dá)");
insert into tbl_douyin_author values(null, "麻辣德子");
insert into tbl_douyin_author values(null, "翔翔大作戰(zhàn)");
insert into tbl_douyin_author values(null, "五月天");
select * from tbl_douyin_author;
2.如果我們在這個(gè)時(shí)候纹磺,不一小心進(jìn)行了drop DATABASE douyin;
的刪庫操作帖烘。想必大家的第一反應(yīng)是收拾行李,趕緊跑路把橄杨!但是飯要慢慢吃秘症,路要慢慢走,我們靜下心來還是能夠解決這個(gè)棘手的問題的式矫!怎么解決呢乡摹?首先用douyin-dump.sql
全量備份恢復(fù)到tbl_douyin_author
表只有一條數(shù)據(jù)的狀態(tài)
3.看到這里衷佃,你肯定又會(huì)疑問了趟卸,這才恢復(fù)了一條數(shù)據(jù)啊,還有4條數(shù)據(jù)呢氏义!不要急锄列,看我操作就行了。查看binlog中的事件惯悠。
show binlog events in 'mysql-bin.000047'\g
4.我們已經(jīng)確定了1641點(diǎn)的操作導(dǎo)致了刪除douyin
數(shù)據(jù)庫邻邮,所以我們要開始恢復(fù)的點(diǎn)是1576至以前。接下來我們就可以從binlog日志恢復(fù)數(shù)據(jù)了克婶。
恢復(fù)語法格式:
# mysqlbinlog mysql-bin.0000xx | mysql -u用戶名 -p密碼 數(shù)據(jù)庫名
常用選項(xiàng):
--start-position=55 起始pos點(diǎn)
--stop-position=555 結(jié)束pos點(diǎn)
--start-datetime="2019-11-29 13:00:00" 起始時(shí)間點(diǎn)
--stop-datetime="2019-11-29 17:00:00" 結(jié)束時(shí)間點(diǎn)
--database=douyin 指定只恢復(fù)douyin數(shù)據(jù)庫(一臺(tái)主機(jī)上往往有多個(gè)數(shù)據(jù)庫筒严,只限本地log日志)
不常用選項(xiàng):
-u --user=name Connect to the remote server as username.連接到遠(yuǎn)程主機(jī)的用戶名
-p --password[=name] Password to connect to remote server.連接到遠(yuǎn)程主機(jī)的密碼
-h --host=name Get the binlog from server.從遠(yuǎn)程主機(jī)上獲取binlog日志
--read-from-remote-server Read binary logs from a MySQL server.從某個(gè)MySQL服務(wù)器上讀取binlog日志
5.執(zhí)行mysqlbinlog --stop-position=1576 --skip-gtids=true --database=douyin mysql-bin.000047 | mysql -uroot -pxiaoma
命令丹泉,tbl_douyin_author
里面的數(shù)據(jù)恢復(fù)成功。
- 如果我們是要恢復(fù)數(shù)據(jù)到源數(shù)據(jù)庫或者和源數(shù)據(jù)庫有相同 GTID 信息的實(shí)例鸭蛙,那么就要使用
--skip-gtids=true
參數(shù)摹恨。如果不帶該參數(shù)的話,是無法恢復(fù)成功的娶视。因?yàn)榘?GTID 已經(jīng)在源數(shù)據(jù)庫執(zhí)行過了,根據(jù) GTID 特性,一個(gè) GTID 信息在一個(gè)數(shù)據(jù)庫只能執(zhí)行一次,所以不會(huì)恢復(fù)成功晒哄。
尾言
不管是神還是惡魔都不會(huì)對不抗?fàn)幍娜松斐鲈帧?/p>