正确清理mysql-bin

Posted 聂炳玉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了正确清理mysql-bin相关的知识,希望对你有一定的参考价值。

1. 背景

机器磁盘满导致mysql连接不上,删除部分日志,错误将mysql-bin.0050* 都删除,重启mysql失败

$ service mysqld start
Starting MySQL.......... ERROR! The server quit without updating PID file (/data/mysqldata/gzqc249-243-214-g249hqzltcs1.pid).

2. 解决方法

MySQL 数据存放的绝对路径

$ grep datadir /etc/my.cnf
datadir = /data/mysqldata

先备份 /data/mysqldata/mysql-bin.index 然后清空 /data/mysqldata/mysql-bin.index。

启动mysql:

service mysqld start

3. 原因

mysql-bin是数据库的操作日志文件,mysql-bin.index中记录的是文件已经删除,mysql-bin.index内容需要清空。

4. 正确清理binlog

4.1 设置过期删除时间

查看当前到期时间:

> show variables like 'expire_logs_days'; 
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| expire_logs_days | 30    |
+------------------+-------+

将到期时间由30天设置为10天。

方法一:编辑配置文件 vim /etc/my.cnf

expire_logs_days = 10  //二进制日志自动删除的天数。

此方法需重启mysql服务

方法二:无需重启mysql服务

> set global expire_logs_days = 10;
Query OK, 0 rows affected (0.00 sec)

MariaDB [vss_stock_option_svr_multi]> show variables like 'expire_logs_days'; 
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| expire_logs_days | 10    |
+------------------+-------+
1 row in set (0.00 sec)

4.1 手动删除binlog文件

PURGE MASTER LOGS BEFORE DATE_SUB(CURRENT_DATE, INTERVAL 1 DAY);   //删除1天前的MySQL binlog日志

删除1天前的MySQL binlog日志

> show binary logs;
+------------------+------------+
| Log_name         | File_size  |
+------------------+------------+
| mysql-bin.002338 | 1073742660 |
| mysql-bin.002339 | 1073742551 |
| mysql-bin.002340 | 1073742466 |
| mysql-bin.002341 | 1073742538 |
| mysql-bin.002342 | 1073742770 |
| mysql-bin.002343 | 1073742574 |
| mysql-bin.002344 | 1073742084 |
| mysql-bin.002345 | 1073742809 |
| mysql-bin.002346 | 1073742195 |
| mysql-bin.002347 | 1074775919 |
| mysql-bin.002348 | 1073742659 |
| mysql-bin.002349 | 1073742546 |
| mysql-bin.002350 | 1073741966 |
| mysql-bin.002351 | 1073741891 |
| mysql-bin.002352 | 1073742204 |
| mysql-bin.002353 | 1073989969 |
| mysql-bin.002354 | 1073742107 |
| mysql-bin.002355 | 1073743414 |
| mysql-bin.002356 | 1073742527 |
| mysql-bin.002357 | 1073742335 |
| mysql-bin.002358 | 1073742868 |
| mysql-bin.002359 | 1073742808 |
| mysql-bin.002360 | 1073742326 |
| mysql-bin.002361 | 1073742219 |
| mysql-bin.002362 | 1073742028 |
| mysql-bin.002363 | 1073742256 |
| mysql-bin.002364 | 1073742147 |
| mysql-bin.002365 | 1073742558 |
| mysql-bin.002366 | 1073742303 |
| mysql-bin.002367 | 1073742538 |
| mysql-bin.002368 | 1073741929 |
| mysql-bin.002369 | 1073742054 |
| mysql-bin.002370 | 1073742387 |
| mysql-bin.002371 |  894966812 |
+------------------+------------+
34 rows in set (0.00 sec)

> PURGE MASTER LOGS BEFORE DATE_SUB(CURRENT_DATE, INTERVAL 1 DAY); 
Query OK, 0 rows affected (0.01 sec)

MariaDB [vss_stock_option_svr_multi]> show binary logs;
+------------------+------------+
| Log_name         | File_size  |
+------------------+------------+
| mysql-bin.002359 | 1073742808 |
| mysql-bin.002360 | 1073742326 |
| mysql-bin.002361 | 1073742219 |
| mysql-bin.002362 | 1073742028 |
| mysql-bin.002363 | 1073742256 |
| mysql-bin.002364 | 1073742147 |
| mysql-bin.002365 | 1073742558 |
| mysql-bin.002366 | 1073742303 |
| mysql-bin.002367 | 1073742538 |
| mysql-bin.002368 | 1073741929 |
| mysql-bin.002369 | 1073742054 |
| mysql-bin.002370 | 1073742387 |
| mysql-bin.002371 |  894966812 |
+------------------+------------+
13 rows in set (0.00 sec)

