Select a MySQL Database on Linux via Command Line
MySQL via Command Line 102: Basic User Interaction
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:
mysql>
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)
Related Articles:
About the Author: Justin Palmer
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.
Latest Articles
How to Edit Your DNS Hosts File
Read ArticleHow to Edit Your DNS Hosts File
Read ArticleMicrosoft Exchange Server Security Update
Read ArticleHow to Monitor Your Server in WHM
Read ArticleHow to Monitor Your Server in WHM
Read Article