First, what is binlog
binlog
It is a binary file format for recording user database updated SQL statement information, such as changing the database tables and change the contents of the SQL statement will be recorded in the binlog, but the library table and other contents of the query will not be recorded .
By default, the binlog
log is in binary format can not be used to view text tool commands (for example, cat, vi, etc.) to view, and use mysqlbinlog
analytic view.
Two, binlog role
Primary database for replication and recovery from an incremental data.
Three, binlog three modes
3.1 Row Level mode (RBR)
不记录每条sql语句的上下文信息,只需记录哪条数据被修改了,修改成什么样了。
advantage:
准确性强,能准确复制数据的变更
Disadvantages:
会产生大量的日志,导致较大的网络IO和磁盘IO
3.2 Statement Level Mode (SBR)
每一条修改数据的 sql 都会记录到 master 的 bin_log 中,slave 在复制的时候 sql 进程会解析成 master 端执行过的相同的 sql 在 slave 库上再次执行。
advantage:
不需要记录每一条sql语句和每一行的数据变化,减少了binlog日志量,节约IO,提高性能。
Disadvantages:
在某些情况下会导致 master-slave 中的数据不一致(如sleep()函数, last_insert_id(),以及user-defined functions(udf)等会出现问题)
3.3 MIXED mode (MBR)
以上两种模式的混合使用,一般的复制使用 STATEMENT 模式保存 binlog,对于 STATEMENT 模式无法复制的操作使用 ROW 模式保存 binlog,MySQL会根据执行的 SQL 语句选择日志保存方式。
advantage:
准确性强,文件大小适中
Disadvantages:
有可能发生主从不一致问题
Select 3.4 binlog mode
1. 不用存储过程、触发器、函数,选择默认的 Statement level
2. 用到MySQL的特殊功能(存储过程、触发器、函数)选择Mixed模式
3. 用到MySQL的特殊功能(存储过程、触发器、函数),又希望数据最大化一直则选择Row模式
Four, binlog common parameters
parameter name | meaning |
---|---|
log_bin = {on | off | base_name} | Specifies whether to enable recording binary log or specify a log path |
sql_log_bin ={ on | off } | Specifies whether to record the binary log enabled |
expire_logs_days | Specified time automatically deleted binary log that logs the expiration time |
log_bin_index | Specifies the path to the file mysql-bin.index |
binlog_format = { mixed | row | statement } | Specifies the binary log records based on what mode |
max_binlog_size | Specifies the maximum binary log file |
binlog_cache_size | Specify transaction log buffer size |
max_binlog_cache_size | Specifies the maximum size for binary log cache |
sync_binlog = { 0 | n } | Specify how many times the write buffer, brush plate |
Fifth, practice
5.1 binlog check function is turned on
mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | OFF |
+---------------+-------+
1 row in set (0.00 sec)
We can see binlog
the function is disabled by default.
5.2 open binlog function
Create a binlog
storage path and empowerment
[root@VM_0_15_centos log]# mkdir -p /var/log/mysql
[root@VM_0_15_centos log]# chown -R mysql.mysql /var/log/mysql
Edit my.cnf
Profile
[root@VM_0_15_centos log]# vim /etc/my.cnf
In the [mysqld]
Edit under the following labels:
# binlog 存放路径
log_bin=/var/log/mysql/mysql-bin
# 服务Id,保持唯一
server_id=1
Restart MySQL service
[root@VM_0_15_centos log]# systemctl restart mysqld
Re-examine whether to open the log function
mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)
5.3 binlog View mode
mysql> show variables like '%binlog_format%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW |
+---------------+-------+
1 row in set (0.00 sec)
You can see the MySQL 5.7
version of the default binlog
mode ROW
.
5.4 modify binlog mode
Edit my.cnf
Profile
[root@VM_0_15_centos log]# vim /etc/my.cnf
In the [mysqld]
Edit under the following labels:
binlog_format=mixed
Restart Service
[root@VM_0_15_centos log]# systemctl restart mysqld
View binlog
mode
mysql> show variables like '%binlog_format%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | MIXED |
+---------------+-------+
1 row in set (0.00 sec)
5.5 View binlog file
View statement
schema file:
mysqlbinlog 文件名
View row
schema file
mysqlbinlog -vv 文件名
5.6 Delete binlog file
method one:
reset master
Note: This command will delete all the logs and let the log file restarts from 000001.
Method Two:
PURGE { BINARY | MASTER } LOGS { TO 'log_name' | BEFORE datetime_expr }
Example:
purge master logs to "binlog_name.00000X"
It will clear 00000X
all log files before.
Method three: Modify the my.cnf
Configuration
expire_logs_days = 3 #保留最近3天的binlog;默认值是0,表示不自动删除.