Size of Each Database
As an administrator, in sudo mysql or sudo mariadb:
SELECT table_schema AS db,
ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) AS size_mb
FROM information_schema.tables
GROUP BY table_schema
ORDER BY size_mb DESC;
An application account sees only the databases it has rights on, so run it as an administrator for the full picture. The same query appears in how to list databases in MySQL.
Size of Each Table
SELECT table_name,
ROUND(data_length / 1024 / 1024, 1) AS data_mb,
ROUND(index_length / 1024 / 1024, 1) AS index_mb,
ROUND(data_free / 1024 / 1024, 1) AS free_mb
FROM information_schema.tables
WHERE table_schema = 'app'
ORDER BY data_length + index_length DESC
LIMIT 20;
This lists the 20 largest tables of app. To find the largest tables on the whole server, remove the WHERE line and add table_schema to the selected columns.
What the Columns Mean
| Column | Meaning |
|---|---|
data_length | Rows, in bytes |
index_length | Secondary indexes |
data_free | Allocated, unused |
table_rows | Row estimate |
In InnoDB, the primary key is stored with the rows, so it counts in data_length. A large index_length compared with the data is normal for tables with many indexes.
Why the Numbers Lag Behind
The sizes are estimates the storage engine reports, not an exact count. MySQL 8.0 and later also cache them for up to 24 hours by default, set by information_schema_stats_expiry (86400 seconds). To refresh the figures for one table:
ANALYZE TABLE app.orders;
ANALYZE TABLE recalculates statistics and is light, but run it outside the busiest hours on very large tables.
Unused Space Inside Tables
When rows are deleted, InnoDB keeps the space for new rows instead of returning it to the disk, and data_free shows that space. OPTIMIZE TABLE app.orders; rebuilds the table and gives the space back. A rebuild needs about as much free disk as the table itself and is heavy on a large table, so check the space first and pick a quiet time. A table that will grow again soon does not need it.
Space on Disk
For the real use of the disk, look at the data directory, /var/lib/mysql, read-only:
sudo du -sh /var/lib/mysql
sudo du -sh /var/lib/mysql/* | sort -h | tail -n 15
Each database is a folder; with one file per table, which is InnoDB's default, each table is an .ibd file inside it. More about du is in how to check disk space in Linux. Look, but do not delete or move anything in this directory by hand: the server tracks every file in it.
Binary Logs
MySQL 8.0 and later write binary logs by default, files named binlog followed by a number, and they can grow larger than the data. MariaDB writes them only when log_bin is set. List them and remove old ones through the server, never with rm:
SHOW BINARY LOGS;
PURGE BINARY LOGS BEFORE NOW() - INTERVAL 7 DAY;
If a replica reads these logs, make sure it has processed them before you purge. binlog_expire_logs_seconds sets how long MySQL keeps them; the default is 30 days.
Size of a Dump
A dump made with mysqldump or mariadb-dump has a different size: it holds no indexes, only the statements to rebuild them, and no unused space. Use it to plan backup storage, not to measure the database. For the version you run, see how to check the MySQL version.