0. precondition
a) install mysql 5.7, for detail please refer my blog post.
1. login mysql and check the variables to see if the binlog has been enabled.
mysql -h 127.0.0.1 -uroot -proot show variables like '%log_bin%';
we can see, the log_bin is disabled.
2. Turn on mysql log_bin
sudo vim /etc/mysql/conf.d/mysql.cnf
add the following config segment at the end of the file
# ---------------------------------------------- # Enable the binlog for replication & CDC # ---------------------------------------------- # Enable binary replication log and set the prefix, expiration, and log format. # The prefix is arbitrary, expiration can be short for integration tests but would # be longer on a production system. Row-level info is required for ingest to work. # Server ID is required, but this will vary on production systems server-id = 223344 log_bin = /var/lib/mysql/mysql-bin expire_logs_days = 3 binlog_format = row
this configration means:
a) the server id is unique for each server, an is required for log_bin capture, it should be a numeric number equal or greater than 0, in my instance I set it to 223344, this number should be unique in the whole cluster. seems it's a good idea to set it as the ip
address number of the machine install. I fact I have do this in my real production enviroment.
b) the path of the log_bin, this is required to define the storage location fo the log_bin.
c) the log_bin retention time, in my case, I set it to 3 days.
d. the bin_log format, we should define it as row.
The whole definition file in my case is:
1 # For advice on how to change settings please see 2 # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html 3 4 [mysqld] 5 # 6 # Remove leading # and set to the amount of RAM for the most important data 7 # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. 8 # innodb_buffer_pool_size = 128M 9 # 10 # Remove leading # to turn on a very important data integrity option: logging 11 # changes to the binary log between backups. 12 # log_bin 13 # 14 # Remove leading # to set options mainly useful for reporting servers. 15 # The server defaults are faster for transactions and fast SELECTs. 16 # Adjust sizes as needed, experiment to find the optimal values. 17 # join_buffer_size = 128M 18 # sort_buffer_size = 2M 19 # read_rnd_buffer_size = 2M 20 skip-host-cache 21 skip-name-resolve 22 #datadir=/var/lib/mysql 23 #socket=/var/lib/mysql/mysql.sock 24 #secure-file-priv=/var/lib/mysql-files 25 user=mysql 26 27 # Disabling symbolic-links is recommended to prevent assorted security risks 28 symbolic-links=0 29 30 #log-error=/var/log/mysqld.log 31 #pid-file=/var/run/mysqld/mysqld.pid 32 33 # ---------------------------------------------- 34 # Enable the binlog for replication & CDC 35 # ---------------------------------------------- 36 37 # Enable binary replication log and set the prefix, expiration, and log format. 38 # The prefix is arbitrary, expiration can be short for integration tests but would 39 # be longer on a production system. Row-level info is required for ingest to work. 40 # Server ID is required, but this will vary on production systems 41 server-id = 223344 42 log_bin = /var/lib/mysql/mysql-bin 43 expire_logs_days = 3 44 binlog_format = row
3. restart mysql service
systemctl restart mysql
after the mysql restarted, we use the command
show variables like '%log_bin%';
and we should found the log_bin is turned on now: log_bin | ON
then we go to file system, and can fould like this :
the log bin files are just there now!
-rw-r----- 1 mysql mysql 177 Apr 20 15:06 mysql-bin.000001
-rw-r----- 1 mysql mysql 154 Apr 20 15:22 mysql-bin.000002
-rw-r----- 1 mysql mysql 64 Apr 20 15:22 mysql-bin.index
Notes: every time we restart the mysql server instance, it will call flush logs and then create a new binlog file.