You are on page 1of 5

Binary Logging

Chap 6

Table of Contents
No table of contents entries found.

What is Binary Logging?


- Binary logging is a feature that enables the logging of chances to the mysql database in
binary format.
- Records all changes made to the database
o Insert
o Updates
o Deletes
 All in the binary log file
- Binary log can be used for various purposes
o Data replication
o Point in time recovery
o Auditing

Functionality and Purposes:

Data Replication
- Changes made to a master database are replicated to one or more slave databases
- Binary log files contain the ata changes which are then read and applied by the
replication slave(s) to synchronize data with the master
Point-In-Time Recovery
- Restore the database to a specific point in time by replaying the binary log events up to
that point
- Valuable when recovering from accidental data loss or corruption
High Availability:
- Enables automatic failover and data consistency across multiple nodes
Data Auditing:
- Track changes made to the database
o Who made the changes
o When they were made
o What changes where performed
- Help in compliance with regulatory requirements and internal policies
Backup and Restore:
- Ensure a consistent backup of the database

Enabling Binary Logs:

1. Enable binary logging and set the server_id. Open the MySQL config file in
your favorite editor and append the following lines. Choose server_id such that
it will be unique to each MySQL server in your infrastructure.
You can also simply put the log_bin variable in my.cnf without any value. In
that case, the binary log is created in the data directory directory and
uses hostname as its name.

shell> sudo vi /etc/my.cnf


[mysqld]

log_bin = /data/mysql/binlogs/server1
server_id = 100
2. Restart the MySQL server:
shell> sudo systemctl restart mysql

3. Verify that binary logs are created:


mysql> SHOW VARIABLES LIKE 'log_bin%';

4. Execute SHOW BINARY LOGS; or SHOW MASTER LOGS; to display all the binary
logs of the server.

5. Execute the SHOW MASTER STATUS; command to get the current binary log
position:
mysql> SHOW MASTER STATUS;

Disabling Binary Logs For A Session:

There may be cases where you do not want the statements to be replicated to other servers.
For that, you can use the following command to disable binary logging for that session:

mysql> SET SQL_LOG_BIN = 0;

All the SQL statements logged after executing the previous statement are not logged to the
binary log. This applies only for that session.
To enable, you can execute the following:

mysql> SET SQL_LOG_BIN = 1;

Move To The Next Log:

mysql> SHOW BINARY LOGS;

mysql> FLUSH LOGS;

Expire Binary Logs:

Binary logs consume a lot of space based on the number of writes. Leaving them as-is can
fill up the disk in no time. It is essential to clean them up:
1. Set the expiry of logs using binlog_expire_logs_seconds and
expire_logs_days.
If you want to set an expiry period in days, set expire_logs_days. For example,
if you want to delete all the binary logs that are older than two days, SET
@@global.expire_logs_days=2. Setting the value to 0 disables automatic
expiry.
If you want to have more granularity, you can use
the binlog_expire_logs_seconds variable, which sets the binary log
expiration period in seconds.
The effects of this variable and expire_logs_days are cumulative. For example,
if expire_logs_days is 1 and binlog_expire_logs_seconds is 43200, then
the binary log is purged every 1.5 days. This produces the same result as setting
binlog_expire_logs_seconds to 129600 and leaving expire_logs_days set
to 0. In MySQL 8.0, both binlog_expire_logs_seconds and
expire_logs_days must be set to 0 to disable automatic purging of the binary
log.
2. To purge the logs manually, execute PURGE BINARY LOGS TO '<file_name>'.
For example, there are files such as server1.000001, server1.000002,
server1.000003, and server1.000004, and if you execute PURGE BINARY
LOGS TO 'server1.000004', all the files up to server1.000003 will be
deleted and server1.000004 will not be touched:
mysql> SHOW BINARY LOGS;

mysql> PURGE BINARY LOGS TO 'server1.000004';

You can also execute the command PURGE BINARY LOGS BEFORE '2017-08-03
15:45:00' rather than specifying a log file. You can also use the word MASTER
instead of BINARY.
mysql> PURGE MASTER LOGS TO 'server1.000004' also works.

3. To delete all the binary logs and start from the beginning again, execute RESET
MASTER:
mysql> SHOW BINARY LOGS;

mysql> RESET MASTER;

Purging or expiring logs is a very unsafe method if you are using


replication. The safe way to purge binary logs is to use
the mysqlbinlogpurge script

Binary Log Format:

Binary logs can be written in three formats:


1. STATEMENT: Actual SQL statements are logged.
2. ROW: Changes made to each row are logged.
For example, an update statement updates 10 rows, the updated information of
all 10 rows is written to the log. Whereas in statement-based replication, only the
update statement is written. The default format is ROW.
3. MIXED: MySQL switches from STATEMENT to ROW as and when needed.
There are statements that can cause different results when executed on different servers. For
example, the output of the UUID() function differs from server to server. Those statements
are called non-deterministic and are unsafe for statement-based replication. In those
situations, a MySQL server switches to row-based format when you set the MIXED format.

How to do it…

mysql> SET GLOBAL binlog_format = 'STATEMENT';


Or:
mysql> SET GLOBAL binlog_format = 'ROW';

1. MySQL 8.0 uses version 2 binary log row events, which cannot be read by
MySQL Server releases prior to MySQL 5.6.6. Set log-bin-use-v1-row-events
to 1 to use version 1 so that it can be read by versions prior to MySQL 5.6.6. The
default is 0.
mysql> SET @@GLOBAL.log_bin_use_v1_row_events=0;

2. When you create a stored function, you must declare either that it is deterministic
or that it does not modify data. Otherwise, it may be unsafe for binary logging.
By default, for a CREATE FUNCTION statement to be accepted, at least one of
DETERMINISTIC, NO SQL, or READS SQL DATA must be specified explicitly.
Otherwise an error occurs:

You might also like