Using and Maintaining the Binary Log
Complete binary log maintenance: PURGE BINARY LOGS/RESET MASTER, expire_logs_days & binlog_expire_logs_seconds, FLUSH BINARY LOGS, and SHOW SLAVE STATUS.
See Overview of the Binary Log for a general overview of what the binary log is, and Activating the Binary Log for how to make sure it's running on your system.
Binary log file security is documented on Securing MariaDB Logs.
For details on using the binary log for replication, see the Replication section.
Purging Log Files
To delete all binary logs on the server, run the RESET MASTER command. To delete all binary logs before a certain datetime, or up to a certain number, use PURGE BINARY LOGS.
If a replica is active but has yet to read from a binary log you attempt to delete, the statement fails with an error. However, if the replica is not connected and has yet to read from a log you delete, the file is deleted, but the replica is unable to continue replicating once it connects again.
Logs can also be removed automatically with the expire_logs_days system variable. This is set to 0 by default (no removal). It can be set to a time, in days, after which a binary log file is automatically removed. Log files are only checked for being older than expire_logs_days upon log rotation, so if your binary log fills up slowly and does not reach max_binlog_size on a daily basis, you may see older logs still being kept. You can also force log rotation, and so expiry deletes, by running FLUSH BINARY LOGS on a regular basis. Always set expire_logs_days higher than any possible replica lag.
From MariaDB 10.6, the binlog_expire_logs_seconds variable allows more precise control over binlog deletion and takes precedence if both are non-zero.
[Note] Binary log 'mariadb-bin.000001' is not purged because less than 'slave_connections_needed_for_purge' slaves have processed itIf the binary log index file has been removed, or incorrectly manually edited, all of the above forms of purging logs fail. The .index file is a plain text file and can be manually recreated or edited so that it lists only the binary log files that are present, in numeric/age order.
Examples of Log File Purging
Safely Purging Binary Log Files While Replicating
To be sure replication is not broken while deleting log files, perform the following steps:
Get a listing of binary log files on the primary by running SHOW BINARY LOGS.
Go to each replica server and run SHOW REPLICA STATUS to check which binary log file each replica is currently reading.
Find the earliest log file still being read by a replica. No log files before this one will be needed.
If you wish, make a backup of the log files to be deleted
Purge all log files before (not including) the file identified above.
Limiting the Binlog Size
You can limit the size of the binlog by setting the max_binlog_total_size system variable. If not set to zero, the total size of the binlog is stored in the binlog_disk_use status variable. It's also possible to limit the size of a single binlog file by setting max_binlog_size.
Selectively Logging to the Binary Log
By default, all changes to data or data structure are logged. This behavior can be changed by starting the server with the --binlog-ignore-db=database_name or --binlog-do-db=database_name options.
--binlog-ignore-db=database_name specifies a database to ignore for logging purposes, while --binlog-do-db=database_name doesn't log any changes unless they apply to the specified database. If --binlog-do-db is set, --binlog-ignore-db is ignored.
Neither option accepts comma-delimited lists of multiple databases as an option, since a database name can contain a comma. To apply to multiple databases, use the option multiple times.
Which database the server checks depends on the format that a change is logged in, not on the binlog_format setting alone:
Row events. For data changes logged in row format, the server checks the database of each table that is changed, irrespective of the current database.
Statement events. For statements logged in statement format, the server checks the default database, which is set with the USE statement. The database of the tables that the statement changes isn't considered. If no default database is selected, the statement is logged when
--binlog-ignore-dbis set, and isn't logged when--binlog-do-dbis set.
DDL statements, such as CREATE TABLE or DROP TABLE, are always logged in statement format, even when binlog_format=ROW. So they're always filtered by the default database. With binlog_format=MIXED, each statement is filtered according to the format it's logged in.
Examples of Selective Logging
Assume the server has started with the option --binlog-ignore-db=employees. The following example is logged if statement-based logging is used, and is not logged with row-based logging.
This is because statement-based logging examines the default database, in this case, customers. Since customers is not specified in the ignore list, the statement is logged. If row-based logging is used, the example is not logged, because updates are written to the tables in the employees database.
Assume now that the server started with the option --binlog-do-db=employees. The following example is not logged if statement-based logging is used, and is logged with row-based logging.
This is again because statement-based logging examines the default database, in this case, customers. Since customers is not specified in the do list, the statement is not logged. If row-based logging is used, the example is logged as updates are written to the tables in the employees database.
DDL statements follow the default database in every format. With --binlog-ignore-db=employees and binlog_format=ROW, the following statement is logged, because the default database is customers:
The following statement is not logged, even though it creates a table in the customers database, because the default database is employees:
Effects of Full Disk Errors on Binary Logging
If MariaDB encounters a full disk error while trying to write to a binary log file, then it will keep retrying the write every 60 seconds. Log messages will get written to the error log every 600 seconds. For example:
However, if MariaDB encounters a full disk error while trying to open a new binary log file, then it will disable binary logging entirely. A log message like the following will be written to the error log:
See Also
This page is licensed: CC BY-SA / Gnu FDL
Last updated
Was this helpful?

