1 Introduction
In MySQL, binlog refers to binary log, binary log file. This file records all DML operations of MySQL. Through binlog logs, we can do data recovery, master-slave replication, and so on. For operation and maintenance or architecture personnel, it is very important to enable the binlog log function.
2 open binlog
(How to open MySQL's binlog log? Two methods will be introduced below)
2.1 Method 1: Add parameters in the main configuration file my.cnf
In the main configuration file my.cnf, find the [mysqld] module, and add the following three lines of parameters.
log_bin=ON
log_bin_basename=/var/lib/mysql/mysql-bin
log_bin_index=/var/lib/mysql/mysql-bin.index
Parameter explanation:
- log_bin: Open the binlog log file, the default value is OFF.
- log_bin_basename: The base file name of the binlog log. MySQL will append an identifier after the file name to represent each binlog file.
- log_bin_index: The index file of the binlog file, which manages all binlog files.
2.2 Method 2: Use the log-bin parameter
If you are using MySQL 5.7 and above, it is more convenient to use the log-bin parameter. In the my.cnf configuration file, find the [mysqld] module and add the following line of parameters.
log-bin=/var/lib/mysql/mysql-bin
Parameter explanation:
- The function of this parameter is the same as that of the above three parameters. MySQL will automatically open the binlog log according to this configuration, and automatically set the log_bin_index file to the file name you specified followed by .index. The parameter log-bin specifies the base file name of the binlog file.
- It should be noted that if you use MySQL 5.7 and above, you must add an additional parameter server-id=123454 (randomly specify a character string that cannot be duplicated), otherwise an error will be reported when you restart the MySQL service.
Then, restart the mysql service.
2.3 Restart mysql service
- On CentOS 6, restart the MySQL service with the following command:
service mysqld restart
- On CentOS 7, restart the MySQL service with the following command:
systemctl restart mysqld
3 Verify whether binlog is enabled
After enabling the binlog log, we can log in to the mysql terminal or the Navicat client, and execute the following command to check whether the binlog log is successfully enabled:
show variables like '%log_bin%';
At the same time, in the /var/lib/mysql directory, you can see multiple mysql-bin files and a mysql-bin.index file, which indicates that the binlog log has been successfully enabled.