Mysql空间清理的几种具体方法

目录

  • 前言
  • 1.查看文件磁盘占用
    • 1.1 查看磁盘空间占用
    • 1.2 查看目录空间占用
  • 2.Binlog日志清理
    • 2.1.定时自动清理Binlog日志
    • 2.2 手动删除Binlog日志
    • 2.3.Slow日志清理
    • 2.4.Error日志清理
  • 3、表清理
    • 3.1.查看表占空间和记录数
    • 3.2 常规表数据清理

前言 在Mysql环境下,常常由于数据磁盘满而导致Mysql故障。下面整理了如何在Mysql环境下做好Mysql的空间清理。

1.查看文件磁盘占用
1.1 查看磁盘空间占用
[root@mysqlhost01 /]# df -lh

Mysql空间清理的几种具体方法
文章图片



1.2 查看目录空间占用
[root@mysqlhost01 /]# du -sh /usr5.5G/usr

Mysql空间清理的几种具体方法
文章图片


2.Binlog日志清理
2.1.定时自动清理Binlog日志
mysql>show variables like '%expire_logs_days%'; --mysql 5.7mysql> show variables like '%binlog_expire_logs_seconds%'--mysql8.0mysql8.0mysql 8开始 expire_logs_days 废弃 启用binlog_expire_logs_seconds设置binlog自动清除日志时间保存时间 以秒为单位;默认2592000 30天144004小时;864001天;2592003天;mysql> set global binlog_expire_logs_seconds=86400; mysql5.7这个默认是0,也就是logs不过期,可通过设置全局的参数,使他临时生效:mysql>set global expire_logs_days=10;

Mysql空间清理的几种具体方法
文章图片



2.2 手动删除Binlog日志
第一步:登陆进入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。删除后就能释放大部分空间。

Mysql空间清理的几种具体方法
文章图片


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

Mysql空间清理的几种具体方法
文章图片


【Mysql空间清理的几种具体方法】Mysql空间清理的几种具体方法
文章图片


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

Mysql空间清理的几种具体方法
文章图片


3、表清理 大表,指单个数据文件磁盘占用大于100G,或者单个表数据记录量大于1亿。


3.1.查看表占空间和记录数
select table_schema,table_name,concat(round((data_length+index_length)/1024/1024/1024,2),'G') as tablesize_gb,table_rows from information_schema.tablesorder by tablesize_gb desc limit 5; table_schema:库名table_name :表名tablesize_gb:表占空间大小,以G为单位table_rows:行数

Mysql空间清理的几种具体方法
文章图片



3.2 常规表数据清理
常规表指没达到大表标准的。Delete语法:Delete from table_name [ where condition]Delete 只删除符合条件的数据,不会减少表所占空间。Delete大量数据后,会存在碎片,需要整理回收碎片空间optimize table table.name或者 alter table table.name engine='innodb' (会锁表,注意在业务低谷期执行)Truncate语法:Truncate table table_nameTruncate 删除全表数据,回收所占表空间。Drop语法:Drop table table_nameDrop 删除全表数据和表结构,回收所占表空间。

到此这篇关于Mysql空间清理的几种具体方法的文章就介绍到这了,更多相关Mysql空间清理内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读