前言
在日常工作或者學(xué)習(xí)中,操作數(shù)據(jù)庫時候難免會因為“大意”而誤操作,需要快速恢復(fù)的話通過備份來恢復(fù)是不太可能的,下面這篇文章主要給大家介紹關(guān)于Mysql誤操作后利用binlog2sql快速回滾的方法,話不多說,來一起看看詳細的介紹:
一、總體解釋:
DML(data manipulation language):
它們是SELECT、UPDATE、INSERT、DELETE,就象它的名字一樣,這4條命令是用來對數(shù)據(jù)庫里的數(shù)據(jù)進行操作的語言
DDL(data definition language):
DDL比DML要多,主要的命令有CREATE、ALTER、DROP等,DDL主要是用在定義或改變表(TABLE)的結(jié)構(gòu),數(shù)據(jù)類型,表之間的鏈接和約束等初始化工作上,他們大多在建立表時使用
DCL(Data Control Language):
是數(shù)據(jù)庫控制功能。是用來設(shè)置或更改數(shù)據(jù)庫用戶或角色權(quán)限的語句,包括(grant,deny,revoke等)語句。在默認狀態(tài)下,只有sysadmin,dbcreator,db_owner或db_securityadmin等人員才有權(quán)力執(zhí)行DCL
二、binlog2sql安裝
從mysql binlog解析出你要的sql。根據(jù)不同選項,你可以得到原始sql、回滾sql、去除主鍵的insert sql等。
2.1、用途
2.2、安裝
| # cd /usr/local# git clone https://github.com/danfengcao/binlog2sql.git# ls binlog2sql games java lib64 mariadb sbin src # cd binlog2sql# pip install -r requirements.txt-bash: pip: command not found -------------安裝pip工具-------------# wget https://bootstrap.pypa.io/get-pip.py # python get-pip.py# pip -V #查看pip版本pip 9.0.1 from /usr/lib/python2.7/site-packages (python 2.7)# pip install -r requirements.txtRequirement already satisfied: PyMySQL==0.7.8 in /usr/lib/python2.7/site-packages (from -r requirements.txt (line 1))Requirement already satisfied: wheel==0.24.0 in /usr/lib/python2.7/site-packages (from -r requirements.txt (line 2))Requirement already satisfied: mysql-replication==0.9 in /usr/lib/python2.7/site-packages (from -r requirements.txt (line 3)) |
2.3、user需要的最小權(quán)限集合:
select, super/replication client, replication slave權(quán)限建議授權(quán)
| mysql > GRANT SELECT,REPLICATION SLAVE,REPLICATION CLIENT ON *.* to flashback@'localhost' identified by 'flashback';mysql > GRANT SELECT,REPLICATION SLAVE,REPLICATION CLIENT ON *.* to flashback@'127.0.0.1' identified by 'flashback'; |
2.4、基本用法
解析出標準SQL
| shell> python binlog2sql.py -h127.0.0.1 -P3306 -uadmin -p'admin' -ddatabase -t table1 table2 --start-file='mysql-bin.000002' --start-datetime='2017-01-12 18:00:00' --stop-datetime='2017-01-12 18:30:00' --start-pos=1240 |