When working with Linux-based hosting environments, managing databases via the command line is a crucial skill — especially if you’re running a virtual server or dedicated server without a graphical interface. In this guide, we’ll walk you through how to list all MySQL databases using the terminal. Whether you’re an experienced sysadmin or a website owner learning the ropes, this article has you covered.

Why You Might Need to List MySQL Databases

Before diving into the commands, let’s quickly cover a few scenarios where this command is useful:

  • You’re managing multiple websites with different databases.
  • You need to confirm that a database was created successfully.
  • You’re troubleshooting issues or cleaning up unused databases.
  • You’re preparing for a backup or migration process.

Now let’s move to the actual commands.

Step 1: Access the MySQL CLI

To interact with MySQL via the command line, first log in to the MySQL shell:

mysql -u root -p
  • -u root — Specifies the MySQL user. Replace root with another user if needed.
  • -p — Prompts for the password (don’t include your password directly in the command for security reasons).

💡 Tip: If your MySQL root user doesn’t have a password set (not recommended for production), you can skip -p.

 Step 2: List All Databases

Once you’re inside the MySQL shell, simply run:

SHOW DATABASES;

You’ll see output similar to:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| your_database_name |
+--------------------+

Each row represents a database stored on the MySQL server. Some of these (like information_schema, performance_schema) are system databases and should not be modified.

Listing Databases Without Entering MySQL

If you prefer to list databases directly from the shell without opening the MySQL prompt, use:

mysql -u root -p -e 'SHOW DATABASES;'

This is useful for scripting and automation tasks.

 Filter or Search Specific Database Names

To filter output and find specific database names, you can combine the command with grep:

mysql -u root -p -e 'SHOW DATABASES;' | grep your_keyword

Replace your_keyword with part of the database name. This trick is handy when managing multiple clients or applications.

 Where Are MySQL Databases Stored on Linux?

If you’re curious about physical storage, MySQL databases are typically stored in:

/var/lib/mysql/

Each folder inside corresponds to a database name. Do not modify or delete anything here manually unless you know exactly what you’re doing — always use SQL commands or admin tools.

Common Errors & Fixes

Error: Access denied for user ‘root’@’localhost’

  • Double-check the username and password.
  • Ensure the MySQL service is running:
sudo systemctl status mysql

Error: Command ‘mysql’ not found

  • Install the MySQL client tools:
    sudo apt install mysql-client
    

Summary

To list MySQL databases via the command line on Linux:

  • Log in with mysql -u root -p
  • Use SHOW DATABASES; inside the MySQL shell
  • Or run mysql -u root -p -e ‘SHOW DATABASES;’ directly in terminal

This method is fast, secure, and works perfectly in SSH environments — exactly what you need for effective server and database management.