search
HomeBackend DevelopmentPHP TutorialExplain how to use mysql binlog

Explain how to use mysql binlog

Jun 08, 2018 pm 03:39 PM
binlogmysql

This article introduces the use of mysql binlog, including opening, closing, viewing status, refreshing, clearing, viewing executed sql statements and other operations. The settings of 5.7 and older versions are also explained to facilitate everyone's learning.

Mysql binlog introduction

Binlog is binary log, a binary log file that records all mysql dml operations.

According to the mysql binlog file, we can check what SQL statements were executed, perform data recovery, master-slave synchronous replication and other operations.

The binlog file plays an important role in the processing and recovery of a database.

1.mysql binlog opening and closing

View mysql binlog configuration

show global variables like '%log_bin%';
+---------------------------------+-------+| Variable_name                   | Value |
+---------------------------------+-------+| log_bin                         | OFF   |
| log_bin_basename                |       |
| log_bin_index                   |       |
| log_bin_trust_function_creators | OFF   |
| log_bin_use_v1_row_events       | OFF   |+---------------------------------+-------+

binlog is currently closed.

Open binlog

Open my.cnf or my.ini and add the following statement, Restart mysql

log_bin=ONlog_bin_basename=/usr/local/var/mysql/mysql-binlog_bin_index=/usr/local/var/mysql/mysql-bin.index

log_bin
ON means to open the binlog log, and change it to OFF.

log_bin_basename
represents the basic file name of the binlog log, and an identifier will be appended to distinguish each file.

log_bin_index
Specify the index file of the binlog file. This file manages the directories of all binlog files.


If it is below mysql5.7, this setting is enough. If it is above 5.7, you need to set it as follows

log_bin=mysql-binserver_id=123456

log_bin Indicates the custom binlog file name.

server_id means randomly specifying a string that does not have the same name as other cluster machines. It needs to be defined when configuring mysql replication. It cannot be repeated with the slaveId of canal.

Check the mysql binlog configuration again after restarting

show global variables like '%log_bin%';
+---------------------------------+--------------------------------------+| Variable_name                   | Value                                |
+---------------------------------+--------------------------------------+| log_bin                         | ON                                   |
| log_bin_basename                | /usr/local/var/mysql/mysql-bin       |
| log_bin_index                   | /usr/local/var/mysql/mysql-bin.index |
| log_bin_trust_function_creators | OFF                                  |
| log_bin_use_v1_row_events       | OFF                                  |+---------------------------------+--------------------------------------+

You can see that binlog is enabled.

2. View the binlog log file list

show master logs;
+------------------+-----------+| Log_name         | File_size |
+------------------+-----------+| mysql-bin.000001 |       177 |
| mysql-bin.000002 |       177 |
| mysql-bin.000003 |       177 |
| mysql-bin.000004 |       177 |
| mysql-bin.000005 |       177 |
| mysql-bin.000006 |       177 |
| mysql-bin.000007 |       201 |
| mysql-bin.000008 |       201 |
| mysql-bin.000009 |       201 || mysql-bin.000010 |       154 |
+------------------+-----------+

3. View the binlog log currently being written

show master status;
+------------------+----------+--------------+------------------+-------------------+| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+| mysql-bin.000010 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

4. Refresh the binlog log file

flush logs;

5. Clear the log file

reset master;

6. View the contents of the binglog log file

View the binlog log file to see which sql statements have been executed. We can use mysqlbinlog Tools for processing.

First, according to log_bin_basename, find the directory where the binlog file is stored, and then use the mysqlbinlog tool to view the corresponding binlog file.

For example:

mysqlbinlog -v mysql-bin.000001 > mysql-bin-1.log

Then check mysql-bin-1.log to view the executed sql statements.

BINLOG '
Xq1HWhNA4gEAPAAAAGQBAAAAAPEEAAAAAAEACXRlc3RfdXNlcgAGY3NfdGFnAAUDDwEDAwL9AgBa
WZlG
Xq1HWh5A4gEANwAAAJsBAAAAAPEEAAAAAAEAAgAF/+ACAAAABABjc2RuAf2LG1r9ixtaIS88ZA==
'/*!*/;### INSERT INTO `test_user`.`cs_tag`### SET###   @1=2###   @2='csdn'###   @3=1###   @4=1511754749###   @5=1511754749# at 411

You need to pay attention to a few points when using mysqlbinlog

1. Do not check the binlog file currently being written. You can copy the file to another directory first and then execute it. Check.

2. Do not add the force parameter to force access.

3. If the binlog format is row mode, please add the -vv parameter.

This article explains how to use mysql binlog. For more related knowledge, please pay attention to the php Chinese website.

Related recommendations:

Explain the relevant content of PHP using the token bucket algorithm to implement flow control based on redis

How to pass PHP creates a QR code class with logo

Detailed explanation of the related methods of mysql to rebuild table partitions and retain data

The above is the detailed content of Explain how to use mysql binlog. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you modify data stored in a PHP session?How do you modify data stored in a PHP session?Apr 27, 2025 am 12:23 AM

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Give an example of storing an array in a PHP session.Give an example of storing an array in a PHP session.Apr 27, 2025 am 12:20 AM

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

How does garbage collection work for PHP sessions?How does garbage collection work for PHP sessions?Apr 27, 2025 am 12:19 AM

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

How can you trace session activity in PHP?How can you trace session activity in PHP?Apr 27, 2025 am 12:10 AM

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

How can you use a database to store PHP session data?How can you use a database to store PHP session data?Apr 27, 2025 am 12:02 AM

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment