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 >>

About the Author: Justin Palmer

Have Some Questions?

Our Sales and Support teams are available 24 hours by phone or e-mail to assist.


Latest Articles

5 Alternatives to GitHub

Read Article

5 Steps to Install and Configure osquery on CentOS 7 & 8

Read Article

Five Steps to Create a Robots.txt File for Your Website

Read Article

Premium Business Email Pricing FAQ

Read Article

Microsoft Exchange Server Security Update

Read Article