Mysql's binlog

https://yq.aliyun.com/articles/669842

binlog related concepts

binlog database table is a record of all structural changes (e.g., CREATE, ALTER TABLE ...) and table data modifications (INSERT, UPDATE, DELETE ...) binary log.
binlog not record SELECT and SHOW this type of operation, because such operations on the data itself is not modified, but you can view all statements that MySQL executed by querying the general log.
binlog binary log file types include:

  • Which log files index file (the file name suffix .index) being used for recording
  • Log file (file name suffix .00000 *) database to record all DDL and DML (Data query in addition to) the statement event.

Assuming that the file my.cnf there are so configured three

log_bin: on open binlog log
log_bin_basename: bin file path and name prefix (/ var / log / MySQL-bin)

log_bin_index: bin file index (/var/log/mysql/mysql-bin.index)

We will find two files mysql-bin.000001 and mysql-bin.index in the file directory / var / log / mysql / below

mysql-bin.index is what we call the index file. Records which files are log files, reads as follows:

./mysql-bin.000001

The log file is divided into two parts innodb, in the cache on the disk part by part. The industry has a word called brush plate, refers to the log cache to disk brush. Brush pan with the relevant parameter has two: sync_binlog and binlog_cache_size effect of these two parameters as follows:

binlog_cache_size: log cache size of the binary portion, the default value of 32K

sync_binlog = [N]: indicates how many times the write buffer, the disc brush, the default value is 0

Note two things:

  • 1) binlog_cache_size set too high, it will result in wasted memory. Under binlog_cache_size set too, frequently log buffer is written to a temporary file.
  • 2) sync_binlog = 0: indicate refresh binlog point in time is determined by the operating system itself, the operating system itself will periodically refreshes the cached data to disk, the best performance.

    sync_binlog = 1, representatives of each will refresh binlog to disk when the transaction commits. sync_binlog = N, N representing each transaction will be submitted to a binlog refresh. sync_binlog = N, database downtime when the operating system may not synchronize data to disk, and then restart the database again, will bring data loss problems.

    - When sync_binlog = 1, when the Commit transaction, data is written to the binlog, but not yet written to the transaction log (redo log and undo log). At this time, downtime, restart the database, the data is rolled back. But binlog already recorded, there is inconsistency here. This transaction logs and binlog issue of consistency, we can query mysql internal XA protocol , the protocol is to solve this problem of consistency.

binlog event is recorded in the form. The most notable difference is Innodb and mysiam support a transaction, a transaction is not supported. binlog innodb logs record not only in myisam, there is also binlog same.

Three purposes: recovery, replication, audit

Recovery :

Copy :

graph LR
write-->Master
Master-->Slave

Main library has a log dump thread will pass a binlog from the library. There are two threads from the library, an I / O thread, a thread SQL, I / O thread reads the primary library binlog pass over the content and writes the relay log, inside the SQL thread reads the content from the relay log, written from database library

audit : the user can be audited by the binary log information, determine whether the database injection attacks.

Four common sense

A common sense: binlog common format

format definition advantage Shortcoming
statement Record is to modify the SQL statement Small log files, saving IO, to improve performance Poor accuracy, can not be transferred for some system functions, but can not copy or replication, such as now (), uuid (), etc.
row The record is changed for each row of actual data Accuracy, able to accurately replicate the changed data Large log files, a larger network and disk IO IO
mixed Statement row and mixed mode Strong accuracy, file size moderate There may occur from the main inconsistencies

Currently the industry is respected row mode

Common sense two: how to view the binlog

binlog itself is a kind of binary files. Binary files more space, write faster, it is not directly open to view.

So mysql command provides mysqlbinlog to view it.

General statement binary file format, you can use the following command

mysqlbinlog mysql-bin.000001

If a row format, with the -v or -vv line parameters, such as

mysqlbinlog -vv mysql-bin.000001

Common sense three: how to delete binlog

  1. Use reset master, this command will delete all the logs and let the log file restarts from 000001.
  2. Use the command
PUGER {BINARY | MASTER} LOGS {TO 'log_name' | BEFORE datetime_expr}

E.g

purge master logs to "binlog_name.00000X"

Will erase all the log files before 00000X
3) Use expire_logs_days = N option to specify how many days the log automatically expire cleared

Common sense four: binlog common parameters

parameter name meaning
log_bin={on,off,base_name} Specifies whether to enable recording binary log or specify a log path
sql_log_bin={on,off} Specifies whether to record the binary log enabled
expire_logs_days Specified time automatically deleted binary log that logs the expiration time
log_bin_index Specifies the path to the file mysql-bin.index
binlog_format={mixed,row,statement} Specifies the binary log records based on what mode
max_binlog_size Specifies the maximum binary log file
binlog_cache_size Specify transaction log buffer size
max_binlog_cache_size Specifies the maximum size for binary log cache
sync_binlog = {0/n} Specify how many times the write buffer, brush plate

Guess you like

Origin blog.csdn.net/licheng989/article/details/90054957