MySQL
CLI
database selection
command line
tutorial

How to select a MySQL database through CLI?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In the MySQL command-line client, selecting a database means setting the default schema for the current session. The core command is USE database_name;, but there are a few practical ways to do it depending on whether you want to choose the database after connecting or at login time.

Select the Database After Connecting

The most common flow is to connect first:

bash
mysql -u root -p

Then list available databases if needed:

sql
SHOW DATABASES;

After that, choose the one you want:

sql
USE my_app_db;

If the command succeeds, MySQL prints:

text
Database changed

From that point on, unqualified table names refer to tables in my_app_db for the current session.

Verify Which Database Is Active

If you are about to run schema changes or destructive queries, verify the current selection explicitly.

sql
SELECT DATABASE();

This small check prevents a surprising number of mistakes, especially when you work across development, staging, and production environments.

Select the Database at Login Time

You can also choose the database as part of the connection command:

bash
mysql -u root -p my_app_db

That starts the session already scoped to my_app_db, which is convenient for scripts and repetitive work.

Another equivalent pattern is:

bash
mysql -u root -p --database=my_app_db

This version is often easier to read in shell scripts where explicit flags are preferred.

Work Without Changing the Default Database

Even if you do not run USE, you can still reference objects with fully qualified names.

sql
SELECT * FROM my_app_db.users;

That is useful when comparing tables across databases or when you intentionally do not want to change the session default.

Still, for ordinary interactive work, USE is usually cleaner.

Common Errors

If USE my_app_db; fails, the usual reasons are:

  • the database does not exist
  • the connected user does not have permission to access it

You can inspect current grants with:

sql
SHOW GRANTS FOR CURRENT_USER;

If the user lacks access, selecting the database will fail even though the MySQL login itself succeeded.

Safe Workflow Tips

For day-to-day command-line work, a good habit is:

  1. connect
  2. run SELECT DATABASE();
  3. change with USE ...; if needed
  4. verify again before major writes

That is especially important when multiple shells stay open at once or when you use similar database names across environments.

One-Off Commands Without Entering the Shell

If you only need to run a single statement, you can also pass both the database and the SQL command directly from the shell:

bash
mysql -u root -p my_app_db -e "SELECT COUNT(*) FROM users;"

This is handy for scripts and operational checks because the database is selected explicitly for that one command and the session ends immediately afterward.

Session Scope Matters

The selected database lasts only for the current MySQL client session. Opening a new shell or reconnecting does not remember the previous USE command unless you pass the database again at login or configure your tooling to do so. That sounds obvious, but it explains many "wrong database" mistakes in day-to-day admin work.

Common Pitfalls

  • Assuming login alone selects the intended database when no database name was passed.
  • Running destructive SQL without checking SELECT DATABASE(); first.
  • Forgetting that USE changes only the current session, not future sessions.
  • Confusing authentication success with permission to access a specific database.
  • Relying on short table names when fully qualified names would make cross-database work clearer.

Summary

  • The standard command is USE my_app_db;.
  • You can also select the database at login with mysql -u user -p my_app_db.
  • Use SELECT DATABASE(); to confirm the active database.
  • Fully qualified table names work even without changing the default database.
  • The main failure modes are nonexistent databases and insufficient privileges.

Course illustration
Course illustration

All Rights Reserved.