MySQL Select Database on Linux Via Command Line
When working with MySQL in the command line, it is important to ensure proper selection of the database you wish to work with. Administering a server has its challenges without unnecessary errors due to an incorrect database selection. This article shows you how to select a MySQL database in Linux via the command line.
Prerequisites
- Linux server running CentOS or AlmaLinux with MySQL.
- Root level access to the server and logged in as root.
- Create a MySQL database.
3 Step MySQL Command Line Select Database Guide
Step 1: Log into the MySQL Server
From the terminal, use the following command to log into the MySQL server. The root user is specified with the -u flag, and the -p flag causes MySQL to prompt for a password.
mysql -u root -p
At the prompt, enter your current password to complete the login. There are steps to changing a password for MySQL via the command line if that is necessary.
You reach the following MySQL prompt in the command line.
mysql>
Step 2: View Selected Database in MySQL
When executing commands via the MySQL command line, a database may be explicitly specified. However, by default, all MySQL operations run via the command line are performed on the currently selected database.
Run the following command to determine which database is currently selected.
SELECT database();
The following output displays. The NULL result means a database is not currently selected.
mysql> SELECT database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
Step 3: Select a Database in MySQL
Use the following command to select a database for use with subsequent MySQL operations.
USE tutorial_database;
The following output displays, signifying the selected database changed.
mysql> USE tutorial_database;
Database changed
Use the SELECT database(); command to verify the database has been selected. The following output displays, showing the database named tutorial_database is selected.
mysql> SELECT database();
+-------------------+
| database() |
+-------------------+
| tutorial_database |
+-------------------+
1 row in set (0.00 sec)
Wrapping Up
Having walked through selecting a MySQL database in Linux via the command line, you are now ready to move on to other MySQL command line operations. This skill helps you ensure that you always work with the appropriate database at all times.
Liquid Web supports these and other operations for existing customers with managed Linux servers. If you are in search of VPS Hosting, Cloud Dedicated Servers, or Dedicated Servers running Linux and MySQL for your next project, contact our sales team for more information.
Related Articles:
- Using MySQL Command Line to Create a User
- How to Use Disk Quotas in Dedicated Linux Servers With cPanel
- How to Use Disk Quotas in Dedicated Linux Servers with Plesk
- Remove a MySQL User on Linux via Command Line
- Remove Permissions for a MySQL User on Linux via Command Line
- Grant Permissions to a MySQL User on Linux via Command Line

About the Author: Ronald Caldwell
Ron is a Technical Writer at Liquid Web working with the Marketing team. He has 9+ years of experience in Technology. He obtained an Associate of Science in Computer Science from Prairie State College in 2015. He is happily married to his high school sweetheart and lives in Michigan with her and their children.
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.
Latest Articles
CentOS Linux 7 EOL — everything you need to know
Read ArticleHow to install Node.js on Linux (AlmaLinux)
Read ArticleUpgrading or installing PHP on Ubuntu — PHP 7.2 and Ubuntu 22.04
Read ArticleWhy is your IP blocked?
Read ArticleLiquid Web Terraform Provider Deploying WordPress
Read Article