Mysql空間清理的幾種具體方法
發(fā)布日期:2022-02-06 19:43 | 文章來源:站長之家
在Mysql環(huán)境下,常常由于數(shù)據(jù)磁盤滿而導(dǎo)致Mysql故障。下面整理了如何在Mysql環(huán)境下做好Mysql的空間清理。
1.查看文件磁盤占用
1.1 查看磁盤空間占用
[root@mysqlhost01 /]# df -lh

1.2 查看目錄空間占用
[root@mysqlhost01 /]# du -sh /usr 5.5G /usr

2.Binlog日志清理
2.1.定時(shí)自動(dòng)清理Binlog日志
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設(shè)置binlog自動(dòng)清除日志時(shí)間 保存時(shí)間 以秒為單位;默認(rèn)2592000 30天 14400 4小時(shí);86400 1天;259200 3天; mysql> set global binlog_expire_logs_seconds=86400; mysql5.7 這個(gè)默認(rèn)是0,也就是logs不過期,可通過設(shè)置全局的參數(shù),使他臨時(shí)生效: mysql>set global expire_logs_days=10;

2.2 手動(dòng)刪除Binlog日志
第一步:登陸進(jìn)入mysql,并使用 show binary logs; 查看日志文件。 mysql>show binary logs; 第二步:查看正在使用的日志文件:show master status; mysql>show master status; 當(dāng)前正在使用的日志文件是mysqlhost01-bin.000010,那么刪除日志文件的時(shí)候應(yīng)該排除掉該文件。 刪除日志文件的命令:purge binary logs to 'mysqlhost01-bin.000010'; mysql>purge binary logs to 'mysqlhost01-bin.000010'; 刪除除mysqlhost01-bin.000010以外的日志文件 也可以指定其他文件名,例如mysql-bin.000003。 刪除后就能釋放大部分空間。

2.3.Slow日志清理
步驟一 查看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日志清理
步驟一 查看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、表清理
大表,指單個(gè)數(shù)據(jù)文件磁盤占用大于100G,或者單個(gè)表數(shù)據(jù)記錄量大于1億。
3.1.查看表占空間和記錄數(shù)
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:行數(shù)

3.2 常規(guī)表數(shù)據(jù)清理
常規(guī)表指沒達(dá)到大表標(biāo)準(zhǔn)的。 Delete 語法:Delete from table_name [ where condition] Delete 只刪除符合條件的數(shù)據(jù),不會減少表所占空間。 Delete大量數(shù)據(jù)后,會存在碎片,需要整理回收碎片空間 optimize table table.name 或者 alter table table.name engine='innodb' (會鎖表,注意在業(yè)務(wù)低谷期執(zhí)行) Truncate 語法:Truncate table table_name Truncate 刪除全表數(shù)據(jù),回收所占表空間。 Drop 語法:Drop table table_name Drop 刪除全表數(shù)據(jù)和表結(jié)構(gòu),回收所占表空間。
到此這篇關(guān)于Mysql空間清理的幾種具體方法的文章就介紹到這了,更多相關(guān)Mysql空間清理內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
相關(guān)文章
關(guān)注官方微信