How to List MySQL Databases with Commands: A Comprehensive Guide
To list MySQL databases using the command line, log into the MySQL shell and execute the command SHOW DATABASES;. This command generates a list of all databases that the current user has permission to access. For shell-based environments, you can also use the...
To list MySQL databases using the command line, log into the MySQL shell and execute the command SHOW DATABASES;. This command generates a list of all databases that the current user has permission to access. For shell-based environments, you can also use the mysqlshow utility or the mysql -e flag to retrieve the list without entering the interactive prompt.
Contents
In the landscape of database management in 2026, the command-line interface (CLI) remains the most efficient and powerful way to interact with MySQL. Whether you are managing a legacy MySQL 8.0 instance or a modern MySQL 9.x deployment, knowing how to quickly audit and navigate your schemas is a foundational skill for developers and system administrators alike.
This guide provides an exhaustive look at the various commands used to list MySQL databases, ranging from basic interactive shell commands to complex queries involving system metadata and automated shell scripts.
Prerequisites for Accessing MySQL via Command Line
Before executing commands to list databases, you must establish a connection to the MySQL server. This requires a terminal (on Linux or macOS) or a Command Prompt/PowerShell window (on Windows).
The standard utility for this is the mysql client. To connect, you typically use the following syntax:
mysql -u [username] -p
Upon hitting Enter, the system will prompt you for your password. If the MySQL server is hosted on a remote machine, you must include the -h flag followed by the IP address or hostname. For security-hardened environments in 2026, many administrators use SSH tunneling or specific SSL/TLS certificates to secure this connection.
If you do not have the necessary "SELECT" or "SHOW DATABASES" privileges assigned to your user account, you may see an empty list or only the information_schema database. Ensuring your user has the correct administrative roles is the first step in successful database discovery.
The Core Command: SHOW DATABASES
The most direct way to see your data structures once logged into the MySQL monitor is the SHOW DATABASES statement. This command is part of the Data Definition Language (DDL) subset and is designed for administrative inspection.
Basic Syntax and Execution
In the MySQL prompt, simply type:
SHOW DATABASES;
Note the semicolon at the end; MySQL requires this to terminate the statement and begin execution. The output will be a formatted table listing every schema name available to your user.
Understanding the Default Databases
When you run this command on a fresh installation, you will notice several system-generated databases. These are critical for the server's operation:
- information_schema: This is a virtual database that acts as a data dictionary. It contains metadata about all other databases, such as table names, column types, and access privileges.
- mysql: This is the system database that stores information required by the MySQL server as it runs. This includes user accounts, permission tables, and log information.
- performance_schema: Used for monitoring MySQL server events at a low level. It provides insights into resource utilization and query performance.
- sys: Introduced to provide a more human-readable view of the
performance_schema, thesysschema helps administrators troubleshoot performance bottlenecks more effectively.
Filtering the Database List with Pattern Matching
In large-scale environments with hundreds of microservices, a simple list of every database can be overwhelming. MySQL allows you to filter the output of the SHOW DATABASES command using the LIKE or WHERE clauses.
Using the LIKE Clause
The LIKE operator uses wildcards to narrow down the results.
- The percent sign (
%) represents zero, one, or multiple characters. - The underscore (
_) represents a single character.
For example, if you only want to see databases that begin with "prod_", you would use:
SHOW DATABASES LIKE 'prod_%';
This is particularly useful in multi-tenant environments where naming conventions are strictly followed to distinguish between development, staging, and production environments.
Advanced Filtering with the WHERE Clause
In modern versions of MySQL, you can use the WHERE clause to apply more complex logic. This allows you to treat the list of databases like a table query. For example:
SHOW DATABASES WHERE Database NOT LIKE 'test%';
This command effectively hides any databases that are used for testing, allowing an administrator to focus only on functional schemas.
Listing Databases Directly from the System Shell
Sometimes, you need to list databases without entering the interactive MySQL shell. This is common when writing Bash scripts, automation routines, or CI/CD pipelines.
The mysqlshow Utility
MySQL provides a dedicated client program called mysqlshow. It is a diagnostic tool that shows information about databases, tables, and columns. To list all databases from your terminal, run:
mysqlshow -u [username] -p
The output is presented in an ASCII table, making it easy to read. This utility is favored by system administrators because it is faster than logging into the full MySQL client for a single piece of information.
Using the Execute (-e) Flag
A more flexible method for shell users is the -e (execute) flag within the standard mysql client. This allows you to pass a SQL command directly from the shell and receive the output.
mysql -u [username] -p -e "SHOW DATABASES;"
To make the output easier for scripts to parse, you can add the -s (silent) or -N (skip column names) flags:
mysql -u [username] -p -sN -e "SHOW DATABASES;"
This returns a clean, unformatted list of database names, which is ideal for piping into other Unix commands like grep, awk, or sort.
Deep Discovery via the Information Schema
While SHOW DATABASES is convenient, it provides very little detail. If you need to know when a database was created, its default character set, or its collation, you must query the information_schema.
The SCHEMATA table within the information_schema contains all the metadata for the databases on your server.
Querying Metadata
To get a list of databases along with their default character sets, use the following SQL query:
SELECT SCHEMA_NAME, DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA;
Why Query the Information Schema?
Querying the information_schema is superior to SHOW DATABASES in three scenarios:
- Auditing: Ensuring all databases are using
utf8mb4for modern emoji and international character support. - Reporting: Exporting database lists into CSV or JSON formats for corporate compliance reports.
- Automation: Writing scripts that only perform actions on databases that meet specific criteria (e.g., databases created after a certain date).
Comparison of Methods for Listing Databases
| Method | Execution Environment | Best Use Case | Metadata Detail |
|---|---|---|---|
| SHOW DATABASES | MySQL Interactive Shell | Quick manual check | Low (Name only) |
| mysqlshow | Operating System Shell | Quick diagnostic without SQL | Medium (Structural overview) |
| mysql -e "..." | Operating System Shell | Automation and Shell Scripting | Low (Unless combined with SELECT) |
| SELECT from SCHEMATA | MySQL Shell / Apps | Advanced auditing and metadata | High (Full schema details) |
Troubleshooting Common Issues
Even a simple command like listing databases can run into hurdles. Understanding these common failure points will save significant time during a migration or setup phase.
Permission Denied Errors
If you execute SHOW DATABASES and see an empty list (excluding information_schema), it is almost certainly a permissions issue. In MySQL's security model, a user can only "see" what they have been granted access to. To resolve this, a user with SUPER or GRANT privileges must run:
GRANT SHOW DATABASES ON *.* TO 'your_user'@'localhost';
Missing Semicolons
The MySQL CLI is a streaming interface. If you type SHOW DATABASES and hit Enter without a semicolon, the terminal will simply move to a new line with a -> prompt, waiting for you to finish the statement. Simply type ; and hit Enter again to execute.
Environment Variable Paths
On Windows 11 or Linux servers, if the mysql command is not recognized, the MySQL bin directory is likely not in your system's PATH variable. You must either navigate to the full path of the executable (e.g., /usr/bin/mysql or C:\Program Files\MySQL\MySQL Server 9.0\bin\mysql.exe) or update your environment variables.
FAQ: Frequently Asked Questions
How do I list databases and their sizes?
You cannot get database sizes using SHOW DATABASES. You must query the information_schema.TABLES. By summing the data_length and index_length columns and grouping by table_schema, you can calculate the total size of each database in megabytes.
Can I see hidden databases in MySQL?
MySQL does not have "hidden" databases in the traditional OS sense. However, if a user lacks the PROCESS or SELECT privilege on a specific schema, it will remain invisible to them. If you are logged in as the root user, SHOW DATABASES will always show the complete list.
Is SHOW SCHEMAS the same as SHOW DATABASES?
Yes. In MySQL, the terms "schema" and "database" are used interchangeably at the syntactic level. Running SHOW SCHEMAS; will produce the exact same output as SHOW DATABASES;. This is done to maintain compatibility with other SQL standards.
How do I list databases using a wildcard for specific characters?
You can use the underscore (_) wildcard. For example, SHOW DATABASES LIKE 'db_'; would find databases named db1, db2, or dbA, but it would not find db10 (because db10 has two characters after "db" instead of one).
What command shows the current database I am using?
If you have already selected a database with the USE command, you can verify which one is active by running SELECT DATABASE();. If no database is selected, this command will return NULL.
Next Steps for Database Management
Once you have successfully listed your MySQL databases, the next logical step is to explore the contents of a specific schema. Use the command USE [database_name]; to select a database, and then execute SHOW TABLES; to view the individual data structures within it.
For those looking to automate their 2026 workflows, consider integrating these commands into monitoring tools or custom administrative dashboards. Understanding the underlying metadata via the information_schema is particularly valuable for building robust, self-documenting database environments. If you are managing multiple instances, ensure that your connection strings and user permissions are audited regularly to prevent unauthorized discovery of your database architecture.