How to Select a MySQL Database on Linux via Command Line

mysql select database

Even a mid-sized project has several databases that store important user and application data.

During the process of delivering services to the users and application maintenance, you’ll perform a wide range of tasks, such as table manipulation, user management, and permission management.

Since there are multiple databases in your project, you need to be careful in executing the right operation on the right database. This is especially important when you realize that most operations can permanently alter data in a database.

Table Of Content

This article will cover the process of how you can select a MySQL database in Linux via command line.

Let’s start with the prerequisites.

Prerequisites

To select a MySQL database in Linux via command line, you’ll need:

  • A VPS running Ubuntu 20.04
  • MySQL 8.0 or higher
  • A user with root privileges 

This is the setup of our test server. The good thing about this setup is that you can apply the steps of this tutorial in almost all MySQL installations with similar results.

Let’s start with the process of selecting a MySQL database on Linux via command line.

Select a MySQL Database on Linux via Command Line

To start the process, make sure you’re logged in as a user with admin privileges. Next, launch the terminal.
Log into the MySQL Server
Use the following command to log into the MySQL server.

#mysql -u root -p

Note that the -u flag specifies the root user, and the -p flag instructs MySQL to demand a password.

Mysql

You’ll notice that the prompt changes to mysql> to indicate that you have successfully logged into the MySQL server.

Find Out The Selected Database

Every SQL command you enter works on a database. Hence, it’s important that you find out which database is selected at the moment. For this, use the following command:

mysql> SELECT database();

Important: If you see NULL in the result of this command, no database is currently selected.

Database2

Select a Database

In response to the previous command, chances are that you find that no database is selected (NULL) or the database selected is not the one you wish to work with. This can be easily fixed with the following command:

mysql> USE <database name>;

Use Database

You’ll get the notification that the Database has been changed. However, it’s always a good practice to verify that the database you want to work with is selected at the moment.

For this, we’ll use the SELECT command once more.

mysql> SELECT database();

Select Database 2

As you can see, the output shows that the redswitches_databae is selected at the moment. This confirms that the USE command we executed in the previous step has successfully done its job.

Now that you’ve confirmed that the right database has been selected, you can now execute other commands with full confidence that you’re working on the right data.

Also Read: How To Rename a Database in MySQL: 3 Easy Methods

Conclusion

In conclusion, selecting a database in a MySQL database on Linux via command line is a crucial step in managing and modifying data. This can be achieved by connecting to the MySQL server using the MySQL command-line client and using the USE command to select the desired database.

Let us know how you use the SELECT and USE commands in your daily database administration.