Mysql開啟binlog及查看


mysql的binlog操作

開啟binlog后,數據庫的執行操作都會被記錄到日志中,方便出差錯時,及時回滾。

1、開啟binlog

找到my.ini文件,在[mysqld]后面增加如下兩行:

log-bin=mysql-bin
binlog-format=Row

重啟mysql數據庫

2、檢查binlog開啟狀態:

mysql> show variables like 'log_%';
+----------------------------------------+---------------+
| Variable_name                          | Value         |
+----------------------------------------+---------------+
| log_bin                                | OFF           |
| log_bin_basename                       |               |
| log_bin_index                          |               |
| log_bin_trust_function_creators        | OFF           |
| log_bin_use_v1_row_events              | OFF           |
| log_builtin_as_identified_by_password  | OFF           |
| log_error                              | .\MONSTER.err |
| log_error_verbosity                    | 3             |
| log_output                             | FILE          |
| log_queries_not_using_indexes          | OFF           |
| log_slave_updates                      | OFF           |
| log_slow_admin_statements              | OFF           |
| log_slow_slave_statements              | OFF           |
| log_statements_unsafe_for_binlog       | ON            |
| log_syslog                             | ON            |
| log_syslog_tag                         |               |
| log_throttle_queries_not_using_indexes | 0             |
| log_timestamps                         | UTC           |
| log_warnings                           | 2             |
+----------------------------------------+---------------+
View Code

如果log_bin顯示為ON,則代表已開啟。

3、查看當前正在寫入的binlog文件

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     1357 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

4、查看當前所有的binlog文件

修改或刪除一條表記錄后,觀察日志
運行以下語句可以查看當前記錄的日志名稱,及日志大小,你會發現每當有修改或刪除操作時,日志大小都會增加。

mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size         |
+------------------+-----------+
| mysql-bin.000001 | 1357      |
+------------------+-----------+

5、只查看第一個binlog文件的內容 和查看指定binlog文件的內容

mysql> show binlog events;
+------------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------------------------------------------+
| Log_name         | Pos  | Event_type     | Server_id | End_log_pos | Info                                                                                                      |
+------------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------------------------------------------+
| mysql-bin.000001 |    4 | Format_desc    |         1 |         123 | Server ver: 5.7.28-log, Binlog ver: 4                                                                     |
| mysql-bin.000001 |  123 | Previous_gtids |         1 |         154 |                                                                                                           |
| mysql-bin.000001 |  154 | Anonymous_Gtid |         1 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                      |
| mysql-bin.000001 |  219 | Query          |         1 |         344 | use `test`; CREATE TABLE `test2` (
  `1` int(1) NOT NULL DEFAULT '0'
)                                    |
| mysql-bin.000001 |  344 | Anonymous_Gtid |         1 |         409 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                      |
| mysql-bin.000001 |  409 | Query          |         1 |         481 | BEGIN                                                                                                     |
| mysql-bin.000001 |  481 | Table_map      |         1 |         529 | table_id: 109 (test.test2)                                                                                |
| mysql-bin.000001 |  529 | Write_rows     |         1 |         569 | table_id: 109 flags: STMT_END_F                                                                           |
| mysql-bin.000001 |  569 | Xid            |         1 |         600 | COMMIT /* xid=11 */                                                                                       |
| mysql-bin.000001 |  600 | Anonymous_Gtid |         1 |         665 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                      |
| mysql-bin.000001 |  665 | Query          |         1 |         825 | use `test`; CREATE TABLE `test3` (
  `a` int(1) NOT NULL DEFAULT '0',
  `b` int(1) NOT NULL DEFAULT '0'
) |
| mysql-bin.000001 |  825 | Anonymous_Gtid |         1 |         890 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                      |
| mysql-bin.000001 |  890 | Query          |         1 |         962 | BEGIN                                                                                                     |
| mysql-bin.000001 |  962 | Table_map      |         1 |        1011 | table_id: 110 (test.test3)                                                                                |
| mysql-bin.000001 | 1011 | Write_rows     |         1 |        1055 | table_id: 110 flags: STMT_END_F                                                                           |
| mysql-bin.000001 | 1055 | Xid            |         1 |        1086 | COMMIT /* xid=14 */                                                                                       |
| mysql-bin.000001 | 1086 | Anonymous_Gtid |         1 |        1151 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                      |
| mysql-bin.000001 | 1151 | Query          |         1 |        1223 | BEGIN                                                                                                     |
| mysql-bin.000001 | 1223 | Table_map      |         1 |        1272 | table_id: 110 (test.test3)                                                                                |
| mysql-bin.000001 | 1272 | Update_rows    |         1 |        1326 | table_id: 110 flags: STMT_END_F                                                                           |
| mysql-bin.000001 | 1326 | Xid            |         1 |        1357 | COMMIT /* xid=20 */                                                                                       |
+------------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------------------------------------------+
21 rows in set (0.00 sec)
View Code
mysql> show binlog events in 'mysql-bin.000001';

