MySQL 历史 binlog 日志处理
一、问题的引出
MySQL binlog 如果不清理会占用服务器的空间,那么怎么处理binlog日志呢
二、解决办法
1、物理删除
- df -Th 查看磁盘空间
➜ bob@bob-pc ~ df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
udev devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs tmpfs 1.6G 2.4M 1.6G 1% /run
/dev/sda1 ext4 110G 101G 3.6G 97% /
tmpfs tmpfs 7.8G 399M 7.4G 6% /dev/shm
tmpfs tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/sdb1 ext4 458G 35G 400G 8% /data/mnt
tmpfs tmpfs 1.6G 68K 1.6G 1% /run/user/1000
- cat bin.index 文件看数据库binlog文件
➜ bob@bob-pc /data/mnt/mysql_data sudo cat bin.index
./bin.000112
./bin.000113
./bin.000114
./bin.000115
./bin.000116
-
rm -rf bin.000112 bin.000113 删除开始的数据
rm -rf bin.000112 bin.000113
-
vim bin.index 编辑bin.index 文件 ,删除掉刚刚删掉的binlog,不然下次mysql 重启会起不来
2、逻辑删除
(root@localhost:mysql3306.sock)[(none)]> show binary logs;
+------------+-----------+
| Log_name | File_size |
+------------+-----------+
| bin.000112 | 0 |
| bin.000113 | 0 |
| bin.000114 | 217 |
| bin.000115 | 194 |
| bin.000116 | 194 |
+------------+-----------+
5 rows in set (0.00 sec)
(root@localhost:mysql3306.sock)[(none)]> show variables like '%expire_logs_days%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| expire_logs_days | 7 |
+------------------+-------+
1 row in set (0.00 sec)
(root@localhost:mysql3306.sock)[(none)]> set global expire_logs_days = 4;
Query OK, 0 rows affected (0.00 sec)
这里修改/etc/my.cnf 重启mysql
3、手动清除binlog文件
登录机器 执行 删除3天前的MySQL binlog日志
(root@localhost:mysql3306.sock)[(none)]> PURGE MASTER LOGS BEFORE DATE_SUB(CURRENT_DATE, INTERVAL 3 DAY);
Query OK, 0 rows affected (0.01 sec)
(root@localhost:mysql3306.sock)[(none)]> show master logs ;
+------------+-----------+
| Log_name | File_size |
+------------+-----------+
| bin.000112 | 0 |
| bin.000113 | 0 |
| bin.000114 | 217 |
| bin.000115 | 194 |
| bin.000116 | 194 |
+------------+-----------+
删除mysql-bin.000114 之前的binlog 这里是个开区间(-无穷,bin.000114)
(root@localhost:mysql3306.sock)[(none)]> PURGE MASTER LOGS to 'bin.000114';
Query OK, 0 rows affected, 2 warnings (0.11 sec)
清除了运行,一下命令显示已经删除了
(root@localhost:mysql3306.sock)[(none)]> show master logs;
+------------+-----------+
| Log_name | File_size |
+------------+-----------+
| bin.000114 | 217 |
| bin.000115 | 194 |
| bin.000116 | 194 |
+------------+-----------+
3 rows in set (0.00 sec)