Several specific methods of Mysql space cleaning

Several specific methods of Mysql space cleaning

Preface

In the MySQL environment, MySQL failures are often caused by a full data disk. The following is a summary of how to clean up MySQL space in a MySQL environment.

1. Check the file disk usage

1.1 Check disk space usage

[root@mysqlhost01 /]# df -lh

insert image description here

1.2 Check the directory space usage

[root@mysqlhost01 /]# du -sh /usr
5.5G /usr

insert image description here

2.Binlog log cleaning

2.1. Automatically clean up Binlog logs regularly

mysql>show variables like '%expire_logs_days%'; --mysql 5.7
mysql> show variables like '%binlog_expire_logs_seconds%' --mysql8.0

mysql8.0
MySQL 8 starts with expire_logs_days. Disable binlog_expire_logs_seconds. Set binlog to automatically clear logs. Save time in seconds. Default: 2592000 for 30 days, 14400 for 4 hours, 86400 for 1 day, and 259200 for 3 days.
mysql> set global binlog_expire_logs_seconds=86400;

mysql5.7
The default value is 0, which means that logs do not expire. You can set the global parameters to make it temporarily effective:
mysql>set global expire_logs_days=10;

insert image description here

2.2 Manually delete Binlog logs

Step 1: Log in to MySQL and use show binary logs; to view the log file.
mysql>show binary logs;
Step 2: View the log file in use: show master status;
mysql>show master status;
The log file currently in use is mysqlhost01-bin.000010, so this file should be excluded when deleting the log file.
The command to delete the log file: purge binary logs to 'mysqlhost01-bin.000010';
mysql>purge binary logs to 'mysqlhost01-bin.000010';
Delete log files other than mysqlhost01-bin.000010. You can also specify another file name, such as mysql-bin.000003.
Deleting it will free up most of the space.

insert image description here

2.3.Slow log cleaning

Step 1: Check the slow log mode mysql>show variables like 'log_output%';
Step 2 Check the Slow log file location show variables like '%slow%';
Step 3 Clear the Slow log [root@mysqlhost01 /]# cd /usr/local/mysql57/mysql5730/data
[root@mysqlhost01 data]# echo "">mysqlhost01-slow.log

insert image description here

insert image description here

2.4.Error log cleaning

Step 1 Check the error log locationmysql>show variables like 'log_error';
Step 2 Check the error log size [root@mysqlhost01 data]# ll -h log.err
Step 3 Clear the error log echo "">/usr/local/mysql57/mysql5730/data/log.err

insert image description here

3. Table cleaning

A large table refers to a single data file that occupies more than 100 GB of disk space, or a single table that contains more than 100 million data records.

3.1. Check the table space and number of records

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: database name table_name: table name tablesize_gb: table size, in G units table_rows: number of rows 

insert image description here

3.2 General table data cleaning

Conventional tables refer to those that do not meet the standards of large tables.
Delete
Syntax: Delete from table_name [ where condition]
Delete only deletes the data that meets the conditions and will not reduce the space occupied by the table.
After deleting a large amount of data, there will be fragments, which need to be sorted and reclaimed. Optimize table table.name
Or alter table table.name engine='innodb' (will lock the table, please be careful to execute during the business off-peak period)


Truncate
Syntax: Truncate table table_name
Truncate deletes all table data and reclaims the occupied table space.

Drop
Syntax: Drop table table_name
Drop deletes all table data and table structure and reclaims the occupied table space.

This concludes this article on several specific methods of MySQL space cleaning. For more relevant MySQL space cleaning content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to clean up Alibaba Cloud MySQL space

<<:  How to implement html input drop-down menu

>>:  Detailed summary of web form submission methods

Recommend

How to reference jQuery in a web page

It can be referenced through CDN (Content Delivery...

Introduction to the use of CSS3 filter attribute

1. Introduction When writing animation effects fo...

How to write elegant JS code

Table of contents variable Use meaningful and pro...

Summary of some common uses of refs in React

Table of contents What are Refs 1. String type Re...

Index Skip Scan in MySQL 8.0

Preface MySQL 8.0.13 began to support index skip ...

VMware virtualization kvm installation and deployment tutorial summary

Virtualization 1. Environment Centos7.3 Disable s...

How to create a web wireframe using Photoshop

This post introduces a set of free Photoshop wire...

JavaScript implements displaying a drop-down box when the mouse passes over it

This article shares the specific code of JavaScri...

ElementUI implements sample code for drop-down options and multiple-select boxes

Table of contents Drop-down multiple-select box U...

Detailed explanation of three ways to import CSS files

There are three ways to introduce CSS: inline sty...

JavaScript to achieve magnifying glass effect

This article shares the specific code for JavaScr...

Detailed installation tutorial of Docker under CentOS

Docker is divided into CE and EE. The CE version ...