表結構html
CREATE TABLE `t` ( `id` int(11) NOT NULL AUTO_INCREMENT, `col1` int(10) unsigned DEFAULT NULL, `col2` varchar(45) DEFAULT NULL, `col3` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
mysql 版本mysql
➜ ~ mysql -V mysql Ver 14.14 Distrib 5.7.15, for osx10.11 (x86_64) using EditLine wrapper
mysql的安裝請看https://my.oschina.net/xinxingegeya/blog/751154sql
啓動mysql的binlog,在配置文件中添加,數據庫
[mysqld] # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. log_bin = mysql-bin # These are commonly set, remove the # and set as required. basedir = /usr/local/mysql datadir = /usr/local/mysql/data port = 3306 server_id = 001
從新啓動MySQL。緩存
二進制日誌(bingary log)記錄了對MySQL數據執行更改的全部操做,可是不包括select 和 show 這類操做,由於這類操做對數據自己並無修改。然而,若操做自己沒有致使數據庫發生變化,那麼該操做可能也會寫入二進制日誌。能夠經過如下命令來查看二進制日誌,session
mysql> show master status \G; *************************** 1. row *************************** File: mysql-bin.000002 Position: 27954 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec) ERROR: No query specified mysql> show binlog events in 'mysql-bin.000002' \G ;
For replication, the binary log on a master replication server provides a record of the data changes to be sent to slave servers. The master server sends the events contained in its binary log to its slaves, which execute those events to make the same data changes that were made on the master.app
Certain data recovery operations require use of the binary log. After a backup has been restored, the events in the binary log that were recorded after the backup was made are re-executed. These events bring databases up to date from the point of the backup.ide
max_binlog_cache_size
binlog_cache_size
當使用InnoDB時,全部未提交的二進制日誌會被記錄到一個緩存中去,等該事務提交時直接將緩存中的二進制日誌寫入二進制日誌文件,而該緩存的大小由binlog_cache_size決定,默認大小爲32K。此外binlog_cache_size 是基於會話的,也就是說當一個線程開始一個事務時,mysql會自動分配一個大小爲binlog_cache_size 的緩存,所以該值的設置須要小心,不能設置過大。當一個事務的記錄大於設定的binlog_cache_size 時,mysql會把緩存中的日誌寫入一個臨時文件中,所以該值又不能設置的過小。 能夠經過狀態變量binlog_cache_use和binlog_cache_disk_use來幫助測試。工具
binlog_cache_use:使用二進制日誌緩存的事務數量
binlog_cache_disk_use:使用二進制日誌緩存但超過binlog_cache_size值並使用臨時文件來保存事務中的語句的事務數量
max_binlog_size
指定了單個二進制日誌文件的最大值,若是超過該值,則產生新的二進制日誌文件,後綴名+1,並記錄到.index 文件。該設置並不能嚴格控制binlog的大小,尤爲是binlog比較靠近最大值而又遇到一個比較大事務時,爲了保證事務的完整性,不可能作切換日誌的動做,只能將該事務的全部SQL都記錄進當前日誌,直到事務結束性能
sync_binlog
這個參數直接影響mysql的性能和完整性 sync_binlog=0: 當事務提交後,Mysql僅僅是將binlog_cache中的數據寫入Binlog文件,但不執行fsync之類的磁盤同步指令,通知文件系統將緩存刷新到磁盤,而讓filesystem自行決定何時來作同步,這個是性能最好的。 sync_binlog=n,在進行n次事務提交之後,mysql將執行一次fsync之類的磁盤同步指令,同志文件系統將Binlog文件緩存刷新到磁盤。 mysql中默認的設置是sync_binlog=0,即不做任何強制性的磁盤刷新指令,這時性能是最好的,但風險也是最大的。一旦系統crash,在文件系統緩存中的全部binlog信息都會丟失
STATEMENT causes logging to be statement based. ROW causes logging to be row based. MIXED causes logging to use mixed format.
The server uses several logging formats to record information in the binary log. The exact format employed depends on the version of MySQL being used. There are three logging formats:
Replication capabilities in MySQL originally were based on propagation of SQL statements from master to slave. **_This is called statement-based logging._** You can cause this format to be used by starting the server with --binlog-format=STATEMENT.
In row-based logging, the master writes events to the binary log that indicate how individual table rows are affected. It is important therefore that tables always use a primary key to ensure rows can be efficiently identified. You can cause the server to use row-based logging by starting it with --binlog-format=ROW.
A third option is also available: mixed logging. With mixed logging, statement-based logging is used by default, but the logging mode switches automatically to row-based in certain cases as described below. You can cause MySQL to use mixed logging explicitly by starting mysqld with the option --binlog-format=MIXED.
Prior to MySQL 5.7.7, statement-based logging format was the default. In MySQL 5.7.7 and later, row-based logging format is the default.
要查看二進制日誌文件的內容,必須經過mysql提供的工具mysqlbinlog。
當binlog_format設置爲STATEMENT時,
binlog_format = STATEMENT
以下方式使用mysqlbinlog查看二進制日誌,
mysql> update t set col1 = col1+1 where id = 50; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> show master status \G; *************************** 1. row *************************** File: mysql-bin.000005 Position: 443 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec) ERROR: No query specified mysql> show binlog events in 'mysql-bin.000005' \G ; ..... ..... *************************** 5. row *************************** Log_name: mysql-bin.000005 Pos: 298 Event_type: Query Server_id: 1 End_log_pos: 412 Info: use `test`; update t set col1 = col1+1 where id = 50
第五行是一個statement,pos爲298,根據這些信息可使用mysqlbinlog來查看具體的信息,
➜ mysql bin/mysqlbinlog --no-defaults --start-position=298 --stop-position=412 ./data/mysql-bin.000005 /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #160925 16:56:47 server id 1 end_log_pos 123 CRC32 0x0856521a Start: binlog v 4, server v 5.7.15-log created 160925 16:56:47 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; BINLOG ' T5HnVw8BAAAAdwAAAHsAAAABAAQANS43LjE1LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAABPkedXEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA ARpSVgg= '/*!*/; # at 298 #160925 16:57:46 server id 1 end_log_pos 412 CRC32 0x8752a208 Query thread_id=2 exec_time=0 error_code=0 use `test`/*!*/; SET TIMESTAMP=1474793866/*!*/; SET @@session.pseudo_thread_id=2/*!*/; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/; SET @@session.sql_mode=1075838976/*!*/; SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/; /*!\C utf8 *//*!*/; SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/; SET @@session.lc_time_names=0/*!*/; SET @@session.collation_database=DEFAULT/*!*/; update t set col1 = col1+1 where id = 50 /*!*/; 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*/;
引用和參考:
http://dev.mysql.com/doc/refman/5.7/en/binary-log.html
http://dev.mysql.com/doc/internals/en/binary-log.html
=======END=======