最近頻繁接到資料庫伺服器磁碟空間不足的警示,實際上資料庫檔案本身不大,主要是半年多累積的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