centos 7 mysql 5.7 开启binlog
一、如何开启
5.7版本,直接在配置文件中指定:
[mysqld]
log-bin=mysql-bin
server-id=1
binlog_format=ROW
接下来是两年前写这篇博客时候的踩坑记录,不感兴趣的,可以直接跳过该部分,直接跳到第二章:
https://blog.csdn.net/king_kgh/article/details/74800513
按照上面的步骤(这个教程应该是近期作者更新过了,下面踩的坑已经被修复了)操作,结果启动失败。
然后查看我这边的配置文件:
[mysqld]
log_bin=ON
log_bin_basename=/var/lib/mysql/mysql-bin
log_bin_index=/var/lib/mysql/mysql-bin.index
启动失败后,发现日志文件/var/log/mysqld.log竟然是空的,什么都没有。
启动失败提示如下:
于是照着提示执行了:
systemctl status mysqld.service
这个并没看出任何的错误线索。
接着执行:
journalctl -xe
这里就清楚了,原来是没有设置server-id。
那就指定一下吧。
log_bin=ON
log_bin_basename=/var/lib/mysql/mysql-bin
log_bin_index=/var/lib/mysql/mysql-bin.index
server-id=123454
再次重启。
结果又报错了,查看/var/log/mysqld.log,发现提示如下:
2019-02-26T06:50:46.581796Z 0 [ERROR] unknown variable 'log_bin_basename=/var/lib/mysql/mysql-bin'2019-02-26T06:50:46.581811Z 0 [ERROR] Aborting
好吧。。。这网上的教程坑多啊。
后边直接看了官网:
16.1.2.1 Setting the Replication Master Configuration
To configure a master to use binary log file position based replication, you must enable binary logging and establish a unique server ID. If this has not already been done, a server restart is required.
Binary logging must be enabled on the master because the binary log is the basis for replicating changes from the master to its slaves. If binary logging is not enabled on the master using the
log-bin
option, replication is not possible.Each server within a replication group must be configured with a unique server ID. This ID is used to identify individual servers within the group, and must be a positive integer between 1 and (232)−1. How you organize and select the numbers is your choice.
To configure the binary log and server ID options, shut down the MySQL server and edit the
my.cnf
ormy.ini
file. Within the[mysqld]
section of the configuration file, add thelog-bin
andserver-id
options. If these options already exist, but are commented out, uncomment the options and alter them according to your needs. For example, to enable binary logging using a log file name prefix ofmysql-bin
, and configure a server ID of 1, use these lines:[mysqld]
log-bin=mysql-bin
server-id=1After making the changes, restart the server.
于是,把原来的配置改成和官网一样。
二、查看binlog文件
1、mysql> show variables like '%log_bin%';
2、查看目录
3、查看当前正在写入的binlog文件
show master status;
4、mysql> show binlog events;
查看当前正在写入的日志文件中的binlog事件(看不出具体内容,只能看个大概)
5、mysql> show binlog events in 'mysql-bin.000001';
查看指定的文件
内容同上。
6、mysql> show binary logs;
显示文件列表。
7、用mysqlbinlog查看binlog详情
https://www.cnblogs.com/snifferhu/p/5280489.html
https://www.cnblogs.com/lvzf/p/10689462.html
我这边尝试了一下,我先修改了一条记录,如下是修改前,file_url是xxxxxx:
修改后如下:
接下来,我们查看binlog中对于本次修改的具体内容:
mysqlbinlog --base64-output=AUTO -v -d cad binlog.000023
如果不熟悉这些命令的选项,可以man mysqlbinlog
查看。
8、查看binlog相关参数
show variables like "%binlog%";
标红的都是我目前知道的,比较重要的。
在innodb存储引擎那本书里,重点说了下面的几个参数(也很值得了解下):
更新于2020-07-20