Select a MySQL Database on Linux via Command Line

Posted on by Justin Palmer | Updated:
Reading Time: 2 minutes

Pre-Flight Check

  • These instructions are intended for selecting a MySQL database on Linux via the command line.
  • I’ll be working from a Liquid Web Core Managed CentOS 6.5 server, and I'll be logged in as root.
select a database via the command line

First we'll login to the MySQL server from the command line with the following command:

mysql -u root -p

In this case, I've specified the user root with the -u flag, and then used the -p flag so MySQL prompts for a password. Enter your current password to complete the login.

If you need to change your root (or any other) password in the database, then follow this tutorial on changing a password for MySQL via the command line.

You should now be at a MySQL prompt that looks very similar to this:


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. Which database is currently selected? To find out issue the following command:

SELECT database();

Your result may be similar to this:

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

The result is null, meaning a database is not currently selected.

Select a Database in MySQL

To select a database for use with subsequent MySQL operations, use the following command:

USE tutorial_database;

That command should yield a result of Database changed similar to:

mysql> USE tutorial_database;
Database changed

To verify the database has been selected simply issue the following command (which we ran previously):

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

Series Navigation
<< Previous ArticleNext Article >>
Avatar for Justin Palmer

About the Author: Justin Palmer

Justin Palmer is a professional application developer with Liquid Web

Latest Articles

How to Install, Enable, and Configure Webmin on Ubuntu 20.04

Read Article

How to Set Up a Cron Job in cPanel

Read Article

What Is Puppet Software and How Do You Use It?

Read Article

Accessing Man Pages on Ubuntu 16.04 LTS

Read Article

What is VMware Fusion?

Read Article