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:
bashmysql -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:
sqlUSE 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:
bashmysql -u admin -p
After entering the password and accessing the MySQL CLI, run:
sqlUSE 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.