MySQL Select Database on Linux Via Command Line

Posted on by Ronald Caldwell | Updated:
Reading Time: 2 minutes

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.


select a database via the command line

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.


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.

Series Navigation
<< Previous ArticleNext Article >>
Avatar for Ronald Caldwell

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.

Latest Articles

How to install Puppet Server on Linux (AlmaLinux)

Read Article

Deploying web applications with NGINX HTTP Server

Read Article

Email security best practices for using SPF, DKIM, and DMARC

Read Article

Linux dos2unix command syntax — removing hidden Windows characters from files

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article