使用mysqlbinlog查看二進制日志

(一)mysqlbinlog工具介紹

binlog類型是二進制的,也就意味着我們沒法直接打開看,MySQL提供了mysqlbinlog來查看二進制日志,該工具類似於Oracle的logminer。mysqlbinlog用法為

其中,常用的option選項如下:

option 作用
-d , --database=name 只列出指定數據庫的操作
-o , --offset = n 忽略日志前n行
-r , --result-file=name 將輸出的文本格式日志保存到文件
-v 
-vv
-v  :從binlog中重建sql語句
-vv:顯示的SQL語句增加了注釋,可以理解為-v的增強
--start-datetime=datetime
--stop-datetime=datetime
指定日期間隔內的所有日志
--start-position=position
--stop-position=position
指定位置間隔內的所有日志

 

(二)mysqlbinlog使用例子

(1)提取指定的binlog日志

mysqlbinlog /home/mysql/data/ins3308/binlog.000001

(2)提取指定position位置的binlog日志
mysqlbinlog --start-position=120 --stop-position=332 binlog.000001

(3)提取指定position位置的binlog日志並輸出到壓縮文件
mysqlbinlog --start-position=120 --stop-position=332 binlog.000001 |gzip >extra_01.sql.gz

(4)提取指定position位置的binlog日志導入數據庫
mysqlbinlog --start-position=120 --stop-position=332 binlog.000001| mysql -uroot -p

(5)提取指定開始時間的binlog並輸出到日志文件
mysqlbinlog --start-datetime="2017-08-14 10:00:00" binlog.000001--result-file=extra02.sql

(6)提取指定位置的多個binlog日志文件
mysqlbinlog --start-position=120 --stop-position=332 binlog.000001 binlog.000002|more

(7)提取指定數據庫binlog並轉換字符集到UTF8
mysqlbinlog --database=test --set-charset=utf8 binlog.000001 binlog.000002 >test.sql

(8)遠程提取日志,指定結束時間 
mysqlbinlog -uroot -p -h192.168.10.02 -P3306 --stop-datetime="2017-08-14 10:00:00" --read-from-remote-server binlog.000001 |more

 

(9)遠程提取使用row格式的binlog日志並輸出到本地文件

mysqlbinlog -uroot -p -P3308 -h192.168.10.02 --read-from-remote-server -vv binlog.000001 >row.sql

 案例:

 mysqlbinlog --base64-output=decode-rows -vv  .\mysql-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#201224 13:55:03 server id 1  end_log_pos 123 CRC32 0x7277df22  Start: binlog v 4, server v 5.7.28-log created 201224 13:55:03 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
