mysql binlog from the main mode of operation

Thirteen kinds of models introduced

1.1 Check the main library mysql binlog format

binlog can be set only in the main library, from the library without setting

The default mode is binlog STATEMENT (show variables like '% binlog_format%';)

mysql> 
mysql> show variables like '%binlog_format%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |    #默认ROW
+---------------+-------+

binlog 1.2 mysql database, there are three main ways

statement # the most ancient way, from the sql-based master copy. Binlog which are saved sql statement, a small amount binlog log. But if, as uuid (), then the data will be inconsistent 

row # based on the main line from the means to ensure data consistency, saved binlog which is to change the information. binlog log volume. Ali cloud default in this way (after 5.7 default mode)

Combined and mixed #statement row format, typically with the statement

Twenty-three kinds of model validation

Owners can dynamically change the way replication, without having to restart mysql

2.1 statement mode

The mode to statement

MySQL> = the SET, Ltd. Free Join binlog_format a STATEMENT; # for the current session after session exit will be reduced to a STATEMENT 
Query the OK, 0 rows affected ( 0.00 sec) 

MySQL > = the SET binlog_format of Statement # for the current session, the session will be restored exit into a STATEMENT
     -> ; 
Query the OK, 0 rows affected ( 0.00 sec) 

MySQL > Show Variables like ' %% binlog_format ' ;      
 + ------- + --------------- + ---- 
| variable_name | Value | 
+ --------------- + ----------- + 
| binlog_format | a STATEMENT | 
+ ----- ---------- + ----------- +

New binlog_format mysql configuration file = STATEMENT # permanently replace binlog format statement

[mysqld]
bind-address=0.0.0.0
port=3306
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
skip-name-resolve
slow_query_log=on
long_query_time=1
slow_query_log_file=/data/mysql/mysql-slow.log
innodb-file-per-table=1
innodb_flush_log_at_trx_commit = 2
log_warnings = 1
connect_timeout = 60
net_read_timeout = 120
performance_schema_max_table_instances = 400
server-id = 1
log-bin
binlog_format = STATEMENT

Use the command to view the contents of recorded binlog

mysql> show master logs;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| mater1-bin.000001 |     31050 |
+-------------------+-----------+
1 row in set (0.00 sec)

mysql> flush logs;
Query OK, 0 rows affected (0.01 sec)

mysql> show master logs;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| mater1-bin.000001 |     31098 |
| mater1-bin.000002 |       154 |
+-------------------+-----------+
2 rows in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| darren             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> create database darren1;
Query OK, 1 row affected (0.00 sec)