操作说明:

  • 操作前最好备份数据库
  • 不推荐直接在/var/lib/mysql(通过 grep datadir /etc/my.cnf 查找路径)路径下,将binlog.0*删除掉,因为手动删除并不会更新binlog.index,而 binlog.index 的作用是加快查找 binlog 文件的速度。

mysql binlog日志自动清理及手动删除

https://blog.csdn.net/atco/article/details/24259333

 

说明:
当开启mysql数据库主从时,会产生大量如mysql-bin.00000* log的文件,这会大量耗费您的硬盘空间。
mysql-bin.000001
mysql-bin.000002
mysql-bin.000003
mysql-bin.000004
mysql-bin.000005

有三种解决方法:1.关闭mysql主从,关闭binlog;2.开启mysql主从,设置expire_logs_days;3.手动清除binlog文件,> PURGE MASTER LOGS TO ‘MySQL-bin.010′;

实现:
1.关闭mysql主从,关闭binlog

# vim /etc/my.cnf  //注释掉log-bin,binlog_format
# Replication Master Server (default)
# binary logging is required for replication
# log-bin=mysql-bin
# binary logging format - mixed recommended
# binlog_format=mixed

然后重启数据库

2.重启mysql,开启mysql主从,设置expire_logs_days

# vim /etc/my.cnf  //修改expire_logs_days,x是自动删除的天数,一般将x设置为短点,如10
expire_logs_days = x  //二进制日志自动删除的天数。默认值为0,表示“没有自动删除”

此方法需要重启mysql,附录有关于expire_logs_days的英文说明

当然也可以不重启mysql,开启mysql主从,直接在mysql里设置expire_logs_days

> show binary logs;
> show variables like \'%log%\';
> set global expire_logs_days = 10;

 
3.手动清除binlog文件

# /usr/local/mysql/bin/mysql -u root -p
> PURGE MASTER LOGS BEFORE DATE_SUB(CURRENT_DATE, INTERVAL 10 DAY);   //删除10天前的MySQL binlog日志,附录2有关于PURGE MASTER LOGS手动删除用法及示例
> show master logs;

 
也可以重置master,删除所有binlog文件:

# /usr/local/mysql/bin/mysql -u root -p
> reset master;  //附录3有清除binlog时,对从mysql的影响说明

 
附录:
1.expire_logs_days英文说明

Where X is the number of days you’d like to keep them around. I would recommend 10, but this depends on how busy your MySQL server is and how fast these log files grow. Just make sure it is longer than the slowest slave takes to replicate the data from your master.
Just a side note: You know that you should do this anyway, but make sure you back up your mysql database. The binary log can be used to recover the database in certain situations; so having a backup ensures that if your database server does crash, you will be able to recover the data.

2.PURGE MASTER LOGS手动删除用法及示例,MASTER和BINARY是同义词

> PURGE {MASTER | BINARY} LOGS TO \'log_name\'
> PURGE {MASTER | BINARY} LOGS BEFORE \'date\'

删除指定的日志或日期之前的日志索引中的所有二进制日志。这些日志也会从记录在日志索引文件中的清单中被删除MySQL BIN-LOG 日志,这样被给定的日志成为第一个。

实例:

> PURGE MASTER LOGS TO \'MySQL-bin.010\';  //清除MySQL-bin.010日志
> PURGE MASTER LOGS BEFORE \'2008-06-22 13:00:00\';   //清除2008-06-22 13:00:00前binlog日志
> PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 3 DAY);  //清除3天前binlog日志BEFORE,变量的date自变量可以为\'YYYY-MM-DD hh:mm:ss\'格式。

 
3.清除binlog时,对从mysql的影响
如果您有一个活性的从属服务器,该服务器当前正在读取您正在试图删除的日志之一,则本语句不会起作用,而是会失败,并伴随一个错误。不过,如果从属服务器是休止的,并且您碰巧清理了其想要读取的日志之一,则从属服务器启动后不能复制。当从属服务器正在复制时,本语句可以安全运行。您不需要停止它们。

以上是关于正确清理mysql-bin的主要内容,如果未能解决你的问题,请参考以下文章

如果维度/事实表中的数据未正确加载,我需要执行哪些步骤来清理数据

Flyway 没有正确清理数据库,执行了两次迁移文件

在将数据输出回文本区域时,如何正确清理从文本区域接收的数据?

如何正确清理视图模型?

中断事件循环后清理的正确方法是啥?

关闭和清理 Socket 连接的正确方法是啥?