Mysql ---查看数据库和表大小
查看mysql数据库大小的四种办法,分别有以下四种:
第一种:进去指定schema 数据库(存放了其他的数据库的信息)
use information_schema
第二种:查询所有数据的数据文件和索引文件 大小
select concat(round(sum(DATA_LENGTH/1024/1024),2),‘MB‘) as data_size from information_schema.TABLES;
select concat(round(sum(DATA_LENGTH/1024/1024),2),‘MB‘) as data_size,
CONCAT(TRUNCATE(SUM(index_length)/1024/1024,2),‘MB‘) AS index_size
from information_schema.TABLES;
第三种:查看指定数据库的数据文件和索引文件 的大小,比如说:数据库test
select concat(round(sum(DATA_LENGTH/1024/1024),2),‘MB‘) as data_size,
CONCAT(TRUNCATE(SUM(index_length)/1024/1024,2),‘MB‘) AS index_size
from information_schema.TABLES where table_schema=‘test‘;
第四种:查看指定数据库的表的数据文件和作引文件 大小,比如说:数据库test 中am21表
select concat(round(sum(DATA_LENGTH/1024/1024),2),‘MB‘) as data_size,
CONCAT(TRUNCATE(SUM(index_length)/1024/1024,2),‘MB‘) AS index_size
from information_schema.TABLES where table_schema=‘test‘ and table_name=‘am21‘;
综合查询在 5.7不适用 是因为sql_mode未设置
SELECT TABLE_SCHEMA, CONCAT(TRUNCATE(SUM(data_length)/1024/1024,2),‘ MB‘) AS data_size,
CONCAT(TRUNCATE(SUM(index_length)/1024/1024,2),‘MB‘) AS index_size
FROM information_schema.tables;
GROUP BY TABLE_SCHEMA
ORDER BY data_length DESC;
报错如下
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘GROUP BY TABLE_SCHEMA
ORDER BY data_length DESC‘ at line 1
解决办法--- echo "sql_mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"" >> /etc/my.cnf 重启mysql,再次执行
查看sql_mode
mysql -uroot -psunfuzhou -e "SHOW VARIABLES LIKE ‘SQL_MODE‘;"
设置sql_mode
临时设置sql_mode
mysql -uroot -psunfuzhou -e "SET sql_mode=‘NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION ‘"
全局设置sql_mode
mysql -uroot -psunfuzhou -e "SET global sql_mode=‘NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION ‘"
SET @@GLOBAL.sql_mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";
SELECT TABLE_SCHEMA, CONCAT(TRUNCATE(SUM(data_length)/1024/1024,2),‘ MB‘) AS data_size,
CONCAT(TRUNCATE(SUM(index_length)/1024/1024,2),‘MB‘) AS index_size
FROM information_schema.tables;
GROUP BY TABLE_SCHEMA
ORDER BY data_length DESC;