Mysql database optimized backup and recovery

Database space cleanup
 
 
The delete operation will bring some data fragments, it is these fragments are occupying hard disk space
The TABLES table in the information_schema database of MySQL records the space occupied by each table in the MySQL database, the number of rows recorded in the table, update time, description, etc. The main fields of this table are as follows:
TABLE_SCHEMA: database name
TABLE_NAME: table name
ENGINE: the storage engine used
TABLES_ROWS: the number of records, that is, the number of rows in the table
DATA_LENGTH: data size
INDEX_LENGTH: index size
CREATE_TIME: creation time
UPDATE_TIME: the latest update time
DATA_FREE: This parameter is related to mysql fragmentation. If it is a shared table space, this field indicates the size of the shared table space rather than the size of the data. Only when the exclusive table space is used, this field indicates the remaining space of the table;
Note: When MySQL deletes a row of content from the list, the space will be left blank. After a large number of delete operations are performed over a period of time, the fragmented space tends to become larger than the space used to store the contents of the list.
In layman's terms: the Data_free field is the occupied physical space. You can view the Data_free field of the specified table through 'show table status'. The corresponding value is the occupied physical space. Drop table reconstruction or re-import can release this part of space .
 
1. View the table structure of the database
USE information_schema;
SELECT table_name,table_rows FROM TABLES WHERE table_schema = 'school' ORDER BY table_rows DESC;
SELECT sum(table_rows) FROM tables WHERE table_schema = 'school';
SELECT table_name,table_rows FROM TABLES WHERE table_schema = 'school' ORDER BY table_rows DESC LIMIT 100;
select * from --Query the say table
order by id desc-Sort by id column size in descending order
limit 100, 15-Take one piece of data from the result of the query, that is, take out 15 pieces of data from the 100th
 
2. The size of the lookup table
SELECT TABLE_NAME, (DATA_LENGTH+INDEX_LENGTH)/1048576, TABLE_ROWS FROM information_schema.tables WHERE TABLE_SCHEMA='dbname' AND TABLE_NAME='tablename(你的表名)';
 
 
Open the Mysql command line, enter the database password, enter the database to be operated (the command line is: use databaseName;), and then run optimize table tableName;
 
Production case: release table space
 
Idea 1:
 
Data is not deleted: backup table, drop table, import backup table to database;
show table status like 'oldtb%';-> Focus on the Data_free field, the corresponding value is the physical space that is occupied more, that is, fragmented space
mysqldump -uroot -p123456 mydb oldtb> /mnt/oldtb.sql-> Export the table that can restore the system space.
drop table oldtb;-> drop old table to release table space
source /mnt/oldtb.sql;-> Export the backup table to the database.
show table status like 'oldtb%';-> Compare the status before and after recovery, focusing on the Data_free field
This operation uses multiple single-table operations and backs up and restores the tables multiple times, thereby freeing up fragmented space, and is suitable for businesses that do not allow downtime.
If the system business allows to stop the service within a period of time, you can back up the entire database, then empty, rebuild the mysql data root directory, and then restore the entire database.
 
 
Idea 2:
 
Delete some old data: suitable for scenarios where some old data can be deleted
CREATE TABLE IF NOT EXISTS `newtb` LIKE` course`;-> New newtb table, the table structure is the same as the old table
insert into newtb select * from oldtb where time > 'xxx' and time<='xxx';
-> Back up the new data of the table, you can back up the last month or half month according to the time period
drop table oldtb;-> drop old table to release table space
alter tables newtb rename to oldtb;-> rename new table "newdtb"
 
 
Idea 3: Direct optimization table: optimize table table name;
 
select table_name,data_free,engine from information_schema.tables where table_schema='mydb';
-> Check the data_free value of each table in the library, the unit is byte, data_free / 1024/1024/1024 is the theoretical recovery space G after release.
show table status like 'oldtb';-> Data_free size of a single table
mysql> OPTIMIZE TABLE  hellodb.students;
-> Show not supported, in fact, reconstruction and analysis have been carried out, and space has been recovered
 
 
 
 
database backup
1. Backup command
Format: cmd under mysqldump -h hostname -P port -u username -p password --database database name> d: \ filename.sql
For example: mysqldump -h 192.168.1.100 -p 3306 -uroot -ppassword --database cmdb> /data/backup/cmdb.sql;
 
2. Recover data
use database
source d:\mysql.sql
 

Guess you like

Origin www.cnblogs.com/YinXuanZhiZhi9/p/12719188.html