如何获取mysql数据库的大小? 假设目标数据库名为“v3”。


运行这个查询,你可能会得到你想要的:

SELECT table_schema "DB Name",
        ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) "DB Size in MB" 
FROM information_schema.tables 
GROUP BY table_schema; 

这个查询来自mysql论坛,那里有更全面的说明可用。


或者,您可以直接跳转到数据目录并检查v3的组合大小。myd, v3。Myi和v3。FRM文件(用于myisam)或v3。Idb & v3.frm(用于innodb)。


或者,如果使用phpMyAdmin,可以在数据库结构选项卡的页脚中查看表大小的总和。实际的数据库大小可能略大于这个大小,但它似乎与上面提到的table_schema方法一致。

的截图:


要获得以MB为单位的结果:

SELECT
SUM(ROUND(((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024), 2)) AS "SIZE IN MB"
FROM INFORMATION_SCHEMA.TABLES
WHERE
TABLE_SCHEMA = "SCHEMA-NAME";

要获得GB的结果:

SELECT
SUM(ROUND(((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024 / 1024), 2)) AS "SIZE IN GB"
FROM INFORMATION_SCHEMA.TABLES
WHERE
TABLE_SCHEMA = "SCHEMA-NAME";

mysqldiskusage  --server=root:MyPassword@localhost  pics

+----------+----------------+
| db_name  |         total  |
+----------+----------------+
| pics     | 1,179,131,029  |
+----------+----------------+

如果没有安装,可以通过安装mysql-utils包来安装,大多数主要发行版都应该打包这个包。


可以使用下面的MySQL命令来确定

SELECT table_schema AS "Database", SUM(data_length + index_length) / 1024 / 1024 AS "Size (MB)" FROM information_schema.TABLES GROUP BY table_schema

结果

Database    Size (MB)
db1         11.75678253
db2         9.53125000
test        50.78547382

以GB为单位获取结果

SELECT table_schema AS "Database", SUM(data_length + index_length) / 1024 / 1024 / 1024 AS "Size (GB)" FROM information_schema.TABLES GROUP BY table_schema

进入“mysql data”目录,执行命令du -h——max-depth=1 | grep databasename


第一次登录MySQL使用

Mysql -u username -p

命令以MB为单位显示单个数据库及其表的大小。

SELECT table_name AS "Table",
ROUND(((data_length + index_length) / 1024 / 1024), 2) AS "Size (MB)"
FROM information_schema.TABLES
WHERE table_schema = "database_name"
ORDER BY (data_length + index_length) DESC;

将database_name更改为您的Database

命令显示具有其属性的所有数据库 大小以MB为单位。

SELECT table_schema AS "Database", 
ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (MB)" 
FROM information_schema.TABLES 
GROUP BY table_schema;

如果你想要排序所有数据库大小的列表,你可以使用:

SELECT * 
FROM   (SELECT table_schema AS `DB Name`, 
           ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) AS `DB Size in MB`
        FROM   information_schema.tables 
        GROUP  BY `DB Name`) AS tmp_table 
ORDER  BY `DB Size in MB` DESC; 

另外:如果有人想获取单个表的大小,请使用以下代码:

SELECT
  TABLE_NAME AS `Table Name`,
  ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024) AS `Size ( in MB)`
FROM
  information_schema.TABLES
WHERE
    TABLE_SCHEMA = "your_db_name"
  AND
    TABLE_NAME = "your_single_table_name"
ORDER BY
  (DATA_LENGTH + INDEX_LENGTH)
DESC;

注意:它不会显示使用ROUND()方法的分数。

希望这能对我们中的许多人有所帮助。