侧边栏壁纸
博主头像
★街角晚灯★博主等级

博观而约取 厚积而薄发

  • 累计撰写 448 篇文章
  • 累计创建 183 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

MySQL BinLog配置

WinJay
2023-05-16 / 0 评论 / 0 点赞 / 156 阅读 / 3566 字 / 正在检测是否收录...
温馨提示:
文章发布较早,内容可能过时,阅读注意甄别。。。。

MySQL BinLog配置

image-20230516092148295

查看binlog保存天数

  • 默认值为0,即永久保存
## 查看binlog的保留时长[expire_logs_days]
## 0表示永久保留
mysql> show variables like 'expire_logs_days';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| expire_logs_days | 0     |
+------------------+-------+
1 row in set (0.00 sec)

image-20230516093735732

配置binlog失效时间

  • 临时配置,重启MySQL后失效
## 设置日志保留时长为30天
mysql> set global expire_logs_days=30;
Query OK, 0 rows affected (0.00 sec)

mysql> flush logs;
Query OK, 0 rows affected (0.01 sec)

mysql>  show variables like 'expire_logs_days';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| expire_logs_days | 30    |
+------------------+-------+
1 row in set (0.00 sec)
  • 永久配置
# 修改my.cnf文件,永久生效,数据库启动的时候会自动加载该文件配置
## 打开my.cnf配置文件
[root@zxy_slave1 mysql]# vim /etc/my.cnf
## [mysqld]模块下添加:expire_logs_days
[mysqld]
expire_logs_days = 30

查看binlog大小限制

## 查看binlog的文件大小限制[max_binlog_size]
## 1073741824
mysql> show variables like 'max_binlog_size';
+-----------------+------------+
| Variable_name   | Value      |
+-----------------+------------+
| max_binlog_size | 1073741824 |
+-----------------+------------+
1 row in set (0.01 sec)

image-20230516093936987

配置binlog文件大小

  • 临时配置,重启MySQL后失效
## 将max_binlog_size设置为500M
mysql> set global max_binlog_size = 524288000;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'max_binlog_size';
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| max_binlog_size | 524288000 |
+-----------------+-----------+
1 row in set (0.00 sec)

mysql> flush logs;
Query OK, 0 rows affected (0.02 sec)
  • 永久配置
# 修改my.cnf文件,永久生效,数据库启动的时候会自动加载该文件配置
## 打开my.cnf配置文件
[root@zxy_slave1 mysql]# vim /etc/my.cnf
## [mysqld]模块下添加:max_binlog_size
[mysqld]
expire_logs_days = 30
max_binlog_size = 500M

清理binlog日志文件

  • 删除所有binlog日志
flush logs;
  • 按照时间删除
## 删除2022-04-21 18:08:00之前的binlog日志
mysql> purge binary logs before '2022-04-21 18:08:00';
Query OK, 0 rows affected, 1 warning (0.02 sec)
  • 按照名称删除
## 将mysql-bin.000010之前的日志清理掉
mysql> purge binary logs to 'mysql-bin.000010';
Query OK, 0 rows affected (0.01 sec)

设定binlog保留时间

  • 在 my.ini/my.cnf 文件中添加配置,设定保留时间:
[mysqld]
binlog_expire_logs_seconds=259200
# 顾名思义:单位为秒,259200 = 3天

重启 mysql 服务后执行 show variables like '%binlog_expire_logs_seconds%'; 验证是否生效,如果你原来binlog文件很多,那么你可以去磁盘上查看一下,多余的已经被自动清理了。

0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区