mysql 导出数据及技巧
常规用法
mysqldump -u root -p -h 172.16.0.2 --column-statistics=0 --single-transaction my_table > my_table.sql
–column-statistics=0 解决Unknown table ‘COLUMN_STATISTICS’ in…,高版本mysqldump导低版本数据库时用
–single-transaction 导出时不锁表
排除日志表
mysqldump -u root -p -h 172.16.0.2 --column-statistics=0 --single-transaction my_table --ignore-table=my_table.log > my_table.sql
–ignore-table=my_table.log 排除my_table数据库中的log表
只导结构
mysqldump -u root -p -h 172.16.0.2 --column-statistics=0 --opt -d --single-transaction my_table > my_table_struct.sql
–opt -d 只导结构
不用count查行数
有些表数据可以不用导出,但记录数非常多,如5000万条,用count()计数非常慢,这种情况下可以用SHOW TABLE STATUS来查看,但这个方法innodb表的计数是一个近似值
SHOW TABLE STATUS WHERE Name = 'log'\G;
SHOW TABLE STATUS 字段说明
SHOW TABLE STATUS WHERE Name = 'log'\G;
*************************** 1. row ***************************
Name: log ##表名
Engine: InnoDB ##存储引擎名字
Version: 10 ## The version number of the table's .frm file
Row_format: Dynamic ##行记录格式,对于MyIsam,它可能是Dynamic,对于Innodb引擎,它可能是(Redundant or Compact),这几种行记录格式区别后面会做介绍
Rows: 10437 ##该值对于MyIsam存储引擎是一个精确值,但对于innodb和其他存储引擎就是一个近似值,且与实际有这40%~50%之间的误差
Avg_row_length: 36 ##平均每行的长度(单位是字节)
Data_length: 376832 ##对于myisam来说,它表示数据文件的长度(单位:字节),对于Innodb来说,它表示聚集索引分配的大约的空间量(单位:字节),具体是聚集索引(数据页数量)乘以每页的大小
Max_data_length: 0 ##这个是只针对Myisam存储引擎,表示该表最大可以容纳多少字节
Index_length: 180224 ##表示索引的长度(占用空间的大小),单位是字节
Data_free: 0 ##分配但未使用的字节数
Auto_increment: NULL
Create_time: 2021-04-06 09:51:14 ##表的创建日期
Update_time: NULL
Check_time: NULL
Collation: utf8mb4_general_ci ##表默认排序
Checksum: NULL
Create_options:
Comment: