mysql> system ls -ltr localhost-bin -rw-rw---- 1 mysql mysql 145 Aug 10 04:04 localhost-bin.000006 -rw-rw---- 1 mysql mysql 144 Aug 10 04:04 localhost-bin.index -rw-rw---- 1 mysql mysql 98 Aug 10 04:04 localhost-bin.000009 -rw-rw---- 1 mysql mysql 145 Aug 10 04:04 localhost-bin.000008 -rw-rw---- 1 mysql mysql 145 Aug 10 04:04 localhost-bin.000007
在测试环境中,二进制日志格式为“hostname-bin.”后跟日志序号而测试机的 hostname 设置为“localhost”,因此这里用“localhost-bin.”来表示所有的日志结果中的 “localhost-bin.index”是日志的索引文件,记录了最大的日志序号,本例中可以将此文件忽略(2)用 RESET MASTER 命令进行日志删除mysql> reset master; Query OK, 0 rows affected (0.08 sec)
(3)查看删除后的日志mysql> system ls -ltr localhost-bin -rw-rw---- 1 mysql mysql 36 Aug 10 04:04 localhost-bin.index -rw-rw---- 1 mysql mysql 98 Aug 10 04:04 localhost-bin.000001
可以发现,以前的日志全部被清空新日志重新从“000001”开始编号2.方法 2执行“PURGE MASTER LOGS TO 'mysql-bin.'”命令,该命令将删除“”编号之前的所有日志下例中删除了“localhost-bin.000006”之前编号的所有日志(1)查看删除前日志mysql> system ls -ltr localhost-bin -rw-rw---- 1 mysql mysql 145 Aug 10 04:00 localhost-bin.000004 -rw-rw---- 1 mysql mysql 98 Aug 10 04:00 localhost-bin.000006 -rw-rw---- 1 mysql mysql 145 Aug 10 04:00 localhost-bin.000005 -rw-rw---- 1 mysql mysql 108 Aug 10 04:00 localhost-bin.index
(2)用 PURGE 命令进行删除mysql> purge master logs to 'localhost-bin.000006'; Query OK, 0 rows affected (0.04 sec)
(3)查看删除后日志mysql> system ls -ltr localhost-bin-rw-rw---- 1 mysql mysql 98 Aug 10 04:00 localhost-bin.000006 -rw-rw---- 1 mysql mysql 36 Aug 10 04:03 localhost-bin.index
从结果中发现,编号“000006”之前的所有日志都已经被删除3.方法 3执行“PURGE MASTER LOGS BEFORE 'yyyy-mm-dd hh24:mi:ss'”命令,该命令将删除日期为 “yyyy-mm-dd hh24:mi:ss”之前产生的所有日志下例中删除了日期在“2007-08-10 04:07:00” 之前的所有日志(1)查看删除前日志mysql> system ls -ltr localhost-bin -rw-rw---- 1 mysql mysql 145 Aug 10 04:06 localhost-bin.000001 -rw-rw---- 1 mysql mysql 145 Aug 10 04:06 localhost-bin.000002 -rw-rw---- 1 mysql mysql 145 Aug 10 04:06 localhost-bin.000003 -rw-rw---- 1 mysql mysql 145 Aug 10 04:06 localhost-bin.000004 -rw-rw---- 1 mysql mysql 145 Aug 10 04:06 localhost-bin.000005 -rw-rw---- 1 mysql mysql 252 Aug 10 04:07 localhost-bin.index -rw-rw---- 1 mysql mysql 98 Aug 10 04:07 localhost-bin.000007 -rw-rw---- 1 mysql mysql 145 Aug 10 04:07 localhost-bin.000006
(2)用 PURGE 命令删除“2007-08-10 04:07:00”之前的所有日志mysql> purge master logs before '2007-08-10 04:07:00'; Query OK, 0 rows affected (0.04 sec)
(3)查看删除后日志,果然,系统只保留了两个指定删除日期后的日志mysql> system ls -ltr localhost-bin -rw-rw---- 1 mysql mysql 98 Aug 10 04:07 localhost-bin.000007 -rw-rw---- 1 mysql mysql 145 Aug 10 04:07 localhost-bin.000006 -rw-rw---- 1 mysql mysql 72 Aug 10 04:08 localhost-bin.index
4.方法 4设置参数--expire_logs_days=#,此参数的含义是设置日志的过期天数,过了指定的天数后日志将会被自动删除,这样将将有利于减少 DBA 管理日志的工作量下例中通过手工更改系统日期来测试此参数的使用(1)查看删除前日志mysql> system ls -ltr localhost-bin. -rw-rw---- 1 mysql mysql 145 Dec 25 00:00 localhost-bin.000041 -rw-rw---- 1 mysql mysql 145 Dec 25 00:06 localhost-bin.000042 -rw-rw---- 1 mysql mysql 144 Dec 25 00:06 localhost-bin.index -rw-rw---- 1 mysql mysql 98 Dec 25 00:06 localhost-bin.000044 -rw-rw---- 1 mysql mysql 145 Dec 25 00:06 localhost-bin.000043
(2)在 my.cnf 的[mysqld]中加入“expire_logs_day=3”,然后重新启动 MySQL 服务[root@localhost zzx]# more /etc/my.cnf [mysqld] ndbcluster log-bin expire_logs_day=3[root@localhost log]# service mysql restart Shutting down MySQL.[ OK ] Starting MySQL[ OK ]
(3)将系统时间改为 1 天以后[root@localhost mysql]# date Tue Dec 25 01:14:19 CST 2007 [root@localhost mysql]# date -s '20071226 14:00:00' Wed Dec 26 14:00:00 CST 2007
(4)用“flush logs”触发日志文件更新,这是由于没有到 3 天,所有日志将不会被删除[root@localhost mysql]# mysqladmin flush-log [root@localhost mysql]# ls -ltr |grep 'localhost-bin' -rw-rw---- 1 mysql mysql 145 Dec 25 00:00 localhost-bin.000041 -rw-rw---- 1 mysql mysql 145 Dec 25 00:06 localhost-bin.000042 -rw-rw---- 1 mysql mysql 145 Dec 25 00:06 localhost-bin.000043 -rw-rw---- 1 mysql mysql 145 Dec 26 14:00 localhost-bin.000044 -rw-rw---- 1 mysql mysql 216 Dec 26 14:00 localhost-bin.index -rw-rw---- 1 mysql mysql 98 Dec 26 14:00 localhost-bin.000046 -rw-rw---- 1 mysql mysql 145 Dec 26 14:00 localhost-bin.000045
(5)将日期改为 3 天以后,再次执行“flush logs”触发日志文件更新[root@localhost mysql]# date -s '20071228 14:00:00' Fri Dec 28 14:00:00 CST 2007 [root@localhost mysql]# mysqladmin flush-log [root@localhost mysql]# ls -ltr |grep 'localhost-bin' -rw-rw---- 1 mysql mysql 145 Dec 26 14:00 localhost-bin.000044 -rw-rw---- 1 mysql mysql 145 Dec 26 14:00 localhost-bin.000045 -rw-rw---- 1 mysql mysql 144 Dec 28 14:00 localhost-bin.index -rw-rw---- 1 mysql mysql 98 Dec 28 14:00 localhost-bin.000047 -rw-rw---- 1 mysql mysql 145 Dec 28 14:00 localhost-bin.000046
从结果中可以看出,3 天前日志 localhost-bin.000041-- localhost-bin.000043 都已经被删除搜索微信公众号:TestingStudio霍格沃兹的干货都很硬核(图片来源网络,侵删)
0 评论