mysql> use darren1;
Database changed
mysql> create table test (id int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test values (1);
Query OK, 1 row affected (0.01 sec)

mysql> update test set id=3 where id = 1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test;
+------+
| id   |
+------+
|    3 |
+------+
1 row in set (0.00 sec)

mysql> show binlog events in 'mater1-bin.000002';
+-------------------+------+----------------+-----------+-------------+--------------------------------------------------+
| Log_name          | Pos  | Event_type     | Server_id | End_log_pos | Info                                             |
+-------------------+------+----------------+-----------+-------------+--------------------------------------------------+
| mater1-bin.000002 |    4 | Format_desc    |         1 |         123 | Server ver: 5.7.26-log, Binlog ver: 4            |
| mater1-bin.000002 |  123 | Previous_gtids |         1 |         154 |                                                  |
| mater1-bin.000002 |  154 | Anonymous_Gtid |         1 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'             |
| mater1-bin.000002 |  219 | Query          |         1 |         322 | create database darren1                          |
| mater1-bin.000002 |  322 | Anonymous_Gtid |         1 |         387 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'             |
| mater1-bin.000002 |  387 | Query          |         1 |         493 | use `darren1`; create table test (id int)        |
| mater1-bin.000002 |  493 | Anonymous_Gtid |         1 |         558 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'             |
| mater1-bin.000002 |  558 | Query          |         1 |         643 | BEGIN                                            |
| mater1-bin.000002 |  643 | Query          |         1 |         750 | use `darren1`; insert into test values (1)       |
| mater1-bin.000002 |  750 | Xid            |         1 |         781 | COMMIT /* xid=659 */                             |
| mater1-bin.000002 |  781 | Anonymous_Gtid |         1 |         846 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'             |
| mater1-bin.000002 |  846 | Query          |         1 |         931 | BEGIN                                            |
| mater1-bin.000002 |  931 | Query          |         1 |        1044 | use `darren1`; update test set id=3 where id = 1 |
| mater1-bin.000002 | 1044 | Xid            |         1 |        1075 | COMMIT /* xid=660 */                             |
+-------------------+------+----------------+-----------+-------------+--------------------------------------------------+

From the end also sync

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| darren             |
| darren1            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.132.121
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mater1-bin.000002
          Read_Master_Log_Pos: 1075
               Relay_Log_File: slave1-relay-bin.000004
                Relay_Log_Pos: 1290
        Relay_Master_Log_File: mater1-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

row major format change is recorded, statement recorded is real sql statements, insecure data

ROW format data safe, but larger than binlog log, imagine, if you delete, update on millions of lines of words

statement will lead to inconsistent data, such as the following statement

mysql> alter table test add age varchar(50);
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> insert into test values (2,UUID());    #插入一个随机数
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> select * from test;
+------+--------------------------------------+
| id   | age                                  |
+------+--------------------------------------+
|    3 | NULL                                 |
|    2 | 2d64d412-9e72-11e9-a2f9-000c2991dd19 |
+------+--------------------------------------+
从端:
mysql> select * from darren1.test;
+------+--------------------------------------+
| id   | age                                  |
+------+--------------------------------------+
|    3 | NULL                                 |
|    2 | 2d666978-9e72-11e9-8aca-000c2963fd11 |
+------+--------------------------------------+

Find data inconsistencies

2.2 Use ROW format

ysql> delete from test;
Query OK, 2 rows affected (0.01 sec)

mysql> select * from test;
Empty set (0.00 sec)

mysql> insert into test values (2,UUID());
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+------+--------------------------------------+
| id   | age                                  |
+------+--------------------------------------+
|    2 | 74ee4ad0-9e73-11e9-a2f9-000c2991dd19 |
+------+--------------------------------------+
从端:
mysql> select * from darren1.test;
+------+--------------------------------------+
| id   | age                                  |
+------+--------------------------------------+
|    2 | 74ee4ad0-9e73-11e9-a2f9-000c2991dd19 |
+------+--------------------------------------+

Completely concordant

Use mysqlbinlog command to view the row format binlog

[root@master mysql]# mysqlbinlog -vv --base64-output=decode-rows master-bin.000001

# at 1857
#190704 11:50:33 server id 1  end_log_pos 1922 CRC32 0x365a53d7     Anonymous_GTID    last_committed=7    sequence_number=8    rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 1922
#190704 11:50:33 server id 1  end_log_pos 1997 CRC32 0x9d17ec5e     Query    thread_id=133    exec_time=0    error_code=0
SET TIMESTAMP=1562255433/*!*/;
BEGIN
/*!*/;
# at 1997
#190704 11:50:33 server id 1  end_log_pos 2050 CRC32 0x91bb97b1     Table_map: `darren1`.`test` mapped to number 110
AT # 2050 
# 190 704  . 11 : 50 : 33 is Server ID  . 1   end_log_pos 2127 the CRC32 0x8c5aca9e      Write_rows: Table ID  110 the flags: STMT_END_F 
### darren1`.`test` the INSERT the INTO ` 
### the SET 
### @ . 1 = 2  / * Meta = 0 = Nullable the INT is_null to. 1 = 0 * / 
### @ 2 = ' 74ee4ad0-9e73-11e9-a2f9-000c2991dd19 '  / * varString (50) Meta. 1 = 50 = Nullable is_null to = 0 * / # is recorded a change in the value of 
# AT 2127
#190704 11:50:33 server id 1  end_log_pos 2158 CRC32 0x7f33c342     Xid = 695
COMMIT/*!*/;
# at 2158
#190704 11:53:53 server id 1  end_log_pos 2206 CRC32 0xd55138bd     Rotate to mater1-bin.000003  pos: 4
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file

2.3 MIXED mode

binlog mixed format, a small amount of log binlog want, but also to ensure consistent data

Operating the master

mysql> set binlog_format = 'MIXED';
Query OK, 0 rows affected (0.00 sec)
mysql> flush logs;
Query OK, 0 rows affected (0.00 sec)
mysql> create database darren;
Query OK, 1 row affected (0.00 sec)
mysql> use darren;
Database changed
mysql> create table test( id int, name varchar(100) ); 
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test values (1, 'darren');
Query OK, 1 row affected (0.00 sec)

mysql> update test set id = 3 where id = 1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> insert into test values (2, UUID());
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+------+--------------------------------------+
| id   | name                                 |
+------+--------------------------------------+
|    3 | darren                               |
|    2 | b86c02cf-9e74-11e9-a2f9-000c2991dd19 |
+------+--------------------------------------+

 Comparison from the end

mysql> select * from darren.test;
+------+--------------------------------------+
| id   | name                                 |
+------+--------------------------------------+
|    3 | darren                               |
|    2 | b86c02cf-9e74-11e9-a2f9-000c2991dd19 |
+------+--------------------------------------+

Data is also consistent

Logging

mysql> mysql> show binlog events in 'mater1-bin.000004';
+-------------------+------+----------------+-----------+-------------+--------------------------------------------------------------+
| Log_name          | Pos  | Event_type     | Server_id | End_log_pos | Info                                                         |
+-------------------+------+----------------+-----------+-------------+--------------------------------------------------------------+
| mater1-bin.000004 |    4 | Format_desc    |         1 |         123 | Server ver: 5.7.26-log, Binlog ver: 4                        |
| mater1-bin.000004 |  123 | Previous_gtids |         1 |         154 |                                                              |
| mater1-bin.000004 |  154 | Anonymous_Gtid |         1 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                         |
| mater1-bin.000004 |  219 | Query          |         1 |         319 | create database darren                                       |
| mater1-bin.000004 |  319 | Anonymous_Gtid |         1 |         384 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                         |
| mater1-bin.000004 |  384 | Query          |         1 |         508 | use `darren`; create table test( id int, name varchar(100) ) |
| mater1-bin.000004 |  508 | Anonymous_Gtid |         1 |         573 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                         |
| mater1-bin.000004 |  573 | Query          |         1 |         656 | BEGIN                                                        |
| mater1-bin.000004 |  656 | Query          |         1 |         771 | use `darren`; insert into test values (1, 'darren')          |
| mater1-bin.000004 |  771 | Xid            |         1 |         802 | COMMIT /* xid=728 */                                         |
| mater1-bin.000004 |  802 | Anonymous_Gtid |         1 |         867 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                         |
| mater1-bin.000004 |  867 | Query          |         1 |         950 | BEGIN                                                        |
| mater1-bin.000004 |  950 | Query          |         1 |        1063 | use `darren`; update test set id = 3 where id = 1            |
| mater1-bin.000004 | 1063 | Xid            |         1 |        1094 | COMMIT /* xid=729 */                                         |
| mater1-bin.000004 | 1094 | Anonymous_Gtid |         1 |        1159 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                         |
| mater1-bin.000004 | 1159 | Query          |         1 |        1233 | BEGIN                                                        |
| mater1-bin.000004 | 1233 | Table_map      |         1 |        1285 | table_id: 114 (darren.test)                                  |
| mater1-bin.000004 | 1285 | Write_rows     |         1 |        1362 | table_id: 114 flags: STMT_END_F                              |
| mater1-bin.000004 | 1362 | Xid            |         1 |        1393 | COMMIT /* xid=730 */                                         |
+-------------------+------+----------------+-----------+-------------+--------------------------------------------------------------+

According SQL Tuning recording mode

Guess you like

Origin www.cnblogs.com/zyxnhr/p/11135878.html
Recommended