search
HomeDatabaseMysql TutorialMySQL method to automatically clean binlog logs

Instructions:

Open MySQL If the binlog server is not set to automatically clean the logs, the binlog logs will be retained by default. Over time, the server disk space will be filled up by the binlog logs, causing an error in the MySQL database.

Use the following method to safely clear binlog logs

1. Clean the logs without master-slave synchronization

mysql -uroot -p123456 -e 'PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ),INTERVAL 5 DAY)';

#mysql Regularly clear the binlog from 5 days ago

mysql -u root -p #Enter the mysql console

reset master; Log

1, mysql -u root -p #Enter the slave server mysql console

show slave statusG; #Check which log is being read from the slave server. There are multiple slave servers. Select the earliest one as the target log.

2. Enter the master server mysql console

show master log; #Get a series of logs on the master server

PURGE MASTER LOGS TO 'binlog.000058'; #Delete the ones before binlog.000005, excluding binlog.000058

PURGE MASTER LOGS BEFORE '2016-06-22 13:00:00'; #Clear binlog logs before 2016-06-22 13:00:00
PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 3 DAY); #Clear binlog logs 3 days ago

Three. Set up automatic cleaning of MySQL binlog logs

vi /etc/my.cnf #Edit configuration

expire_logs_days = 15 #自动删除15天前的日志。默认值为0,表示从不删除。
log-bin=mysql-bin #注释掉之后,会关闭binlog日志
binlog_format=mixed #注释掉之后,会关闭binlog日志

:wq! # Save and exit

Extended reading:

mysql> help purge;

Name: 'PURGE BINARY LOGS'

Description:

Syntax:

PURGE { BINARY | MASTER } LOGS

{ TO 'log_name' | BEFORE datetime_ expr }

The binary log is a set of files that contain information about data

modifications made by the MySQL server. The log consists of a set of

binary log files, plus an index file (see

http:// dev.mysql.com/doc/refman/5.5/en/binary-log.html).

The PURGE BINARY LOGS statement deletes all the binary log files listed

in the log index file prior to the specified log file name or date.

BINARY and MASTER are synonyms. Deleted log files also are removed from

the list recorded in the index file, so that the given log file becomes

the first in the list.

This statement has no effect if the server was not started with the

--log-bin option to enable binary logging.

URL: http://dev.mysql.com/doc/refman/5.5/en/purge-binary-logs.html

Examples:

PURGE BINARY LOGS TO 'mysql-bin.010';

PURGE BINARY LOGS BEFORE '2008-04-02 22:46:26';

The following are the methods given by other netizens, you can refer to them

MYSQL master-slave replication uses RBR After changing the mode, the format of binlog is "ROW", which can solve many original key duplication problems.

In a busy master db On the server, the binlog log file grows very quickly. If it is not cleared regularly, the hard disk space will be filled up quickly.

Set up automatic cleaning of mysql Binlog log, configure my.cnf:

expire_logs_days = 10

Modify at runtime:

show binary logs;

show variables like '%log%';

set global expire_logs_days = 10;

You can use the corresponding backup strategy before clearing.

Manually delete the MySQL binlog logs 10 days ago:

PURGE MASTER LOGS BEFORE DATE_SUB(CURRENT_DATE, INTERVAL 10 DAY);

show master logs;


MASTER and BINARY are synonyms.

Under normal circumstances, it is recommended to use MIXED binlog replication. Instructions in http://dev.mysql.com/doc/refman/5.1/en/open-bugs-general.html: Replication uses query-level logging: The master writes the executed queries to the binary log. This is a very fast, compact, and efficient logging method that works perfectly in most cases.

Attachment: Several modes of MYSQL replication

Starting from MySQL 5.1.12, the following three modes can be used to achieve:

–Statement-based replication replication, SBR),
– row-based replication (row-based replication, RBR),
– Mixed-based replication (MBR).
Correspondingly, there are three formats of binlog: STATEMENT, ROW, and MIXED. In MBR mode, SBR mode is the default.

The binlog format can be dynamically changed at runtime, except for the following situations:

. In the middle of a storage process or trigger

. NDB is enabled
. Try RBR for the current session mode, and the temporary table has been opened

If the binlog adopts the MIXED mode, the binlog mode will be automatically changed from SBR mode to RBR mode in the following situations.

. When a DML statement updates an NDB table

. When the function contains UUID()
. When 2 or more tables containing AUTO_INCREMENT fields are updated
. When executing any INSERT DELAYED statement
. When using UDF
. When the view must require the use of RBR, for example, when creating the view, UUID() is used Function

Set master-slave replication mode:
log-bin=mysql-bin
#binlog_format="STATEMENT"
#binlog_format="ROW"
binlog_format="MIXED"

You can also dynamically modify the binlog format at runtime. For example
mysql> SET SESSION binlog_format = 'STATEMENT';
mysql> SET SESSION binlog_format = 'ROW';
mysql> SET SESSION binlog_format = 'MIXED';
mysql> SET GLOBAL binlog_format = 'STATEMENT';
mysql> SET GLOBAL binlog_format = 'ROW';
mysql> SET GLOBAL binlog_format = 'MIXED';

The advantages and disadvantages of the two modes:

