MySQL log - Binary Log

(A) What is the binary log

Binary log (binlog) records all DDL and DML operations in the database, but does not include select statements, statements saved as "events" recorded change database changes, play in the Lord's recovery from replication (replication) and data an important role. For comparison, MySQL binary log acts as redo log + archive log Oracle database.

 

(B) open the binary log and configuration parameters

Binary log when you need to open the configuration parameters in the configuration file inside, and then restart the database or open the database to add the relevant parameters. The main parameters are as follows:

(2.1)log_bin

This parameter controls binlog file storage location, the usage log_bin = path / file_name. If no path, the default path for the data file storage address (datadir), if no file name is given, the default file name is "host name -bin.num". The initial document is hostname-bin-000001, each time you start MySQL service or refresh, auto-increment numbering create a binary log file.

(2.1)binlog_format

This parameter controls the binary log file format, optional formats:

  • STATEMENT: log record is executed SQL statement, a small amount of logs, but there will be master-slave replication inconsistency
  • ROW: changes in each row of data logging, the log volume, the main benefit is that the situation does not appear inconsistent from copying data, recommended this way
  • MIXED: STATEMENT combined log format and ROW.

(2.3)expire_logs_days

This parameter controls the number of days to retain binary logs, over a specified number of days, the log will be automatically deleted.

(2.4)max_binlog_size

This parameter controls the size of the binary log file when the log file reaches the size specified parameters, creates a new binary log file. However, in practice binary log files may exceed this value, such as when the binary log is almost full, the implementation of a large thing, because the things that determine the characteristics related events must be continuous. In this case, the event must be written with a log file, max_binlog_size value greater than log phenomenon occurs.

(2.5)binlog_do_db

This parameter controls the database you want to save the binary log, if there are multiple databases, this parameter is used more than once, can not be separated by commas.

image

(2.6)binlog_ignore_db

This parameter controls without saving the binary log database, if there are multiple databases, this parameter is used more than once, can not be separated by commas.

(2.7)sync_binlog

This parameter controls the frequency of the binary log flush to disk, in seconds (s).

(2.8)server_id

If you want to open binlog in MySQL5.7 version, you must set the parameters, or instances can not start.

 

A simple parameter configuration shown in FIG:

image

 

(C) the binary log using mysqlbinlog View

binlog type is binary, which means we can not directly open look, MySQL provides mysqlbinlog to see the binary log, the tool is similar to Oracle's LogMiner . mysqlbinlog usage is

[root@masterdb binlog]# mysqlbinlog

Usage: mysqlbinlog [options] log-files

Among them, the commonly used option options are as follows:

option effect
-d , --database=name List only the operation of the specified database
-o , --offset = n Ignore the first n log line
-r , --result-file=name Save the text format log output to a file
-v
-vv
-v: binlog rebuild sql statement from the
-vv: display the SQL statement added comments can be understood as the enhancement -v
--start-datetime=datetime
--stop-datetime=datetime
All logs in the specified date interval
--start-position=position
--stop-position=position
All logs in the specified location spaced

Example 1: Use --start-datetime and --stop-datetime show in 2020 at 23:30 on February 12 2020 at 23:50 on February 12 log

[root@masterdb binlog]# mysqlbinlog master-bin.000025 --start-datetime='2020-02-12 23:30:00' --stop-datetime='2020-02-12:23:50:00'
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#200212 23:40:10 server id 1  end_log_pos 123 CRC32 0xfc4fd0fc     Start: binlog v 4, server v 5.7.24-log created 200212 23:40:10 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
WhxEXg8BAAAAdwAAAHsAAAABAAQANS43LjI0LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAABaHEReEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AfzQT/w=
'/*!*/;
# at 123
#200212 23:40:10 server id 1  end_log_pos 154 CRC32 0x3dc8c184     Previous-GTIDs
# [empty]
# at 154
#200212 23:40:46 server id 1  end_log_pos 219 CRC32 0x12bba458     Anonymous_GTID    last_committed=0    sequence_number=1    rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 219
#200212 23:40:46 server id 1  end_log_pos 315 CRC32 0x5ed83729     Query    thread_id=3    exec_time=0    error_code=0
use `db1`/*!*/;
SET TIMESTAMP=1581522046/*!*/;
SET @@session.pseudo_thread_id=3/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1436549152/*!*/;
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/*!*/;
create table t1(id int )
/*!*/;
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*/;

 

The role of (four) binary log

(4.1) Copy

MySQL Master end of binary log to a slave terminal, in accordance with slave redo logs, to achieve the purpose of replication from the master.

(4.2) recovery

Whether you use mysqldump or xtrabackup, we can only restore a database to backup the moment, if you want to restore the database to any point in time, you need to use the binary log.

 

(E) related commands

(5.1) binlog cleanup

If a larger volume of business, binlog log is growing rapidly, the need for regular cleaning, there are several ways to clean up the log.

