表结构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=======