MySQL Binary Logs
The Binary Logs have two main purposes...
Point in time recovery (PITR)
Replication
Check
SELECT @@log_bin;
SHOW BINARY LOGS;
View Binary Log Contents
mysqlbinlog log_file
Enable/Disable Binary Logging
Binary logging is enabled by default (the log_bin system variable is set to ON).
To enable at startup add this to the startup command...
--log-bin
If you supply the --log-bin option with no string (as shown) or with an empty string, the base name defaults to host_name-bin.To change the base name use: --log-bin[=base_name]If you do not include an absolute path in the base_name definition the binary logs will be written to the data directory.When you enable Binary Logging an index file is also created using the base name with the .index suffix.To disable at startup add this to the startup command...
--skip-log-bin
or
--disable-log-bin
A suitably privileged client can disable binary logging of its own statements using...
SET sql_log_bin=OFF
Flush
FLUSH BINARY LOGS;
Deleting Binary Logs
Note that deleting the logs has implications for PITR and Replication. Understand these implications before continuing.To delete all existing binary log files and resets the binary log index file...
RESET MASTER
If you do not want the numbering to restart at 1, then use...
RESET MASTER TO 1234
If you just want to remove older logs (without impacting Replication)...
For PITR you need to retain all logs since the start of the last backupPURGE BINARY LOGS TO 'mysql-bin.010';
or
PURGE BINARY LOGS BEFORE '2019-04-02 22:46:26';
Binary Log Configuration
binlog_encryption
Introduced in MySQL 8.0.14Default: OFF
SELECT @@binlog_encryption;
To resize this variable online (without the need to restart MySQL Server)...
SET GLOBAL binlog_encryption=ON;
binlog_expire_logs_seconds
Default: 2592000
SELECT @@binlog_expire_logs_seconds;
To resize this variable online (without the need to restart MySQL Server)...
SET GLOBAL binlog_expire_logs_seconds=1296000;
For PITR you need all binlogs since your last successful backup.
binlog_format
Default: ROW
SELECT @@binlog_format;
To resize this variable online (without the need to restart MySQL Server)...
SET GLOBAL binlog_format=ROW;
log_replica_updates
Introduced in 8.0.26Default: ON
"Causes the replica to write the updates that are received from a source and performed by the replication SQL thread to the replica's own binary log." (3) Useful when you want to chain replication servers.
SELECT @@log_replica_updates;
log_slave_updates
Deprecated in 8.0.26. Use log_replica_updates insteadDefault: OFF (5.7) / ON (8.0)
Useful when you want to chain replication servers
SELECT @@log_slave_updates;
max_binlog_size
Default: 1073741824 bytes (1 GB)
SELECT @@max_binlog_size;
sync_binlog
Default: 1
Recommended: 1
See also: innodb_flush_log_at_trx_commit
SELECT @@sync_binlog;
To change this variable online (without the need to restart MySQL Server)...
SET GLOBAL sync_binlog=0;
0 relies on the OS to flush the binary log to disk from time to time as it does for any other file. This improves performance but risks data loss.my.cnf
Example entries relating to binary logs...
binlog-format = ROW
binlog_expire_logs_seconds = 2592000
expire-logs-days = 0
log-bin = /var/lib/mysql/log
log-replica-updates = ON
sync-binlog = 1
max_binlog_size = 1024M
server-id = 0
For PITR you need all binlogs since your last successful backup.
Bibliography & References
https://dev.mysql.com/doc/refman/8.0/en/mysqlbinlog.htmlhttps://dev.mysql.com/doc/refman/8.0/en/binary-log-formats.htmlhttps://dev.mysql.com/doc/refman/8.0/en/replication-options-binary-log.htmlhttps://dev.mysql.com/doc/refman/8.0/en/reset-master.htmlhttps://dev.mysql.com/doc/refman/8.0/en/reset.htmlhttps://dev.mysql.com/doc/refman/8.0/en/replication-options-binary-log.html#sysvar_binlog_encryption(1) https://dev.mysql.com/doc/refman/8.0/en/flush.html#flush-binary-logshttps://dev.mysql.com/doc/refman/8.0/en/flush.html#flush-logs(2) https://dev.mysql.com/doc/refman/8.0/en/purge-binary-logs.html
https://gist.github.com/rhtyd/d59078be4dc88123104ehttps://mysqlserverteam.com/new-option-to-stop-the-server-if-binlogging-fails-in-mysql-5-6/https://dba.stackexchange.com/questions/206598/how-to-set-mysql-binlog-retention-in-hourshttps://dba.stackexchange.com/questions/259710/percona-server-binlog-not-automatically-purge-with-mysqladmin-flush-logshttps://hevodata.com/learn/using-mysql-binlog/
https://dev.mysql.com/doc/refman/5.7/en/replication-options-binary-log.html#sysvar_log_slave_updates(3) https://dev.mysql.com/doc/refman/8.0/en/replication-options-binary-log.html#sysvar_log_replica_updates
(4) https://dev.mysql.com/doc/refman/8.0/en/replication-options-binary-log.html#sysvar_sync_binlog
https://dev.mysql.com/doc/refman/8.0/en/replication-options-binary-log.html#sysvar_max_binlog_sizehttps://aws.amazon.com/blogs/database/best-practices-for-configuring-parameters-for-amazon-rds-for-mysql-part-2-parameters-related-to-replication/https://serverfault.com/questions/405726/max-binlog-size-log-bin-size