您好,登錄后才能下訂單哦!
下文給大家帶來關于利用Mysqlbinlog日志恢復數據庫數據的操作指引,感興趣的話就一起來看看這篇文章吧,相信看完利用Mysqlbinlog日志恢復數據庫數據的操作指引對大家多少有點幫助吧。
一、開啟mysql-binlog日志
在mysql配置文件my.cnf加上如下配置
[mysqld]
log-bin=mysql-bin
binlog_do_db=bin_test
重啟mysql
service mysqld restart
二、備份數據庫
1)先查看一下當前數據庫情況及binlog日志情況:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 106 | bin_test | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
2)創建庫及數據:bin_test
mysql> create database bin_test;
Query OK, 1 row affected (0.00 sec)
mysql> use bin_test;
Database changed
mysql> create table abc(id int(10) primary key auto_increment,name varchar(255));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into abc(name) value('zhangsan');
Query OK, 1 row affected (0.00 sec)
mysql> insert into abc(name) value('lisi');
Query OK, 1 row affected (0.00 sec)
mysql> insert into abc(name) value('wangwu');
Query OK, 1 row affected (0.00 sec)
mysql> select * from abc;
+----+----------+
| id | name |
+----+----------+
| 1 | zhangsan |
| 2 | lisi |
| 3 | wangwu |
+----+----------+
3 rows in set (0.00 sec)
3)備份數據到/tmp/test.sql
[root@localhost ~]# whereis mysqldump
mysqldump: /usr/bin/mysqldump /usr/share/man/man1/mysqldump.1.gz
[root@localhost ~]# /usr/bin/mysqldump -uroot -p123456 bin_test > /tmp/test.sql
4)查看下binlog日志情況:
mysql> show binlog events in 'mysql-bin.000001';
+------------------+-----+-------------+-----------+-------------+-------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+-------------+-----------+-------------+-------------------------------------------------------------------------------------------+
| mysql-bin.000001 | 4 | Format_desc | 1 | 106 | Server ver: 5.1.73-log, Binlog ver: 4 |
| mysql-bin.000001 | 106 | Query | 1 | 197 | create database bin_test |
| mysql-bin.000001 | 197 | Query | 1 | 337 | use `bin_test`; create table abc(id int(10) primary key auto_increment,name varchar(255)) |
| mysql-bin.000001 | 337 | Intvar | 1 | 365 | INSERT_ID=1 |
| mysql-bin.000001 | 365 | Query | 1 | 471 | use `bin_test`; insert into abc(name) value('zhangsan') |
| mysql-bin.000001 | 471 | Intvar | 1 | 499 | INSERT_ID=2 |
| mysql-bin.000001 | 499 | Query | 1 | 601 | use `bin_test`; insert into abc(name) value('lisi') |
| mysql-bin.000001 | 601 | Intvar | 1 | 629 | INSERT_ID=3 |
| mysql-bin.000001 | 629 | Query | 1 | 733 | use `bin_test`; insert into abc(name) value('wangwu') |
+------------------+-----+-------------+-----------+-------------+-------------------------------------------------------------------------------------------+
9 rows in set (0.00 sec)
三、這時模擬誤操作(刪除數據庫)
此時突然數據庫損壞或者人為刪除
mysql> drop database bin_test;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)
再查看下binlog日志有沒有記錄刪庫的操作:
mysql> show binlog events in "mysql-bin.000001";
+------------------+-----+-------------+-----------+-------------+-------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+-------------+-----------+-------------+-------------------------------------------------------------------------------------------+
| mysql-bin.000001 | 4 | Format_desc | 1 | 106 | Server ver: 5.1.73-log, Binlog ver: 4 |
| mysql-bin.000001 | 106 | Query | 1 | 197 | create database bin_test |
| mysql-bin.000001 | 197 | Query | 1 | 337 | use `bin_test`; create table abc(id int(10) primary key auto_increment,name varchar(255)) |
| mysql-bin.000001 | 337 | Intvar | 1 | 365 | INSERT_ID=1 |
| mysql-bin.000001 | 365 | Query | 1 | 471 | use `bin_test`; insert into abc(name) value('zhangsan') |
| mysql-bin.000001 | 471 | Intvar | 1 | 499 | INSERT_ID=2 |
| mysql-bin.000001 | 499 | Query | 1 | 601 | use `bin_test`; insert into abc(name) value('lisi') |
| mysql-bin.000001 | 601 | Intvar | 1 | 629 | INSERT_ID=3 |
| mysql-bin.000001 | 629 | Query | 1 | 733 | use `bin_test`; insert into abc(name) value('wangwu') |
| mysql-bin.000001 | 733 | Query | 1 | 822 | drop database bin_test |
+------------------+-----+-------------+-----------+-------------+-------------------------------------------------------------------------------------------+
10 rows in set (0.00 sec)
可以看到第20條的操作為刪除數據庫的操作。
四、此時數據庫已經被完全破壞
1)使用mysqlbinlog命令進行恢復;首先在恢復前要確認正確的position起始值與終止值:
mysql> show binlog events in "mysql-bin.000001";
+------------------+-----+-------------+-----------+-------------+-------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+-------------+-----------+-------------+-------------------------------------------------------------------------------------------+
| mysql-bin.000001 | 4 | Format_desc | 1 | 106 | Server ver: 5.1.73-log, Binlog ver: 4 |
| mysql-bin.000001 | 106 | Query | 1 | 197 | create database bin_test |
| mysql-bin.000001 | 197 | Query | 1 | 337 | use `bin_test`; create table abc(id int(10) primary key auto_increment,name varchar(255)) |
| mysql-bin.000001 | 337 | Intvar | 1 | 365 | INSERT_ID=1 |
| mysql-bin.000001 | 365 | Query | 1 | 471 | use `bin_test`; insert into abc(name) value('zhangsan') |
| mysql-bin.000001 | 471 | Intvar | 1 | 499 | INSERT_ID=2 |
| mysql-bin.000001 | 499 | Query | 1 | 601 | use `bin_test`; insert into abc(name) value('lisi') |
| mysql-bin.000001 | 601 | Intvar | 1 | 629 | INSERT_ID=3 |
| mysql-bin.000001 | 629 | Query | 1 | 733 | use `bin_test`; insert into abc(name) value('wangwu') |
| mysql-bin.000001 | 733 | Query | 1 | 822 | drop database bin_test |
+------------------+-----+-------------+-----------+-------------+-------------------------------------------------------------------------------------------+
10 rows in set (0.00 sec)
此時我們已經確認“起始值”為創建庫時的起始:106;"終止值"為drop庫時的前一句的結束:733
2)使用mysqlbinlog進行恢復:
mysqlbinlog --no-defaults --start-position="106" --stop-position="733" /var/lib/mysql/mysql-bin.000001 |mysql -uroot -p
Enter password:
3)查看下數據是否恢復成功:
11mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bin_test |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
11mysql> select * from bin_test.abc;
+----+----------+
| id | name |
+----+----------+
| 1 | zhangsan |
| 2 | lisi |
| 3 | wangwu |
+----+----------+
3 rows in set (0.00 sec)
3)恢復成功
【總結】:mysql備份和bin-log日志
備份數據:
mysqldump -uroot -p123456 bin_test -l -F '/tmp/test.sql'
-l:讀鎖(只能讀取,不能更新)
-F:即flush logs,可以重新生成新的日志文件,當然包括log-bin日志
查看binlog日志:
mysql>show master status
如果數據較多,先導入之前備份數據:
mysql -uroot -p123456 bin_test -v -f </tmp/test.sql
-v查看導入的詳細信息
-f是當中間遇到錯誤時,可以skip過去,繼續執行下面的語句
恢復binlog-file二進制日志文件:
mysqlbinlog --no-defaults binlog-file | mysql -uroot -p123456
從某一(567)點開始恢復:
mysqlbinlog --no-defaults --start-position="567" mysql-bin.000001| mysql -uroot -p123456 test
先查好那一點(用more來查看)
[root@localhost mysql]# /usr/bin/mysqlbinlog --no-defaults mysql-bin.000001 --start-position="794" --stop-position="1055" | more
然后恢復:
[root@localhost mysql]# /usr/bin/mysqlbinlog --no-defaults mysql-bin.000001 --start-position="794" --stop-position="1055" | /usr/bin/mysql -uroot -p123456 test
重置binlog日志
mysql> reset master;
Query OK, 0 rows affected (0.01 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 106 | | |
+------------------+----------+--------------+------------------+
mysql> flush logs;#關閉當前的二進制日志文件并創建一個新文件,新的二進制日志文件的名字在當前的二進制文件的編號上加1。
看了以上關于利用Mysqlbinlog日志恢復數據庫數據的操作指引詳細內容,是否有所收獲。如果想要了解更多相關,可以繼續關注我們的行業資訊板塊。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。