MySQL Select Database on Linux Via Command Line

Image Ronald Caldwell
Tutorials

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

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.

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 servers, or dedicated servers running Linux and MySQL for your next project, contact our sales team for more information.

Related articles

Wait! Get exclusive hosting insights

Subscribe to our newsletter and stay ahead of the competition with expert advice from our hosting pros.

Loading form…