1. 程式人生 > >MySQL mysqldump備份與恢復

MySQL mysqldump備份與恢復

1 使用者許可權

grant select,RELOAD,PROCESS,SUPER, REPLICATION CLIENT ON *.* TO 'bak'@'192.168.%' IDENTIFIED BY "**";

2 mysqldump 備份

2.1利用mysqldump進行全備份

--A 全備
/usr/local/mysql/bin/mysqldump -h 127.0.0.1 -usystem -p'***' -A -R -S /data/mysqldata/3306/mysql.sock \
--single-transaction --default-character-set=utf8 --master-data=2 | gzip > /data/mysqldata/backup/mysqldump/mysqldumpfullbk_20170301.sql.gz

 

--set global general_log=1
檢視mysqldump的原理,flush tables with read lock;短暫的一致性,不支援寫,支援讀,
snapshot,unlock tables,時間很短
--引數--single-transaction --master-data=2 --default-character-set=utf8
--會先對所有資料庫的所有表上鎖,讀取binlog的資訊之後就立即釋放鎖,這個過程是十分短暫的。然後整個匯出過程都在一個事務裡.
--慢查詢會阻塞flush table,ddl也會

--db.table 針對資料和表

/usr/local/mysql/bin/mysqldump -usystem -p -h127.0.0.1 -P3306 test a \
--single-transaction --master-data=2 --default-character-set=utf8 -q \
| gzip > /data/mysqldata/test_a`date +'%Y%m%d'`.sql.gz


--db.tables 多個表
/usr/local/mysql/bin/mysqldump -usystem -p -h127.0.0.1 -P3306 test a b \
--single-transaction --master-data=2 --default-character-set=utf8 -q \
| gzip > /data/mysqldata/test_a`date +'%Y%m%d%H'`.sql.gz


-- dbs 多個庫
/usr/local/mysql/bin/mysqldump -usystem -p -h127.0.0.1 -P3306 -B test bi1 \
--single-transaction --master-data=2 --default-character-set=utf8 -q \
| gzip > /data/mysqldata/test_a`date +'%Y%m%d%H%m'`.sql.gz

2.2 全庫恢復

[[email protected] mysqldump]$ gunzip mysqldumpfullbk_20170301.sql.gz
[[email protected] mysqldump]$ ll
total 2272
-rw-rw-r-- 1 mysql mysql 2323927 Mar 1 07:21 mysqldumpfullbk_20170301.sql
--全庫恢復
[[email protected] mysqldump]$ mysql -usystem -p*** -h127.0.0.1 -P3306 <mysqldumpfullbk_20170301.sql

 

2.3 增量恢復

在全備份後進行插入資料,然後刪除表等操作

