Linux学習ノート:mysqlbinlog


まず、デフォルトではbinlog機能は開いていないので、myを変更する必要があります.cnfが開く
[root@vmtest ~]# grep bin/disk2/mysql_multi_instances/3308/my.cnf
log-bin =/disk2/mysql_multi_instances/3308/mysql-bin<<最も重要なのはこのコマンドをオンにすることであり、[mysqld]の下に書かなければならない.
binlog_cache_size = 1M
max_binlog_cache_size = 1M
max_binlog_size = 2M
binlogの設定値を表示します.
[root@lab ~]# mysql -uroot -pjet-speed -e "show variables;"|grep bin
次に、災害復旧のシミュレーション
ライブラリの内容は以下の通りです.
mysql> select * from student;
+----+-----------+-------+
| id | name      | score |
+----+-----------+-------+
|  1 | coosh     |   100 |
|  2 | mingming  |    90 |
|  3 | xiaozhang |    60 |
|  4 | mama      |    63 |
+----+-----------+-------+
4 rows in set (0.00 sec)

フルライブラリバックアップ
[root@lab ~]# mysqldump -uroot -pjet-speed -B mydb -F -x --events --master-data=1|gzip >mydb.sql.gz
studentテーブルに3つのデータを挿入します.
mysql> insert into student (name,score) values('hsooc',100), ('ginmginm',90), ('amam',63);                         
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
studentテーブル全体を削除します
mysql> drop table student;
Query OK, 0 rows affected (0.00 sec)
災害復旧の開始
バックアップファイルを確認してください.sql.gz
[root@lab ~]# zcat mydb.sql.gz                           
-- MySQL dump 10.13  Distrib 5.1.71, for redhat-linux-gnu (x86_64)
--
-- Host: localhost    Database: mydb
-- ------------------------------------------------------
-- Server version       5.1.71-log
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Position to start replication or point-in-time recovery from
--
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000005', MASTER_LOG_POS=106;
【たくさんの内容】
上の内容の最後の文は、--master-data=1の効果で、DBAに注意して、次のSQLポイントの位置はファイルmysql-binです.1000005では、106に位置しています.
まず、このバックアップファイルのデータを復元し、mysqlbinlogを使用してインクリメンタルリカバリを行います.
[root@lab ~]# gzip -d mydb.sql.gz<<テキストファイルmydbを得る.sql
[root@lab ~]# mysql -uroot -p mydb [root@lab ~]# mysql -uroot -p -e "use mydb;select * from student;"
+----+-----------+-------+
| id | name      | score |
+----+-----------+-------+
|  1 | coosh     |   100 |
|  2 | mingming  |    90 |
|  3 | xiaozhang |    60 |
|  4 | mama      |    63 |
+----+-----------+-------+

4番目のレコードに復元されました
binlogファイルの表示
[root@lab ~]# mysqlbinlog -d mydb /var/lib/mysql/mysql-bin.000005|grep -B 20 'drop table'  
# at 106
#160122 22:16:13 server id 1  end_log_pos 134   Intvar
SET INSERT_ID=5/*!*/;
# at 134
#160122 22:16:13 server id 1  end_log_pos 279   Query   thread_id=12    exec_time=0     error_code=0
use `mydb`/*!*/;
SET TIMESTAMP=1453472173/*!*/;
SET @@session.pseudo_thread_id=12/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=0/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C latin1 *//*!*/;
SET @@session.character_set_client=8,@@session.collation_connection=8,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
insert into student (name,score) values('hsooc',100), ('ginmginm',90), ('amam',63)
/*!*/;
# at 279
#160122 22:17:05 server id 1  end_log_pos 360   Query   thread_id=12    exec_time=0     error_code=0
SET TIMESTAMP=1453472225/*!*/;
drop table student

-dを使用して、指定したデータベースの内容をフィルタ出力します.drop table studentはat 279の位置であり、前の位置134は私たちが回復する点であることがわかります.つまり、私たちの回復点は106から134です.
[root@lab ~]# mysqlbinlog -d mydb /var/lib/mysql/mysql-bin.000005 --start-position=106 --stop-position=279 >mydb.binlog.sql
[root@lab ~]# less mydb.binlog.sql 
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#160122 22:13:53 server id 1  end_log_pos 106   Start: binlog v 4, server v 5.1.71-log created 160122 22:13:53
# Warning: this binlog is either in use or was not closed properly.
BINLOG '
ITmiVg8BAAAAZgAAAGoAAAABAAQANS4xLjcxLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC
'/*!*/;
# at 106
#160122 22:16:13 server id 1  end_log_pos 134   Intvar
SET INSERT_ID=5/*!*/;
# at 134
#160122 22:16:13 server id 1  end_log_pos 279   Query   thread_id=12    exec_time=0     error_code=0
use `mydb`/*!*/;
SET TIMESTAMP=1453472173/*!*/;
SET @@session.pseudo_thread_id=12/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=0/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C latin1 *//*!*/;
SET @@session.character_set_client=8,@@session.collation_connection=8,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
insert into student (name,score) values('hsooc',100), ('ginmginm',90), ('amam',63)
/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
mydb.binlog.sql (END)

インポートして表示し、正常に復元されました.
[root@lab ~]# mysql -uroot -p mydb <mydb.binlog.sql               
[root@lab ~]# mysql -uroot -p -e "use mydb;select * from student;"
+----+-----------+-------+
| id | name      | score |
+----+-----------+-------+
|  1 | coosh     |   100 |
|  2 | mingming  |    90 |
|  3 | xiaozhang |    60 |
|  4 | mama      |    63 |
|  5 | hsooc     |   100 |
|  6 | ginmginm  |    90 |
|  7 | amam      |    63 |
+----+-----------+-------+