How to Select a Database in MySQL via Command Line

When working with MySQL Server, one of the initial things you need to do is select a database. Making changes to a MySQL database and executing MySQL choose database commands could be done by running them in a command line.

So in this tutorial, we will take a deep look at how to choose or select databases from a MySQL server with the help of a terminal or command line.

The Short and Straightforward Answer

To select a database, open the command line, log in as the user who works with the MySQL server, and then execute the following command for MySQL select the database you want. Write the name of the MySQL database you want to select after the ‘Use’ command, add a semicolon, and press Enter.

Use YourDatabaseName;

If it doesn’t work for you or need more details on how to select databases in MySQL, please continue with this tutorial.

Different Scenarios When Selecting a MySQL Database

Data is stored in tables, tables are in databases, databases are a part of servers, and a server is deployed on a host. Now, based on the type of host you are using, you may find yourself in one of the following scenarios:

  • Have installed your server on a local host such as a Linux or Windows computer.
  • Or you may be working with an online server like Linux OS or Windows Server.

To select a MySQL database, you need to open a terminal and run some commands inside it, in both situations.

This tutorial uses a Windows local host but you can still run the following commands and instructions on your desired operating system.

So let’s get started by preparing the prerequisites.

Prerequisites To Select a database in MySQL

Before you can run a command for MySQL select databases, you have first to check if MySQL Server is installed on your device (local or online host) and then make sure that it is correctly started and running.

If not, you can download MySQL Server from its official website on local Windows devices and install it. Then, open the Run dialog (Windows Key + R), and insert ‘services. music, open the Services window, look for MySQL80, right-click, and select Start.

Please note that the MySQL service may have a different name on your system.

To make sure that it is installed and running correctly, open the command line and run the following command.

Sc query MySQL80

Note: It is recommended to open the command prompt as an administrator and navigate to the MySQL installation directory before running select database MySQL or MySQL-related commands.

The State property should be equal to Running.
After this, you must connect or login to your MySQL Server with the Command below.

MySQL -u YourUserName -p

Next, you will be requested to insert the password associated with the user.

Use the command below to display the names of all available and existing databases.

Show databases;

With this step, you can ensure you’re connected and write down a database name for future reference in this tutorial.

Now that you are connected to your MySQL server, everything is now set for selecting a database.

MySQL Command to Select a Database

Now the question is how to select the database in MySQL, From the mysql command line select database, and simply write the MySQL Use database command followed by your database name, like at the beginning of this article.

Use YourDatabaseName;

As you can see in the screenshot above, I have a database named World. So I select the use database mysql command with the command below.

Use world;

If you select it correctly, you’ll see the message “Database changed.”

One Step Further – Selecting a Database on a Specific Server and Port

In some cases, you may want to connect to a specific server on your current device or a remote server. In such situations, use the command below to specify the desired server’s name or address and connect it while selecting a database in one go.

MySQL -h ServerNameOrIP -P PortNumber -u UserName -p -D DatabaseName

For example, I use the command below to connect to my local server (localhost) on the default MySQL port number (3306) and connect to my database (world) by running a single command in the command line.

MySQL -h localhost -P 3306 -u Amin -p -D world

Faster and easier!

To ensure you have selected your database with this unified command, run a sample command such as ‘show tables’ to see if you can access the database’s information like the screenshot above.

If you prefer using a graphical interface over command-line operations, dbForge Studio for MySQL offers an intuitive solution. With dbForge Studio, you can easily select databases through a user-friendly MySQL GUI that greatly simplifies complex tasks

Wrapping Up

In this article, you have learned how to work with MySQL select db commands or how to select the database in a MySQL server. No matter what operating system you have, or what host type you are using, the core steps and commands remain the same as presented in this tutorial.

Mastered the command line of selecting databases in MySQL, so fire up that terminal and explore different databases, experiment with queries, and watch your SQL skills. So, take your MySQL expertise to the next level with Ultahost’s dedicated hosting servers. Unlock the power of your databases without sacrificing security, control, or speed.

FAQ

How do I list all databases in MySQL using the command line?
What command selects a specific database in MySQL?
How can I check the currently selected database?
Is there a command to switch to a different database without re-entering MySQL?

Related Post

Remote MySQL in cPanel

Remote access to MySQL databases is an essential featur...

How to Install phpPgAdmin on Ubuntu 24.04

phpPgAdmin is a free, open-source web-based tool that s...

Installing and Securing phpMyAdmin on Ubuntu

Managing MySQL databases is a crucial part of web devel...

How to Install phpPgAdmin on Windows 10

phpPgAdmin is a free, web-based interface for managing ...

How to Install PostgreSQL on Windows

PostgreSQL, also known as Postgres is a powerful open-s...

How to Install Microsoft SQL Server on Window

SQL Server is a relational database management system (...

Leave a Comment