mysql查看数据库大小方便进行数据备份和迁移

知道每个数据库的大小的话 进入 information_schema 数据库

# 1、查询所有数据大小 
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables;

# 2、查看指定数据库大小  查看 cms_test数据库 存储数据大小 
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables
 where TABLE_SCHEMA='cms_test';
 
 # 3、查看指定数据库的某个表的大小 比如查看数据库home中 members 表的大小
select concat(round(sum(data_length/1024/1024),2),‘MB’) as data from tables where table_schema=‘数据库’ and table_name=‘表名’;

-- DATA_LENGTH:数据长度(字节单位)
-- /1024/1024:字节转换为M
-- round:四舍五入,留2位小数
-- concat 拼接MB

查看指定数据库容量大小

select
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024/1024, 2)) as '数据容量(G)',
sum(truncate(index_length/1024/1024/1024, 2)) as '索引容量(G)'
from information_schema.tables
where table_schema='mysql'; 

看了一下
测试环境数据50G+索引10G 数据库容量差不多得需要80G勉强可以
线上环境数据40G+索引6G 数据库容量也是80G

查看各个数据库容量大小

select
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024/1024, 2)) as '数据容量(G)',
sum(truncate(index_length/1024/1024/1024, 2)) as '索引容量(G)'
from information_schema.tables
group by table_schema

数据库命令

1、将一个数据库迁移到另一个

# 导入测试环境数据库 到本地 scucess
mysqldump -hxxx -P端口 -u用户名 -p密码 数据库 | mysql -uroot -p3306  authority
# 本地测试 jwt 迁移到 jtw2 数据库需要存在才行 scucess
mysqldump -uroot -p3306 jwt | mysql -uroot -p3306  jwt2

猜你喜欢

转载自blog.csdn.net/qq_43566782/article/details/129750477