SBR Advantages:
Long history, mature skills
Binlog file is smaller
Binlog contains all database modification information, which can be used to audit the security of the database
Binlog can be used for real-time restoration, not just for replication
The master and slave versions can be different, and the slave server version can be higher than the master server version
SBR Disadvantages:
Not all UPDATE statements can be copied, especially when they contain uncertain operations.
Call UDF with non-deterministic factors There may be problems when copying. Statements using the following functions cannot be copied:
* LOAD_FILE()
* UUID()
* USER()
* FOUND_ROWS()
* SYSDATE() (unless the –sysdate-is-now option is enabled at startup)
INSERT … SELECT Will generate more row-level locks than RBR
When copying an UPDATE that needs to perform a full table scan (no index is used in the WHERE statement), it needs to be larger than RBR Request more row-level locks
For InnoDB tables with AUTO_INCREMENT fields, INSERT statements will block other INSERTs Statement
For some complex statements, the resource consumption on the slave server will be more serious, and in RBR mode, it will only affect the changed record
Stored function (not stored process ) will also execute the NOW() function once when it is called. This can be a bad thing or a good thing
Determined UDF It also needs to be executed on the slave server
The data table must be almost consistent with the master server, otherwise it may cause replication errors
If errors occur when executing complex statements, more resources will be consumed

RBR Advantages:

Any situation can be replicated, which is the safest and most reliable for replication
The same as the replication skills of most other database systems
In most cases, if the table on the slave server has a primary key, replication will Much faster
Fewer row locks when copying the following statements:
* INSERT … SELECT
* INSERT
* containing the AUTO_INCREMENT field UPDATE without conditions or without modifying many records Or DELETE statement
Less locks when executing INSERT, UPDATE, DELETE statements
It is possible to use multi-threading to perform replication from the server
RBR Disadvantages:
The binlog is much larger
During complex rollbacks, the binlog will contain a large amount of data
Execute UPDATE on the main server statement, all changed records will be written to the binlog, while SBR will only write once, which will lead to frequent concurrent writing problems of the binlog
Large BLOB generated by UDF The value will cause replication to slow down
You cannot see from the binlog what statements were copied and written (encrypted)
When executing a pile of SQL statements on a non-transactional table, it is best to use SBR mode, otherwise it will easily lead to data inconsistency between the master and slave servers
In addition, the processing guidelines for changes in the tables in the system library mysql are as follows:
If using When INSERT, UPDATE, and DELETE directly operate the table, the log format is recorded according to the setting of binlog_format
If it is used If management statements such as GRANT, REVOKE, and SET PASSWORD are used to do this, SBR mode recording will be used no matter what.
Note: Using RBR After the mode is implemented, many primary key duplication problems that originally occurred can be solved. Example:
For insert into db_allot_ids select * from db_allot_ids This statement:
in BINLOG_FORMAT=STATEMENT In mode:
BINLOG log information is:
————————————–
BEGIN
/*!*/;
# at 173
#090612 16:05:42 server id 1 end_log_pos 288 Query thread_id=4 exec_time=0 error_code=0
SET TIMESTAMP=1244793942/*!*/;
insert into db_allot_ids select * from db_allot_ids
/*!*/;
————————————–

In BINLOG_FORMAT=ROW mode:

BINLOG log information is:
———————— —————–
BINLOG '
hA0yShMBAAAAMwAAAOAAAAAAA8AAAAAAAAAA1NOUwAMZGJfYWxsb3RfaWRzAAIBAwAA
hA0yShcBAAAANQAAABUBAAAQAA8AAAAAAAEAAv/8AQEAAAD8AQEAAAD8AQEAAAD8AQEAAAA=
'/*!*/;
——————————————–


Steps to clear logs
1. Find log files
mysql> show binary logs;
+----------------+----------+
| Log_name | File_size |
+----------------+-----------+
| ablelee.000001 | 150462942 |
| ablelee.000002 | 125 |
| ablelee.000003 | 106 |
+----------------+----------+
2. Delete bin-log (delete the one before ablelee.000003 but does not include ablelee .000003)
mysql> purge binary logs to 'ablelee.000003';
Query OK, 0 rows affected (0.16 sec)

3. Query results (there is only one record now.)

mysql> show binlog eventsG
****************************** * 1.row ***************************
Log_name: ablelee.000003
Pos: 4
Event_type: Format_desc
Server_id: 1
End_log_pos: 106
Info: Server ver: 5.1.26-rc-log, Binlog ver: 4
1 row in set (0.01 sec)
(ablelee.000001 and ablelee.000002 have been deleted)
mysql> show binary logs;
+----------------+----------+
| Log_name | File_size |
+----------------+-----------+
| ablelee.000003 | 106 |
+----------------+-----------+
1 row in set (0.00 sec)

(Other formats deleted!)
  PURGE {MASTER | BINARY} LOGS TO 'log_name'
  PURGE {MASTER | BINARY} LOGS BEFORE 'date'
Used to delete all binary logs listed in the log index before the specified log or date. These logs are also removed from the list recorded in the log index file so that the given log becomes first.
 For example:
 PURGE MASTER LOGS TO 'mysql-bin.010';
 PURGE MASTER LOGS BEFORE '2008-06-22 13:00:00';
Clear the binlog from 3 days ago
PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 3 DAY);
 The date argument of the BEFORE variable can be 'YYYY-MM-DD hh:mm:ss' format. MASTER and BINARY are synonyms.
 If you have an active slave server that is currently reading one of the logs you are trying to delete, this statement will not work and will fail with an error. However, if the slave is quiesced and you happen to clear one of the logs it wants to read, the slave cannot replicate after it is started. This statement can be safely run while the slave server is replicating. You don't need to stop them.
 To clear the logs, follow the steps below:
 1. On each slave, use SHOW SLAVE STATUS to check which log it is reading.
 2. Use SHOW MASTER LOGS gets a series of logs on the master server.
 3. Determine the oldest log among all slave servers. This is the target log. If all slave servers are up to date, this is the last log on the list.

 4. Make a backup of all logs you will delete. (This step is optional, but recommended.)

  5. Clean all logs, but not including the target log


The above is the content of MySQL’s method of automatically cleaning binlog logs. For more related articles, please Follow the PHP Chinese website (www.php.cn)!


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
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

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.