Skip to content

Instantly share code, notes, and snippets.

@tiagocardosos
Last active March 5, 2024 12:18
Show Gist options
  • Save tiagocardosos/0d8e7e95316696be7a400e2c60909c43 to your computer and use it in GitHub Desktop.
Save tiagocardosos/0d8e7e95316696be7a400e2c60909c43 to your computer and use it in GitHub Desktop.
MySQL Clear Binary Log
Can I Remove MySQL Binary Log Yes, as long as the data is replicated to Slave server, it’s safe to remove the file. It’s recommend only remove MySQL Binary Log older than 1 month.
Besides, if Recovery of data is the main concern, it’s recommend to archive MySQL Binary Log.
There are several ways to remove or clean up MySQL Binary Log, it’s not recommend to clean up the file manually, manually means running the remove command.
Remove MySQL Binary Log with RESET MASTER Statement Reset Master statement is uses for new database start up during replication for Master and Slave server. This statement can be used to remove all Binary Log.
To clean up Binary Log on Master Server
```
shell> mysql -u username -p
mysql> RESET MASTER;
```
To clean up Binary Log on Slave Server
```
mysql -u username -p
mysql> RESET SLAVE;
```
Remove MySQL Binary Log with PURGE BINARY LOGS Statement PURGE BINARY LOGS statement can remove Binary Log base on date or up to a Binary Log sequence number
Base on the binary logs example shown above, I would like to remove binary up to mysql-bin.000015
```
shell> mysql -u username -p
mysql>PURGE BINARY LOGS TO 'mysql-bin.000015';
```
Alternatively, you can remove the binary older than a specific date.
```
shell> mysql -u username -p
mysql> PURGE BINARY LOGS BEFORE '2009-05-01 00:00:00';
```
Remove MySQL Binary Log with mysqladmin flush-logs Command Another method is running mysqladmin flush-logs command, it will remove binary logs more than 3 days old.
```
shell> mysqladmin -u username -p flush-logs
```
Keep MySQL Binary Log for X Days All of the methods above required monitoring on disk usage, to “rotate” and keep the binary logs for x number of day. The option below can be configured on MySQL’s config file, my.cnf
```
expire_logs_days = 7
```
Consider turning off MySQL Binary Log if MySQL Replication is not deploy on the database server and recovery is not the main concern.
I have additional 15GB if disk space now
@seunggabi
Copy link

PURGE BINARY LOGS BEFORE NOW();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment