Monitor changes in MySQL table content and enable MySQL binlog

Monitor changes in MySQL table content and enable MySQL binlog

Preface

binlog is a binary log file, which records all the add, delete, and modify statements of MySQL. Through binlog logs, we can perform data recovery, master-slave replication, etc. As you can see, as long as we have this binlog, we have a complete backup of mysql.

We often encounter such a requirement, that is, we need to monitor the changes in a certain table and then perform some operations.

If the data in the table is only added and not deleted or modified, it is relatively simple to monitor. You can query the latest ID regularly. However, if deletion or modification operations are required, the entire table must be scanned, which is extremely inefficient. It would be best if when the table changes, an event can be triggered for monitoring.

Now we can complete it through binlog. Just monitor the changes in binlog, so that every time a statement is executed, it will be recorded in binlog and we can monitor it.

Binlog is disabled by default. To enable it, we need to modify the MySQL configuration file.

The default installation directory of MySQL installed by brew on Mac is /usr/local/Cellar, and the version is 5.7.21.


As you can see, there is no configuration file in the directory. This may be different from some other versions. Other versions may have a my.ini or my.cnf file in the root directory, or a my-default.cnf file in support-files, but this version does not have that.

To modify the configuration file, we need to create it ourselves.

Create a my.cnf file in the /etc/ directory with the following content:

[mysqld]
server_id = 1
log-bin = mysql-bin
binlog-format = ROW

mysql-bin is just a name, you can use any name you want. The log file names saved in the future will be mysql-bin.000001, mysql-bin.000002.

Note that binlog_format must be set to ROW, because in STATEMENT or MIXED mode, Binlog will only record and transmit SQL statements (to reduce log size) without specific data, so we cannot save it.

Then restart mysql via brew restart mysql. Then enter the MySQL console through the mysql -uroot -p command and execute

show variables like '%log_bin%' ; 

The arrow above is when my.cnf is not set, and the arrow below is after my.cnf is set and restarted.

You can use the show master status command to view the status of the binlog currently being written.


There are also commands like:

Refresh binlog log file flush logs #After refreshing, a new binlog log will be created.

Clear the log file and reset the master.

View the contents of the first binlog file show binlog events .

View the contents of the specified binlog file show binlog events in 'mysql-bin.000004' .

Get the list of binlog files show binary logs .

Having binlog alone is not enough, we also need a tool to monitor binlog. Next article: Canal

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • How to choose the format when using binlog in MySQL
  • Example verification MySQL | update field with the same value will record binlog
  • Detailed explanation of the binlog log analysis tool for monitoring MySQL: Canal
  • In-depth explanation of binlog in MySQL 8.0
  • Summary of some thoughts on binlog optimization in MYSQL
  • Detailed installation and configuration tutorial of MySQL flashback tool binlog2sql
  • MySQL uses mysqldump+binlog to completely restore the deleted database principle analysis
  • MySQL Binlog Data Recovery: Detailed Explanation of Accidentally Deleting a Database
  • Specific use of MySQL binlog_ignore_db parameter

<<:  How to clear the timer elegantly in Vue

>>:  Deleting files with spaces in Linux (not directories)

Recommend

Why does MySQL database index choose to use B+ tree?

Before further analyzing why MySQL database index...

5 common scenarios and examples of JavaScript destructuring assignment

Table of contents Preface 1. Extract data 2. Alia...

4 ways to implement routing transition effects in Vue

Vue router transitions are a quick and easy way t...

A simple way to implement all functions of shopping cart in Vue

The main functions are as follows: Add product in...

Practical basic Linux sed command example code

The Linux stream editor is a useful way to run sc...

Problems with using multiple single quotes and triple quotes in MySQL concat

When dynamically concatenating strings, we often ...

Some conclusions on the design of portal website focus pictures

Focus images are a way of presenting content that ...

Ubuntu 20.04 how to modify the IP address example

illustrate: Today, when continuing the last offic...

Solution to the problem that Navicat cannot remotely connect to MySql server

The solution to the problem that Navicat cannot r...

Mysql Workbench query mysql database method

Mysql Workbench is an open source database client...

MySQL parameter related concepts and query change methods

Preface: In some previous articles, we often see ...

Specific use of the autoindex module in the Nginx Http module series

The main function of the brower module is to dete...

A brief discussion on how to learn JS step by step

Table of contents Overview 1. Clearly understand ...