-----------------------------------------------------------------------------------------------------------------
Disclaimer: This article is CSDN blogger " lonely smoke original article", the reprint please attach the original source link and statements.
Original link: https: //www.cnblogs.com/rjzheng/p/9721765.html
-----------------------------------------------------------------------------------------------------------------
introduction
Why write this article?
Then when I learn MySQL, in order to be able to quickly obtain employment, usually learn about basic MySQL syntax, almost on coming out looking for work. Slightly better level of children's shoes to understand it will be a little write stored procedures, or is it to understand and use it to create the index. But then, I think we all learn to ignore the underlying knowledge. why? Well because of work rarely used. And then, much of this knowledge underlying the spread of the market, and more biased operation and maintenance, research and development know so much sense is not too large, a lot of knowledge in this life may never use.
So, I put together a portion of the relevant knowledge, we hope to gain something.
R & D to understand exactly what?
It divided into two parts
- binlog related concepts
- How to resolve binlog
Program was divided into two parts narrative. The part about the binlog of the concepts of knowledge of this part, we do not know as operation and maintenance so deep, I'll list some common concepts and common configuration, we hurried glance, an idea can be. After all this time dimension and discuss issues of transport, it will not look ignorant force. As the saying goes
Ignorant ignorant to force to force the tree fruit, tree ignorant to force you and me.
Ignorant force are sitting before a tree, a man forced a muddled fruit.
A blogger who quietly taken away if forced to rip off exclusive enough, or you readers know something about the basic concepts, and to facilitate future operation and maintenance of communication. The lower half say how to resolve binlog .
In addition, this article is to look at the big R & D, there may be some understanding of the concepts I do not pair, operation and maintenance are significantly lighter spray.
text
Remember my "definition, two misunderstanding, use three, four common sense."
A definition
Start with start with the definition of
binlog database table is a record of all structural changes (e.g. the CREATE, the ALTER TABLE ...) and modifying table data ( the INSERT , the UPDATE , the 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.
Say one more thing, if the update
operation does not cause data changes, it will also be credited binlog
.
Two misconceptions
Misunderstanding number one: binlog just a kind of log file records the contents of the operation
as binlog
called binary log, a lot of research and development will put this binary log and we usually write code in the code logs together. Because our Code Log , only one type of record operating capacity of the file does not contain an index file. However, the binary log includes two types of files:
- 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.
So there may be a little abstract, assuming the file my.cnf
there are so configured three
log_bin: ON Open binlog log log_bin_basename: bin path and file name prefix ( / var / log / MySQL / MySQL - bin) log_bin_index: bin file index ( / var / log / MySQL / MySQL - . bin index )
Then you will file in the directory /var/log/mysql/
found in the following two files mysql-bin.000001
and mysql-bin.index
. mysql-bin.index
What we call the index file, open Chou Chou, the content is below record which files are log files.
./mysql-bin.000001
So when it comes to the log file. In innodb
in fact, it can be divided in the cache on the disk part two parts, one part. Here industry has a term called the brush plate , refers to the log cache to disk brush. With the brush plate associated with two parameters: 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 large, it will be a memory waste.binlog_cache_size
Set too small, frequently log buffer is written to a temporary file. Specifically how to set up, are interested in their own inquiries, I did not think much research and development opportunities to set this value, you can understand. - (2)
sync_binlog=0
: Indicates the refreshbinlog
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 will refresh every binlog to disk when the transaction commits.sync_binlog=N
, Will be submitted on behalf of the transaction once every N binlog refresh.
In addition, there is a consistency here, sync_binlog=N
the database when the operating system is down, it may not synchronize data to disk, and then restart the database again, it will bring data loss problems.
When the sync_binlog=1
transaction at commit
the time the data is written 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. The transaction log and binlog
the issue of consistency, we can query mysql internal XA protocol, which is to solve this problem of consistency.
Misunderstanding number two: binlog is InnoDb uniquebinlog
is recorded in the form of events, these words popular point that is, binlog
the content is one of the events. This specific I would say the next one, keep in mind this binlog
is the content of one event on the line.
Note, the term used here, is an event, not the transaction. Everyone should know Innodb
and mysiam
most significant difference is the support of a transaction, a transaction is not supported.
So you can say, binlog
based on transactions to record binary log, for example sync_binlog=1
, each committing a transaction, they write binlog
. You can not say binlog
that transaction log binlog
records not only innodb
logs in myisam
, the same existence binlog
.
Three purposes
These three uses, from "MySQL InnoDB storage engine technology insider," a book, were restored , copied , audited . These three applications, developed much they can look at, such as data recovery, you run into colleagues the opportunity to delete the library is too small. If there really is a colleague of self-sacrifice, you run the risk of leaving to provide for data recovery opportunities, a lot of operation and maintenance engineers on standby at that, not your's. So, these three functions can understand.
Restore : Here there are a lot of articles online to guide you how to use binlog
the log to recover the database data. If you really feel great time on their own to create a library, then deleted, go look at data recovery, practice your hand now.
Copy : As shown (not the picture painted himself, lazy)
Primary library has a log dump
thread, to binlog
pass from the library
from the library has two threads, one I / O thread, a thread SQL, I / O thread reads the primary library pass over the binlog
content and to write relay log
, the SQL thread from relay log
reading the inside take the contents into the database from the library.
Audit : Users can be audited by the binary log information, determine whether the database injection attacks.
Four common sense
A common sense: binlog format common
piece of knowledge I use a table to represent, you did not need a lot of long-winded.
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 copied or can not accurately replicate some system functions, 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 |
The industry is currently recommended row
mode, high accuracy, although large files, but now there are SSD and 10-Gigabit fiber-optic network, which network disk IO and IO are acceptable.
So, we must ask, why is not recommended mixed
mode for the following reasons
assume that there are two master record, only one record and slave.
The master data
+----+------------------------------------------------------+ | id | n | +----+------------------------------------------------------+ | 1 | d24c2c7e-430b-11e7-bf1b-00155d016710 | | 2 | ddd | +----+------------------------------------------------------+
slave of data
+----+-------------------------------------------------------+ | id | n | +----+-------------------------------------------------------+ | 1 | d24c2c7e-430b-11e7-bf1b-00155d016710 | +----+-------------------------------------------------------+
When master
a recording from the library does not exist, which is updated on id=2
a record, you will find that master
they can be implemented successfully. And slave
after get this SQL, will be performed as usual, do not report any abnormalities, but the update does not affect the number of lines only. And you execute the command show slave status
, view the output, you will find no abnormalities. However, if you are a row
model, because the line does not exist, it will report 1062 errors.
Common sense two: how to view the binlogbinlog
itself is a kind of binary files. Binary files more space, write faster, is not directly open to view.
Therefore mysql provides a command mysqlbinlog
for viewing.
General statement
binary file format, you can use the following command
mysqlbinlog mysql-bin.000001
If row
format, plus -v
or -vv
parameters of the line, such as
mysqlbinlog -vv mysql-bin.000001
Common sense three: how to delete binlog
delete binlog
many ways, there are three are common
(1) use reset master
, this command will delete all the logs and let the log file restarts from 000001.
(2) use the command
PURGE { BINARY | MASTER } LOGS { TO 'log_name' | BEFORE datetime_expr }
E.g
purge master logs to "binlog_name.00000X"
It will clear all log files before 00000X.
(3) Use --expire_logs_days=N
option to specify how many days the log automatically expire emptied.
Common sense four: binlog common parameters
common parameters are listed below, there is a fine impression.
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 |
Questions
Excuse me, I said
- A definition
- Two misconceptions
- Three purposes
- Four common sense
You say what is it?
In addition, I will be the next one are introduced, how to use code analysis binlog
logs.
Author: Lonely smoke Source: HTTP: // rjzheng.cnblogs.com / This article belongs to the author and blog Park total, welcome to reprint, but without the author's consent declared by this section must be retained, and given the original connection in the apparent position of the article page, otherwise the right to pursue legal responsibilities. If you feel that there is help, you can tap the bottom right corner [Recommended].