00:00

MySQL Show Databases

Last updated: 4 min read

When you connect to a MySQL server, the first thing you often want to know is which databases are here? The SHOW DATABASES statement lists them. This tutorial covers the statement, how to filter the list, how to get the same information (and more, such as sizes) from information_schema, and what privileges affect the result.

1. Listing All Databases

Our sample server has four user databases. List everything:

query.sql
SHOW DATABASES;

Result: 8 rows

Database
blog
hr_portal
information_schema
mysql
performance_schema
sales_2025
sales_2026
sys
DatabaseWhat it is
blog, hr_portal, sales_2025, sales_2026User databases created for applications
information_schemaVirtual, read-only metadata about all objects
mysqlAccounts, privileges and server settings
performance_schemaRuntime performance statistics
sysHelper views for diagnostics
SHOW SCHEMAS is an exact synonym of SHOW DATABASES.

2. Filtering with LIKE

Add a LIKE pattern to show only matching names. % matches any number of characters and _ matches exactly one:

query.sql
SHOW DATABASES LIKE 'sales%';

Result: 2 rows

Database (sales%)
sales_2025
sales_2026
query.sql
SHOW DATABASES LIKE '%\_portal';   -- \_ means a literal underscore

Result: 1 row

Database (%\_portal)
hr_portal
In a LIKE pattern an unescaped _ is a wildcard, so 'hr_portal' would also match 'hrXportal'. Escape it as \_ when you mean the underscore itself.

3. Filtering with WHERE

The result has one column, named Database. You can filter on it with WHERE, which allows any condition:

query.sql
SHOW DATABASES
WHERE `Database` NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys');

Result: 4 rows

Database
blog
hr_portal
sales_2025
sales_2026

4. Using information_schema.schemata

SHOW DATABASES is convenient, but a normal SELECT on information_schema.schemata gives more columns and can be sorted, joined and used in programs:

query.sql
SELECT schema_name, default_character_set_name, default_collation_name
FROM information_schema.schemata
WHERE schema_name NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
ORDER BY schema_name;

Result: 4 rows

SCHEMA_NAMEDEFAULT_CHARACTER_SET_NAMEDEFAULT_COLLATION_NAME
blogutf8mb4utf8mb4_0900_ai_ci
hr_portalutf8mb4utf8mb4_0900_ai_ci
sales_2025utf8mb4utf8mb4_0900_ai_ci
sales_2026utf8mb4utf8mb4_0900_ai_ci

5. How Many Tables Are in Each Database?

query.sql
SELECT s.schema_name AS db, COUNT(t.table_name) AS tables
FROM information_schema.schemata s
LEFT JOIN information_schema.tables t ON t.table_schema = s.schema_name
WHERE s.schema_name NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
GROUP BY s.schema_name
ORDER BY tables DESC, db;

Result: 4 rows

dbtables
sales_20262
hr_portal1
blog0
sales_20250

The LEFT JOIN keeps empty databases such as blog, showing 0 tables.

6. Finding the Size of Each Database

information_schema.tables holds approximate data and index sizes per table. Add them up per database:

size.sql
ANALYZE TABLE sales_2026.orders, sales_2026.customers, hr_portal.staff;
SELECT table_schema AS db,
       ROUND(SUM(data_length + index_length) / 1024, 1) AS size_kb
FROM information_schema.tables
WHERE table_schema IN ('sales_2026', 'hr_portal')
GROUP BY table_schema;

Result: 2 rows

dbsize_kb
hr_portal16.0
sales_202632.0
InnoDB allocates space in 16 KB pages, so even tiny tables report at least 16 KB. The numbers are estimates; run ANALYZE TABLE to refresh them.

7. From the Command Line

You can list databases without opening the interactive client:

Terminal
mysql -u root -p -e "SHOW DATABASES;"
mysqlshow -u root -p              # lists databases
mysqlshow -u root -p sales_2026   # lists the tables in one database

8. Why Some Users See Fewer Databases

SHOW DATABASES only lists databases on which you have some privilege, unless you have the global SHOW DATABASES privilege. Let's create a user who can only read blog:

limited-user.sql
CREATE USER 'writer'@'localhost' IDENTIFIED BY 'Wr1ter#2026';
GRANT SELECT ON blog.* TO 'writer'@'localhost';
SHOW GRANTS FOR 'writer'@'localhost';

Result: 2 rows

Grants for writer@localhost
GRANT USAGE ON *.* TO `writer`@`localhost`
GRANT SELECT ON `blog`.* TO `writer`@`localhost`

Now log in as writer and run the same statement:

as writer
SHOW DATABASES;

Result: 3 rows

Database
blog
information_schema
performance_schema

writer sees blog plus the two metadata databases every account can see, information_schema and performance_schema. The application databases hr_portal, sales_2025 and sales_2026 are hidden, which protects other teams' database names from view.

9. Listing Databases from Java

ListDatabases.java
import java.sql.*;

public class ListDatabases {
    public static void main(String[] args) throws SQLException {
        try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/", "root", "your-password");
             ResultSet rs = con.getMetaData().getCatalogs()) {      // MySQL databases are JDBC "catalogs"
            while (rs.next()) {
                System.out.println(rs.getString("TABLE_CAT"));
            }
        }
    }
}

10. Practice Exercises

Exercise 1: Show the databases whose names end with a 4-digit year starting with 20.
solution.sql
SHOW DATABASES LIKE '%20__';

Result: 2 rows

Database (%20__)
sales_2025
sales_2026
Exercise 2: Using information_schema, list user databases that contain no tables.
solution.sql
SELECT s.schema_name
FROM information_schema.schemata s
LEFT JOIN information_schema.tables t ON t.table_schema = s.schema_name
WHERE s.schema_name NOT IN ('mysql','information_schema','performance_schema','sys')
GROUP BY s.schema_name
HAVING COUNT(t.table_name) = 0;

Result: 2 rows

SCHEMA_NAME
sales_2025
blog

11. Summary

  • SHOW DATABASES (or SHOW SCHEMAS) lists the databases you are allowed to see.
  • Filter with LIKE 'pattern' or WHERE `Database` ....
  • information_schema.schemata gives the same list as a normal table, with character sets and collations.
  • Join with information_schema.tables for table counts and sizes.
  • Users without the global SHOW DATABASES privilege see only databases they have privileges on.