最近频繁接到数据库服务器磁盘空间不足的告警,实际上数据库文件本身不大,主要是半年多累积的binary logs大,尤其是有主从配置的DB。查看了很多资料,总结了一下常用的专门针对binary logs的mysql命令:

1. Binary logs 位置:

1)查看/etc/my.cnf (Windows下为my.ini), 例如:

log_bin=/var/mydb/bin-log

如果该设置没带路径,就放在datadir=/var/lib/mysql 下

2) 到数据库查看是否开启binary log 功能:

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

mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin             | OFF   |
+---------------+-------+
1 row in set (0.01 sec)

如果是OFF就没有以下工作了:)

2. 查看当前工作的logfile名及大小: show binary logs /show master logs;

mysql> show binary logs;
+-----------------+-----------+
| Log_name           | File_size |
+-----------------+-----------+
| bin-log.000011 |    148329 |
+-----------------+-----------+

mysql> show master logs;
+-----------------+-----------+
| Log_name           | File_size |
+-----------------+-----------+
| bin-log.000011 |    148329 |
+-----------------+-----------+

mysql> show binary logs;
ERROR 1381 (HY000): You are not using binary logging ###没有开启binary logs

3. 清除所有binary logs;

1)

mysql> show master logs;
+-----------------+-----------+
| Log_name        | File_size |
+-----------------+-----------+
| log-bin.000001 |    259548 |
| log-bin.000002 |     37200 |
| log-bin.000003 |     74219 |
+-----------------+-----------+
3 rows in set (0.00 sec)

mysql> reset master; (在slave上,用reset slave,之前应stop slave,之后再start slave)
Query OK, 0 rows affected (0.00 sec)

mysql> show master logs;
+-----------------+-----------+
| Log_name        | File_size |
+-----------------+-----------+
| log-bin.000001 |        98 |
+-----------------+-----------+
1 row in set (0.00 sec)

4. 清除指定部分logs:

mysql>purge binary logs to 'log-bin.000012';

将log-bin.000012之前的binary logs清掉;

mysql>purge binary logs before '2011-05-28 12:05:38';

将指定时间之前的binary logs清掉;

5.查看当前binary log的情况:

mysql>show master status;

6. 查看binary logs的内容:

mysql>show binlog events;

命令行下:

#mysqlbinlog /var/log/mysql/log-bin.000140; 或者

#mysqlbinlog --start-datetime='2011-07-01 00:00:00' --stop-datetime='2010-07-15 00:00:00' /var/log/mysql/log-bin.000020 > ./tmp.log

7. 在my.cnf/my.ini中设定binary logs回滚天数:

expire_logs_days = 7