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:
Then list available databases if needed:
After that, choose the one you want:
If the command succeeds, MySQL prints:
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.
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:
That starts the session already scoped to my_app_db, which is convenient for scripts and repetitive work.
Another equivalent pattern is:
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.
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:
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:
- connect
- run
SELECT DATABASE(); - change with
USE ...;if needed - 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:
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
USEchanges 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.

