How to get size of mysql database?

MysqlDatabase

Mysql Problem Overview


How to get size of a mysql database?
Suppose the target database is called "v3".

Mysql Solutions


Solution 1 - Mysql

Run this query and you'll probably get what you're looking for:

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; 

This query comes from the mysql forums, where there are more comprehensive instructions available.

Solution 2 - Mysql

It can be determined by using following MySQL command

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

Result

Database	Size (MB)
db1	        11.75678253
db2	        9.53125000
test	    50.78547382

Get result in 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

Solution 3 - Mysql

Alternatively, if you are using phpMyAdmin, you can take a look at the sum of the table sizes in the footer of your database structure tab. The actual database size may be slightly over this size, however it appears to be consistent with the table_schema method mentioned above.

Screen-shot :

enter image description here

Solution 4 - Mysql

Alternatively you can directly jump into data directory and check for combined size of v3.myd, v3. myi and v3. frm files (for myisam) or v3.idb & v3.frm (for innodb).

Solution 5 - Mysql

To get a result in MB:

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

To get a result in 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";

Solution 6 - Mysql

mysqldiskusage  --server=root:MyPassword@localhost  pics

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

If not installed, this can be installed by installing the mysql-utils package which should be packaged by most major distributions.

Solution 7 - Mysql

First login to MySQL using

mysql -u username -p

> Command to Display the size of a single Database along with its table in 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;

Change database_name to your Database

> Command to Display all the Databases with its > size in 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;

Solution 8 - Mysql

If you want the list of all database sizes sorted, you can use :

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; 

Solution 9 - Mysql

Go into the mysql data directory and run du -h --max-depth=1 | grep databasename

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNewbieView Question on Stackoverflow
Solution 1 - MysqlBrian WillisView Answer on Stackoverflow
Solution 2 - MysqlNadeem0035View Answer on Stackoverflow
Solution 3 - MysqlJoelView Answer on Stackoverflow
Solution 4 - MysqlkedarView Answer on Stackoverflow
Solution 5 - MysqlwilliambarauView Answer on Stackoverflow
Solution 6 - MysqlRick JamesView Answer on Stackoverflow
Solution 7 - MysqlHiren ParghiView Answer on Stackoverflow
Solution 8 - MysqlJörg AsmussenView Answer on Stackoverflow
Solution 9 - MysqlEvan HastonView Answer on Stackoverflow