When working with MySQL in the command line, it is important to ensure proper selection of the database you wish to work with. Administering a server has its challenges without unnecessary errors due to an incorrect database selection. This article shows you how to select a MySQL database in Linux via the command line.
3 Step MySQL Command Line Select Database Guide
Step 1: Log into the MySQL Server
From the terminal, use the following command to log into the MySQL server. The root user is specified with the -u flag, and the -p flag causes MySQL to prompt for a password.
mysql -u root -p
At the prompt, enter your current password to complete the login. There are steps to changing a password for MySQL via the command line if that is necessary.
You reach the following MySQL prompt in the command line.
Step 2: 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.
Run the following command to determine which database is currently selected.
The following output displays. The NULL result means a database is not currently selected.
mysql> SELECT database(); +------------+ | database() | +------------+ | NULL | +------------+ 1 row in set (0.00 sec)
Step 3: Select a Database in MySQL
Use the following command to select a database for use with subsequent MySQL operations.
The following output displays, signifying the selected database changed.
mysql> USE tutorial_database; Database changed
Use the SELECT database(); command to verify the database has been selected. The following output displays, showing the database named tutorial_database is selected.
mysql> SELECT database(); +-------------------+ | database() | +-------------------+ | tutorial_database | +-------------------+ 1 row in set (0.00 sec)
Having walked through selecting a MySQL database in Linux via the command line, you are now ready to move on to other MySQL command line operations. This skill helps you ensure that you always work with the appropriate database at all times.
- Using MySQL Command Line to Create a User
- How to Use Disk Quotas in Dedicated Linux Servers With cPanel
- How to Use Disk Quotas in Dedicated Linux Servers with Plesk
- Remove a MySQL User on Linux via Command Line
- Remove Permissions for a MySQL User on Linux via Command Line
- Grant Permissions to a MySQL User on Linux via Command Line
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.