一、如何開啟
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