本篇文章為大家展示了MySQL中怎么通過binlog日志恢復(fù)數(shù)據(jù),內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
站在用戶的角度思考問題,與客戶深入溝通,找到鄂倫春網(wǎng)站設(shè)計(jì)與鄂倫春網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋鄂倫春地區(qū)。
一、數(shù)據(jù)備份
操作的前一天晚上進(jìn)行了日常邏輯備份
mysqldump -uroot -pmysql -P3306 --all-databases > /mysql/backup/dump/alldb_bak.sql
二、模擬事故
模擬事故發(fā)生前后的業(yè)務(wù)情況
mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | kk | | t1 | | t2 | | t3 | | t4 | | t5 | | t6 | | t7 | +----------------+ 8 rows in set (0.00 sec) mysql> desc t7; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(30) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec) mysql> create table t8 as select * from t7; Query OK, 3 rows affected (0.17 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from t8; +----+--------+ | id | name | +----+--------+ | 1 | steven | | 3 | steven | | 4 | steven | +----+--------+ 3 rows in set (0.00 sec) mysql> insert into t8 select * from t7; Query OK, 3 rows affected (0.04 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from t8; +----+--------+ | id | name | +----+--------+ | 1 | steven | | 3 | steven | | 4 | steven | | 1 | steven | | 3 | steven | | 4 | steven | +----+--------+ 6 rows in set (0.00 sec) mysql> update t8 set id=2 where id=3; Query OK, 2 rows affected (0.33 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> update t8 set id=3 where id=4; Query OK, 2 rows affected (0.03 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> select * from t8; +----+--------+ | id | name | +----+--------+ | 1 | steven | | 2 | steven | | 3 | steven | | 1 | steven | | 2 | steven | | 3 | steven | +----+--------+ 6 rows in set (0.00 sec) mysql> drop table t8; Query OK, 0 rows affected (0.10 sec)
三、查看當(dāng)前binlog
mysql> show master status ; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 1344 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
四、恢復(fù)數(shù)據(jù)
拷貝生產(chǎn)庫前一天晚上的備份文件以及備份到事故期間的binlog至臨時(shí)庫
scp alldb_bak.sql 192.168.8.32:/mysql/backup/dump/
scp /mysql/data/mysql-bin.000001 192.168.8.32:/mysql/backup/dump/
在臨時(shí)庫創(chuàng)建出現(xiàn)事故的database
mysql> create database test; Query OK, 1 row affected (0.03 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | | testdb13 | | testdb14 | | testdb15 | | testdb16 | | testdb17 | | testdb18 | | testdb19 | | testdb20 | | testdb21 | | testdb22 | | testdb23 | | testdb24 | +--------------------+ 17 rows in set (0.00 sec)
從備份中恢復(fù)test數(shù)據(jù)庫
mysql -uroot -pmysql -P3306 -o test < alldb_bak.sql
-o是指單獨(dú)恢復(fù)test庫,忽略其他數(shù)據(jù)庫
從mysql-bin.000001中查看到drop table t8之前的pos是1164
update t8 set id=3 where id=4 /*!*/; # at 1133 #181127 14:12:41 server id 330631 end_log_pos 1164 CRC32 0x1203751c Xid = 1661 COMMIT/*!*/; # at 1164 #181127 14:12:53 server id 330631 end_log_pos 1229 CRC32 0x48fad728 Anonymous_GTID last_committed=4 sequence_number=5 rbr_only=no SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/; # at 1229 #181127 14:12:53 server id 330631 end_log_pos 1344 CRC32 0x2a7eb0d7 Query thread_id=3 exec_time=1 error_code=0 SET TIMESTAMP=1543299173/*!*/; DROP TABLE `t8` /* generated by server */ /*!*/;
mysqlbinlog --no-defaults --stop-position=1164 --database=test mysql-bin.000001 |mysql -uroot -p test
五、根據(jù)臨時(shí)庫的數(shù)據(jù),將該表恢復(fù)至生產(chǎn)庫
六、數(shù)據(jù)驗(yàn)證
mysql> use test; Database changed mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | kk | | t1 | | t2 | | t3 | | t4 | | t5 | | t6 | | t7 | | t8 | +----------------+ 9 rows in set (0.01 sec) mysql> select * from t8; +----+--------+ | id | name | +----+--------+ | 1 | steven | | 2 | steven | | 3 | steven | | 1 | steven | | 2 | steven | | 3 | steven | +----+--------+ 6 rows in set (0.00 sec)
上述內(nèi)容就是MySQL中怎么通過binlog日志恢復(fù)數(shù)據(jù),你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。