Method 1 : Perform "reset master" command, which will delete all binlog log new log number from "000001" to start

mysql> reset master;
Query OK, 0 rows affected (0.00 sec)

Method 2 : Perform the log before the "purge master logs to 'hostname- bin ******.'" Command, which will remove ******

# Delete 'master-bin.000007' log before 
 
[root@masterdb binlog]# ls -lrt
total 36
-rw-r----- 1 mysql mysql 202 Feb 13 00:20 master-bin.000001
-rw-r----- 1 mysql mysql 202 Feb 13 00:20 master-bin.000002
-rw-r----- 1 mysql mysql 202 Feb 13 00:20 master-bin.000003
-rw-r----- 1 mysql mysql 202 Feb 13 00:20 master-bin.000004
-rw-r----- 1 mysql mysql 202 Feb 13 00:20 master-bin.000005
-rw-r----- 1 mysql mysql 202 Feb 13 00:20 master-bin.000006
-rw-r----- 1 mysql mysql 202 Feb 13 00:20 master-bin.000007
-rw-r----- 1 mysql mysql 154 Feb 13 00:20 master-bin.000008
-rw-r----- 1 mysql mysql 256 Feb 13 00:20 master-bin.index
[root@masterdb binlog]# 

mysql> purge master logs to 'master-bin.000007';
Query OK, 0 rows affected (0.00 sec)


[root@masterdb binlog]# ls -lrt
total 12
-rw-r----- 1 mysql mysql 202 Feb 13 00:20 master-bin.000007
-rw-r----- 1 mysql mysql 154 Feb 13 00:20 master-bin.000008
-rw-r----- 1 mysql mysql  64 Feb 13 00:22 master-bin.index

Method 3 : Before execution of "purge master logs before'yyyy-mm- dd hh24:: mi ss'" command, which will delete the specified log log

Delete # 2020- 02 - 13 is  00 : 24 : 00 before the log
 
MySQL > System LS -lrt / MySQL / the binlog 
Total 36 
-rw-R & lt ----- . 1 MySQL MySQL 202 On Feb 13 is  00 : 20 is Master-bin. 000007 
------R & lt -rw . 1 MySQL MySQL 202 On Feb 13 is  00 : 25 Master-bin. 000008 
-rw-R & lt ----- . 1 MySQL MySQL 202 On Feb 13 is  00 : 25 Master-bin.000009
-rw-r----- 1 mysql mysql 202 Feb 13 00:25 master-bin.000010
-rw-r----- 1 mysql mysql 202 Feb 13 00:25 master-bin.000011
-rw-r----- 1 mysql mysql 202 Feb 13 00:25 master-bin.000012
-rw-r----- 1 mysql mysql 202 Feb 13 00:25 master-bin.000013
-rw-r----- 1 mysql mysql 154 Feb 13 00:25 master-bin.000014
-rw-r----- 1 mysql mysql 256 Feb 13 00:25 master-bin.index
mysql> purge master logs before '2020-02-13 00:24:00';
Query OK, 0 rows affected (0.00 sec)

mysql> system ls -lrt /mysql/binlog
total 32
-rw-r----- 1 mysql mysql 202 Feb 13 00:25 master-bin.000008
-rw-r----- 1 mysql mysql 202 Feb 13 00:25 master-bin.000009
-rw-r----- 1 mysql mysql 202 Feb 13 00:25 master-bin.000010
-rw-r----- 1 mysql mysql 202 Feb 13 00:25 master-bin.000011
-rw-r----- 1 mysql mysql 202 Feb 13 00:25 master-bin.000012
-rw-r----- 1 mysql mysql 202 Feb 13 00:25 master-bin.000013
-rw-r----- 1 mysql mysql 154 Feb 13 00:25 master-bin.000014
-rw-r----- 1 mysql mysql 224 Feb 13 00:29 master-bin.index

Method 4 : Add in the configuration file "expire_logs_days" parameter specifies the number of days expired, the expiration will be automatically deleted.

 

(5.2) binlog switch

Flush logs may be used to switch flush binary logs or logs.

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000017 |      154 | db1,db2,db3  | db4              |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

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

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000018 |      154 | db1,db2,db3  | db4              |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

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

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000019 |      154 | db1,db2,db3  | db4              |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

 

(5.3)查看binlog events

mysql> show binlog events in 'master-bin.000019';
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name          | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+
| master-bin.000019 |   4 | Format_desc    |         1 |         123 | Server ver: 5.7.24-log, Binlog ver: 4 |
| master-bin.000019 | 123 | Previous_gtids |         1 |         154 |                                       |
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+
2 rows in set (0.00 sec)

 

(5.4)查看当前biblog位置

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000019 |      154 | db1,db2,db3  | db4              |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

 

 

【Finish】

Guess you like

Origin www.cnblogs.com/lijiaman/p/12301998.html
log