# at 123
#201224 13:55:03 server id 1  end_log_pos 154 CRC32 0xb076cda3  Previous-GTIDs
# [empty]
# at 154
#201224 14:07:05 server id 1  end_log_pos 219 CRC32 0x940c4fcf  Anonymous_GTID  last_committed=0        sequence_number=1       rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 219
#201224 14:07:05 server id 1  end_log_pos 344 CRC32 0x52b87213  Query   thread_id=3     exec_time=0     error_code=0
use `test`/*!*/;
SET TIMESTAMP=1608790025/*!*/;
SET @@session.pseudo_thread_id=3/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1344274432/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C gbk *//*!*/;
SET @@session.character_set_client=28,@@session.collation_connection=28,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
CREATE TABLE `test2` (
  `1` int(1) NOT NULL DEFAULT '0'
)
/*!*/;
# at 344
#201224 14:07:05 server id 1  end_log_pos 409 CRC32 0x6b4a8cc7  Anonymous_GTID  last_committed=1        sequence_number=2       rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 409
#201224 14:07:05 server id 1  end_log_pos 481 CRC32 0x222ab41e  Query   thread_id=3     exec_time=0     error_code=0
SET TIMESTAMP=1608790025/*!*/;
BEGIN
/*!*/;
# at 481
#201224 14:07:05 server id 1  end_log_pos 529 CRC32 0xec90b509  Table_map: `test`.`test2` mapped to number 109
# at 529
#201224 14:07:05 server id 1  end_log_pos 569 CRC32 0xf08256ae  Write_rows: table id 109 flags: STMT_END_F
### INSERT INTO `test`.`test2`
### SET
###   @1=1 /* INT meta=0 nullable=0 is_null=0 */
# at 569
#201224 14:07:05 server id 1  end_log_pos 600 CRC32 0x932732d7  Xid = 11
COMMIT/*!*/;
# at 600
#201224 14:11:42 server id 1  end_log_pos 665 CRC32 0x454a006d  Anonymous_GTID  last_committed=2        sequence_number=3       rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 665
#201224 14:11:42 server id 1  end_log_pos 825 CRC32 0xc75a0c24  Query   thread_id=3     exec_time=0     error_code=0
SET TIMESTAMP=1608790302/*!*/;
CREATE TABLE `test3` (
  `a` int(1) NOT NULL DEFAULT '0',
  `b` int(1) NOT NULL DEFAULT '0'
)
/*!*/;
# at 825
#201224 14:11:42 server id 1  end_log_pos 890 CRC32 0x010aa555  Anonymous_GTID  last_committed=3        sequence_number=4       rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 890
#201224 14:11:42 server id 1  end_log_pos 962 CRC32 0x54d9f256  Query   thread_id=3     exec_time=0     error_code=0
SET TIMESTAMP=1608790302/*!*/;
BEGIN
/*!*/;
# at 962
#201224 14:11:42 server id 1  end_log_pos 1011 CRC32 0x735e8b7c         Table_map: `test`.`test3` mapped to number 110
# at 1011
#201224 14:11:42 server id 1  end_log_pos 1055 CRC32 0x60ddce64         Write_rows: table id 110 flags: STMT_END_F
### INSERT INTO `test`.`test3`
### SET
###   @1=1 /* INT meta=0 nullable=0 is_null=0 */
###   @2=1 /* INT meta=0 nullable=0 is_null=0 */
# at 1055
#201224 14:11:42 server id 1  end_log_pos 1086 CRC32 0x64816cb5         Xid = 14
COMMIT/*!*/;
# at 1086
#201224 14:13:33 server id 1  end_log_pos 1151 CRC32 0x81a76dba         Anonymous_GTID  last_committed=4        sequence_number=5       rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 1151
#201224 14:13:33 server id 1  end_log_pos 1223 CRC32 0xf626162b         Query   thread_id=3     exec_time=0     error_code=0
SET TIMESTAMP=1608790413/*!*/;
BEGIN
/*!*/;
# at 1223
#201224 14:13:33 server id 1  end_log_pos 1272 CRC32 0x62f1ec4a         Table_map: `test`.`test3` mapped to number 110
# at 1272
#201224 14:13:33 server id 1  end_log_pos 1326 CRC32 0xd84d3f41         Update_rows: table id 110 flags: STMT_END_F
### UPDATE `test`.`test3`
### WHERE
###   @1=1 /* INT meta=0 nullable=0 is_null=0 */
###   @2=1 /* INT meta=0 nullable=0 is_null=0 */
### SET
###   @1=1 /* INT meta=0 nullable=0 is_null=0 */
###   @2=2 /* INT meta=0 nullable=0 is_null=0 */
# at 1326
#201224 14:13:33 server id 1  end_log_pos 1357 CRC32 0xb1b4d8b0         Xid = 20
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM