Select a MySQL Database on Linux via Command Line

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.

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:


Follow this tutorial if you have not yet created a database, or you want a list of databases.

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)

Tagged with:

Published by

Be Sociable, Share!
Here's $75, Launch a New VPS Today. Find out why 30,000 customers have chosen our Best-in-Class Performance & 24x7 Heroic Support.