Linux system MySQL binary log (binary log)

Concept
Binary Log (Binary Log), also called Change Log (Update Log), is a very important log in MySQL. It is mainly used to record changes in the database, that is, DDL and DML statements of SQL statements, and does not include data record query operations.

Function:
Mainly used for data recovery, master-slave replication, and audit operations during disasters.

Usage experience
In actual work, it is not recommended to put the binary log file and the database data file on the same hard disk. The advantage is that even if the hard disk where the data file is located is damaged, the binary log on another hard disk can be used to restore the database file. . The possibility of two hard drives failing at the same time is much smaller, which ensures the security of the data in the database.

1. View binary log startup status

mysql> show variables like 'log_bin';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| log_bin                         | OFF   |
+---------------------------------+-------+
# OFF表示未开启,ON表示开启

2. View the binary log file

myslq> show binary logs;
myslq> show master logs; 	# 与上条命令一样
myslq> show master status;  # 查看当前正在写入的二进制日志文件

3. Enable binary log log-bin

vi /etc/my.cnf
#!修改my.cnf,在[mysqld]下面增加log-bin=dir/[filename],其中,dir 参数指定二进制文件的存储路径;
# filename 参数指定二进制文件的文件名,其形式为 filename.number,number 的形式为 000001、000002 等。
# 然后重启MySQL即可。每次重启 MySQL 服务后,都会生成一个新的二进制日志文件,这些日志文件的文件名中 filename 部分不会改变,number 会不断递增。
# 同时也要添加参数 server-id(随便一个唯一值)。如下所示:
# 配置文件中加入以下两行即可开启二进制日志文件,且文件会保存在mysql数据存放目录下
[mysqld]
log-bin=mysql-bin 	# 日志文件会默认存放在mysql数据库存放目录下
# log-bin=/home/ichroma/mysqllog/mysql-bin 	# 日志文件设置存放位置为/home/ichroma/mysqllog/
server-id=1

4. View the contents of the binary log file

# 二进制日志使用二进制格式存储,不能直接打开查看。如果需要查看二进制日志,必须使用 mysqlbinlog 命令。
mysqlbinlog filename.number
sudo mysqlbinlog  /usr/local/LAMP/mysql/var/mysql-bin.000017
# 也可以将日志文件拷贝成log或txt格式后查看
sudo mysqlbinlog  /usr/local/LAMP/mysql/var/mysql-bin.000017  >  /home/ichroma/log.log
sudo mysqlbinlog  /usr/local/LAMP/mysql/var/mysql-bin.000017  >  /home/ichroma/log.txt

5. Define binary log expiration time

# 修改my.cnf,在[mysqld]下面增加expire_logs_days参数,不设置的话,默认值是0,即不自动清除。下面参数10表示天数
expire_logs_days = 10   # 设置日志过期时间为10天,到时间则自动删除日志文件 

6. Define the binary log file size

# 修改my.cnf,在[mysqld]下面增加max_binlog_size参数,不设置的话,默认值为1GB。
# 下面设置表示设置每个日志文件大小为100M。如果二进制日志写入的内容大小超出给定值,日志就会发生滚动(关闭当前文件,重新打开一个新的日志文件)。
# 不能将该变量设置为大于 1GB 或小于 4096B(字节)。
max_binlog_size = 100M

7. Clean the log files of mysql database

mysql> reset master;   # 删除所有日志,删除所有二进制日志文件。并重新创建二进制文件,扩展名从000001开始
mysql> PURGE MASTER LOGS TO 'mysql-bin.000010';  # 删除文件名编号比指定文件名编号小的所有日志文件
mysql> purge {master | binary} logs before 'date';     # 删除指定日期以前的所有日志文件

8. Pause the binary log function

# 我们可以在执行 SQL 语句之前执行下面SQL语句暂停或启动二进制日志功能
set sql_log_bin = 0; 	# 暂停
set sql_log_bin = 1; 	# 启动

Guess you like

Origin blog.csdn.net/qq_34125713/article/details/127918514