前言
在Mysql環境下,常常由於數據磁盤滿而導致Mysql故障。下面整理了如何在Mysql環境下做好Mysql的空間清理。
1.查看文件磁盤佔用
1.1 查看磁盤空間佔用
1
|
[root@mysqlhost01 /]# df -lh |
1.2 查看目錄空間佔用
1
2
|
[root@mysqlhost01 /] # du -sh /usr 5.5G /usr |
2.Binlog日誌清理
2.1.定時自動清理Binlog日誌
1
2
3
4
5
6
7
8
9
10
11
12
|
mysql>show variables like '%expire_logs_days%' ; --mysql 5.7 mysql> show variables like '%binlog_expire_logs_seconds%' --mysql8.0 mysql8.0 mysql 8開始 expire_logs_days 廢棄 啓用binlog_expire_logs_seconds設置binlog自動清除日誌時間 保存時間 以秒爲單位;默認2592000 30天 14400 4小時;86400 1天;259200 3天; mysql> set global binlog_expire_logs_seconds=86400; mysql5.7 這個默認是0,也就是logs不過期,可通過設置全局的參數,使他臨時生效: mysql> set global expire_logs_days=10; |
2.2 手動刪除Binlog日誌
1
2
3
4
5
6
7
8
9
10
|
第一步:登陸進入mysql,並使用 show binary logs; 查看日誌文件。 mysql>show binary logs; 第二步:查看正在使用的日誌文件:show master status; mysql>show master status; 當前正在使用的日誌文件是mysqlhost01-bin.000010,那麼刪除日誌文件的時候應該排除掉該文件。 刪除日誌文件的命令:purge binary logs to 'mysqlhost01-bin.000010' ; mysql>purge binary logs to 'mysqlhost01-bin.000010' ; 刪除除mysqlhost01-bin.000010以外的日誌文件 也可以指定其他文件名,例如mysql-bin.000003。 刪除後就能釋放大部分空間。 |
2.3.Slow日誌清理
1
2
3
4
5
6
7
|
步驟一 查看slow日誌模式 mysql>show variables like 'log_output%' ; 步驟二 查看Slow日誌文件位置 show variables like '%slow%' ; 步驟三 清空Slow日誌 [root@mysqlhost01 /]# cd /usr/ local /mysql57/mysql5730/data [root@mysqlhost01 data]# echo "" >mysqlhost01-slow.log |
2.4.Error日誌清理
1
2
3
4
5
6
|
步驟一 查看error日誌位置 mysql>show variables like 'log_error' ; 步驟二 查看error日誌大小 [root@mysqlhost01 data]# ll -h log.err 步驟三 清空error日誌 echo "" >/usr/ local /mysql57/mysql5730/data/log.err |
3、表清理
大表,指單個數據文件磁盤佔用大於100G,或者單個表數據記錄量大於1億。
3.1.查看錶佔空間和記錄數
1
2
3
4
5
6
7
8
9
|
select table_schema,table_name, concat(round((data_length+index_length)/1024/1024/1024,2), 'G' ) as tablesize_gb, table_rows from information_schema.tables order by tablesize_gb desc limit 5; table_schema:庫名 table_name :表名 tablesize_gb:表佔空間大小,以G爲單位 table_rows:行數 |
3.2 常規表數據清理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
常規表指沒達到大表標準的。 Delete 語法: Delete from table_name [ where condition] Delete 只刪除符合條件的數據,不會減少表所佔空間。 Delete 大量數據後,會存在碎片,需要整理回收碎片空間 optimize table table . name 或者 alter table table . name engine= 'innodb' (會鎖表,注意在業務低谷期執行) Truncate 語法: Truncate table table_name Truncate 刪除全表數據,回收所佔表空間。 Drop 語法: Drop table table_name Drop 刪除全表數據和表結構,回收所佔表空間。 |
到此這篇關於Mysql空間清理的幾種具體方法的文章就介紹到這了