mysql> insert into yhq_t1 values(2,'BBBBBB');
Query OK, 1 row affected (0.04 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> truncate table yhq_t1 ;
Query OK, 0 rows affected (0.05 sec)

在上全庫恢復後只有1條資料

mysql> select * from yhq_t1 ;
+------+-------+
| id | name |
+------+-------+
| 1 | AAAAA |
+------+-------+

--增量恢復,利用binlog
--可以類似查詢

mysql> system ls /data/mysqldata/3306/binlog/
mysql> show binlog events in 'mysql-bin.000011';

mysql> show master status;

[[email protected] scripts]$ mysqlbinlog --help #可以根據幫助文件,查詢mysqlbinlog支援的引數,常用的 -d,start-datetime,start-position,stop-datetime等

$ mysqlbinlog -d test -vv mysql-bin.000011 >/tmp/mysqlbin07.txt #找到對應的binlog檔案

$ mysqlbinlog --no-defaults -v -v --base64-output=DECODE-ROWS mysql-bin.000011--stop-datetime="2017-08-31 07:19:33" --start-datetime="2017-08-31 07:17:33"  >/tmp/test0732.sql

$ mysql -usystem  -p*** -h127.0.0.1  -P3306  </tmp/test0732.sql

 

--如果有多個binlog需要恢復,就類似這樣,然後在匯入

shell> mysqlbinlog binlog.000001 > /tmp/statements.sql
shell> mysqlbinlog binlog.000002 >> /tmp/statements.sql

Point-in-Time (Incremental) Recovery Using the Binary Log
1 Point-in-Time Recovery Using Event Times
為了指示要恢復的開始和結束時間,指定的mysqlbinlog可以--start - datetime和--stop -日期時間選擇,在DATETIME格式。
舉個例子,假設在2005年4月20日,正是在上午10點的SQL語句被執行的刪除一個大表。
要恢復表和資料,你可以恢復前一天晚上的備份,然後執行以下命令
shell> mysqlbinlog --stop-datetime="2005-04-20 9:59:59" \
/var/log/mysql/bin.123456 | mysql -u root -p
這個命令恢復所有的資料直到--stop-datetime指定的時間
如果沒有檢測已輸入直到小時後,錯誤的SQL語句,你可能還需要恢復之後發生的活動。
shell> mysqlbinlog --start-datetime="2005-04-20 10:01:00" \
/var/log/mysql/bin.123456 | mysql -u root -p
要使用點及時恢復的這種方法,您應檢查日誌,以確保準確的時間到指定的命令。為了不執行它們顯示日誌檔案的內容,請使用以下命令
shell> mysqlbinlog /var/log/mysql/bin.123456 > /tmp/mysql_restore.sql
Then open the /tmp/mysql_restore.sql file with a text editor to examine it.

2 Point-in-Time Recovery Using Event Positions
--start-position and --stop-position
shell> mysqlbinlog --start-datetime="2005-04-20 9:55:00" \
--stop-datetime="2005-04-20 10:05:00" \
/var/log/mysql/bin.123456 > /tmp/mysql_restore.sql
用文字編輯器開啟該檔案,尋找你不要想重複的語句。確定二進位制日誌的位置停止和恢復恢復,
使他們的註釋。位置被標記為log_pos後跟一個數字。恢復以前的備份檔案後,使用位置編號來處理二進位制日誌檔案。
shell> mysqlbinlog --stop-position=368312 /var/log/mysql/bin.123456 \
| mysql -u root -p

shell> mysqlbinlog --start-position=368315 /var/log/mysql/bin.123456 \
| mysql -u root -p

drop table用mysqldump恢復
drop table恢復測試
1 create table t_d/insert
2 mysqldump -A
3 insert into t_d/drop
--恢復操作
1 mysqldump中恢復到備份時刻
2 根據binlog位置點,恢復到drop的位置點
--test
([email protected]:3306) [test]> select * from t_d;
+---+
| a |
+---+
| 1 |
| 2 |
| 4 |
| 5 |
mysqldump
insert into t_d values (6),(7),(8);
([email protected]:3306) [test]> select * from t_d;
+---+
| a |
+---+
| 1 |
| 2 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
drop table t_d

mysql mysql 101305262 Oct 31 01:05 allDatabase_20171031.sql.gz
$ gunzip allDatabase_20171031.sql.gz
$ head -50 allDatabase_20171031.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000078', MASTER_LOG_POS=154;

[[email protected] logical]$ sed -e'/./{H;$!d;}' -e 'x;/CREATE TABLE `t_d`/!d;q' allDatabase_20171031.sql

ROP TABLE IF EXISTS `t_d`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `t_d` (
`a` int(11) NOT NULL,
PRIMARY KEY (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
[[email protected] logical]$ grep 'INSERT INTO `t_d`' allDatabase_20171031.sql>t_d_bak20171031.sql
[[email protected] logical]$ cat t_d_bak20171031.sql
INSERT INTO `t_d` VALUES (1),(2),(4),(5);

([email protected]:3306) [test]> select * from t_d;
+---+
| a |
+---+
| 1 |
| 2 |
| 4 |
| 5 |
+---+

[[email protected] binlog]$ mysqlbinlog -v -v --base64-output=DECODE-ROWS --set-charset=UTF8 --start-position=154 /data/mysqldata/3306/binlog/mysql-bin.000078 |grep DROP -A10 -B10
# at 1630230
#171031 2:10:32 server id 20170814 end_log_pos 1630261 CRC32 0xdf5c044f Xid = 6032638
COMMIT/*!*/;
# at 1630261
#171031 2:10:31 server id 20170814 end_log_pos 1630326 CRC32 0xbe0887b7 Anonymous_GTID last_committed=4148 sequence_number=4149
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 1630326
#171031 2:10:31 server id 20170814 end_log_pos 1630442 CRC32 0x91db776e Query thread_id=635196 exec_time=1 error_code=0
use `test`/*!*/;
SET TIMESTAMP=1509387031/*!*/;
DROP TABLE `t_d` /* generated by server */
/*!*/;
# at 1630442
#171031 2:10:33 server id 20170814 end_log_pos 1630507 CRC32 0x84362a1f Anonymous_GTID last_committed=4149 sequence_number=4150
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 1630507
#171031 2:10:33 server id 20170814 end_log_pos 1630581 CRC32 0x9ba1d726 Query thread_id=14 exec_time=0 error_code=0
SET TIMESTAMP=1509387033/*!*/;
BEGIN
/*!*/;
# at 1630581
--刪除事件1630442,我們要恢復到這個之前,也就是1630326這個事件
[[email protected] binlog]$ mysqlbinlog -v -v --base64-output=DECODE-ROWS --set-charset=UTF8 --start-position=154 --stop-position=1630326 \
/data/mysqldata/3306/binlog/mysql-bin.000078 > db_test_t_d20171031.sql

-----
'/*!*/;
### INSERT INTO `test`.`t_d`
### SET
### @1=6 /* INT meta=0 nullable=0 is_null=0 */
### INSERT INTO `test`.`t_d`
### SET
### @1=7 /* INT meta=0 nullable=0 is_null=0 */
### INSERT INTO `test`.`t_d`
### SET
### @1=8 /* INT meta=0 nullable=0 is_null=0 */
# at 1619713
#171031 2:10:03 server id 20170814 end_log_pos 1619744 CRC32 0xc7b1762a Xid = 6032401
COMMIT/*!*/;

3 mysqldump 日誌

-----------
show master status;
show variables like 'general_log';
show variables like 'log_output';
show variables like 'general_log_file';
set global general_log=on;
set global log_output='table';
set global log_output='file';
show create table mysql.general_log;
select command_type,argument from mysql.general_log order by event_time desc;
delete from mysql.general_log;
set global general_log=off;
-----------
2017-09-28T22:42:26.099799Z 357112 Query show variables like 'general_log_file'
2017-09-28T22:42:56.854552Z 370024 Connect [email protected] on using TCP/IP
2017-09-28T22:42:56.855764Z 370024 Query /*!40100 SET @@SQL_MODE='' */
2017-09-28T22:42:56.856766Z 370024 Query /*!40103 SET TIME_ZONE='+00:00' */
2017-09-28T22:42:56.857718Z 370024 Query FLUSH /*!40101 LOCAL */ TABLES
2017-09-28T22:42:56.875349Z 370024 Query FLUSH TABLES WITH READ LOCK
2017-09-28T22:42:56.876115Z 370024 Query SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ
2017-09-28T22:42:56.876737Z 370024 Query START TRANSACTION /*!40100 WITH CONSISTENT SNAPSHOT */
2017-09-28T22:42:56.877350Z 370024 Query SHOW VARIABLES LIKE 'gtid\_mode'
2017-09-28T22:42:56.883794Z 370024 Query SHOW MASTER STATUS
2017-09-28T22:42:56.884410Z 370024 Query UNLOCK TABLES
FROM INFORMATION_SCHEMA.PARTITIONS
FROM INFORMATION_SCHEMA.FILES
2017-09-28T22:42:56.936366Z 370024 Query SHOW VARIABLES LIKE 'ndbinfo\_version'
2017-09-28T22:42:56.938931Z 370024 Init DB test
2017-09-28T22:42:56.939138Z 370024 Query SHOW CREATE DATABASE IF NOT EXISTS `test`
2017-09-28T22:42:56.939242Z 370024 Query SAVEPOINT sp
2017-09-28T22:42:56.939331Z 370024 Query show tables
2017-09-28T22:42:56.939917Z 370024 Query show table status like 'BatchTemp'
2017-09-28T22:42:56.940558Z 370024 Query SET SQL_QUOTE_SHOW_CREATE=1
2017-09-28T22:42:56.940618Z 370024 Query SET SESSION character_set_results = 'binary'
2017-09-28T22:42:56.940755Z 370024 Query show create table `BatchTemp`
2017-09-28T22:42:56.941053Z 370024 Query SET SESSION character_set_results = 'utf8'
2017-09-28T22:42:56.941321Z 370024 Query show fields from `BatchTemp`
2017-09-28T22:42:56.942625Z 370024 Query show fields from `BatchTemp`
2017-09-28T22:42:56.943093Z 370024 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `BatchTemp`
2017-09-28T22:42:56.948903Z 370024 Query SET SESSION character_set_results = 'binary'
2017-09-28T22:42:56.949070Z 370024 Query use `test`
2017-09-28T22:42:56.949250Z 370024 Query select @@collation_database
2017-09-28T22:42:56.949513Z 370024 Query SHOW TRIGGERS LIKE 'BatchTemp'
2017-09-28T22:42:56.950046Z 370024 Query SET SESSION character_set_results = 'utf8'
2017-09-28T22:42:56.950190Z 370024 Query ROLLBACK TO SAVEPOINT sp