乐闻世界logo
搜索文章和话题

How to select a MySQL database through CLI?

1个答案

1

When managing a MySQL database using the Command Line Interface (CLI), you first need to log in to the MySQL service using the MySQL CLI tool. The following are basic steps and examples:

Step 1: Log in to the MySQL Server

First, log in to the MySQL server using the mysql command with necessary parameters such as the username and server information:

bash
mysql -u username -p

Here, -u specifies your database username, and after executing this command, the system will prompt you to enter your password. If the database runs on a non-default port or a remote server, you may also need to specify the -h (hostname) or -P (port) parameters.

Step 2: Select a Database

Once logged in, you will see the MySQL command-line interface. To select a database, use the USE statement:

sql
USE database_name;

Replace database_name with the name of the database you wish to work with.

Example:

Assume your database user is admin, password is password123, and the target database is inventory. The following shows the complete command sequence from login to selecting the database:

bash
mysql -u admin -p

After entering the password and accessing the MySQL CLI, run:

sql
USE inventory;

After completing these steps, you have successfully selected the inventory database and can proceed with further operations such as querying, updating, or deleting data.

These basic steps enable you to effectively select and operate on a MySQL database via CLI.

2024年8月6日 23:56 回复

你的答案