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

A brief talk about JavaScript variable promotion

Table of contents Preface 1. What variables are p...

Detailed explanation of common methods of JavaScript arrays

Table of contents Common array methods pop() unsh...

How to store false or true in MySQL

MySQL Boolean value, stores false or true In shor...

Three ways to achieve background blur in CSS3 (summary)

1. Normal background blur Code: <Style> htm...

The difference and usage between div and span

Table of contents 1. Differences and characterist...

Docker container exits after running (how to keep running)

Phenomenon Start the Docker container docker run ...

Reasons why MySQL kill cannot kill threads

Table of contents background Problem Description ...

Vue3 compilation process-source code analysis

Preface: Vue3 has been released for a long time. ...

js dynamically implements table addition and deletion operations

This article example shares the specific code for...

Summary of some efficient magic operators in JS

JavaScript now releases a new version every year,...

Solution to Incorrect string value in MySQL

Many friends will report the following error when...

MySQL 5.7.17 installation and use graphic tutorial

MySQL is a relational database management system ...