How to Select a MySQL Database on Linux via Command Line

By Raju

February 1, 2017

In this tutorial you are going to learn how to select a MySQL database via command line on Linux server. Also, you'll learn basic MySQL database operations through below steps.

Let's get straight to the point.

How to access MySQL command line or shell?

If MySQL server already installed on your server, then you can access it through shell or command line.

First, login to the server as a root and then enter below command to access MySQL shell interface.

You can use any of the below commands to access MySQL command line.

mysql -u root -p

In the above command u flag indicates root user, and -p flag for password authentication.

You have to enter password to access MySQL.


Now, you are ready to select a database and work on it!

How to Select a MySQL Database?

When you are working on MySQL databases through command line, you have to select a database on which you are going execute SQL queries. Creating a database does not select it for use; you must do that explicitly.

If you haven't yet created database follow this article.

To make a database "sampledb" the current database, execute this below query:

mysql> USE sampledb;

Your database needs to be created only once, but you must select it for use each time you begin a mysql session. You can do this by issuing a USE statement as shown in the example.

The above command gives you following output and shows that database has changed.

mysql> USE sampledb;
Database changed

You can verify the database has been selected by executing the following command.

mysql> SELECT database();
| database() |
| sampledb |
1 row in set (0.00 sec)

TIP: All the database names, table names, table fields name are case sensitive. So you would have to use proper names while giving any SQL command.

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Create a website in 3 simple steps

Choose a website template, add features, then customise! - Free Online Website builder.