实践
1)开启binlog
1、
vim /etc/mysql/mysql.cnf
添加
# binlog 配置
log-bin = /var/log/mysql/mysql-bin.log
expire-logs-days = 14
max-binlog-size = 500M
server-id = 1
2、重启服务,观察是否开启
service mysql status
service mysql stop
service mysql start
查看是否开启
show variables like 'log_bin';
Value 为 ON开启。
2)查看
在binlog文件目录
$ sudo mysqlbinlog --no-defaults --base64-output=decode-rows mysql-bin.000002 |nl
查看内容
mysqlbinlog 查看binlog时报错unknown variable 'default-character-set=utf8' - CobbLiu - 博客园 https://www.cnblogs.com/cobbliu/p/4311926.html
下午在排查MySQL主从同步问题时,想从主库的binlog中找一些线索,裸的binlog文件是无法直视的,mysqlbinlog这个工具是用来查看binlog文件内容的(使用方式man mysqlbinlog查看),但是使用mysqlbinlog将binlog文件转换成人类可读的内容时却报错:
|
1
2
|
[xxx@dbhost log]$ mysqlbinlog mysql-bin.000004mysqlbinlog: unknown variable 'default-character-set=utf8'
|
原因是mysqlbinlog这个工具无法识别binlog中的配置中的default-character-set=utf8这个指令。
两个方法可以解决这个问题
一是在MySQL的配置/etc/my.cnf中将default-character-set=utf8 修改为 character-set-server = utf8,但是这需要重启MySQL服务,如果你的MySQL服务正在忙,那这样的代价会比较大。
二是用mysqlbinlog --no-defaults mysql-bin.000004 命令打开
https://dev.mysql.com/doc/refman/5.7/en/innodb-redo-log.html
https://dev.mysql.com/doc/refman/5.7/en/mysqlbinlog.html
The redo log is a disk-based data structure used during crash recovery to correct data written by incomplete transactions. During normal operations, the redo log encodes requests to change InnoDB table data that result from SQL statements or low-level API calls. Modifications that did not finish updating the data files before an unexpected shutdown are replayed automatically during initialization, and before the connections are accepted. For information about the role of the redo log in crash recovery, see Section 15.18.2, “InnoDB Recovery”.
The server's binary log consists of files containing “events” that describe modifications to database contents. The server writes these files in binary format. To display their contents in text format, use the mysqlbinlog utility. You can also use mysqlbinlog to display the contents of relay log files written by a slave server in a replication setup because relay logs have the same format as binary logs. The binary log and relay log are discussed further in Section 6.4.4, “The Binary Log”, and Section 17.2.4, “Replication Relay and Status Logs”.