MySQL Command-Line Client (CLI)
MySQL Command-Line Client (CLI) Essentials
While graphical tools like MySQL Workbench are convenient, true database professionals and backend engineers must master the MySQL Command-Line Interface (CLI). Server deployments, Docker containers, remote cloud servers (AWS EC2, Google Cloud Compute), and CI/CD automated deployment pipelines interact with databases purely via terminal commands.
1. Connecting to MySQL via the CLI
To connect to a local or remote MySQL Server from your terminal:
When prompted, type your password. Note that on Unix/Linux systems, no asterisks or characters are shown while typing passwords for security. Press Enter.
Once successfully logged in, you will be greeted with the MySQL prompt:
2. Navigating the MySQL Terminal
Inside the mysql> prompt, every SQL statement must terminate with a semicolon (;) or \G.
3. Semicolons & Multiline SQL Statements
If you press Enter without a semicolon, MySQL expects you to continue typing on the next line. Notice how the prompt changes:
mysql>: Ready for a new command.->: Continuation of the current multiline statement.'>: Unclosed single quote waiting for closing quote.">: Unclosed double quote waiting for closing quote.- `
>: Unclosed backtick waiting for closing backtick.
If you make a typo and want to cancel the current multiline statement without executing it, type \c and press Enter:
4. Vertical Output Formatting (\G)
When inspecting wide tables with 20+ columns, the standard horizontal table layout wraps around the terminal and becomes unreadable. In MySQL CLI, end your statement with \G (capital G) instead of a semicolon to format results vertically!
Example Vertical Output:
5. Exiting the MySQL CLI
To exit the MySQL monitor and return to your operating system shell:
Multiple Choice Questions
1. Which flag in the command mysql -u root -p indicates that a password will be provided?
A. -P B. -p C. -pass D. -pwd Answer: B Explanation: In the MySQL CLI command line, lowercase -p prompts for the user password, whereas uppercase -P is used to specify the port number.
2. How do you cancel an unfinished multiline command in the MySQL CLI without executing it?
A. Press ESC twice B. Type \c and press Enter C. Type CANCEL and press Enter D. Close the terminal window Answer: B Explanation: Typing \c clears the current input buffer and returns you immediately to the standard mysql> prompt.
3. What does terminating a SQL query with \G instead of a semicolon accomplish in the MySQL CLI?
A. It runs the query in debug mode B. It displays each row's columns vertically line-by-line rather than horizontally C. It rolls back the transaction immediately D. It saves the query to a log file Answer: B Explanation: Terminating with \G prints results vertically, presenting each column as a separate row—indispensable for inspecting wide records.
4. What does the prompt '> indicate in the MySQL CLI?
A. The server is disconnected B. The command was executed successfully C. The CLI is waiting for the user to close an opened single quote string literal D. An error has occurred Answer: C Explanation: The prompt '> informs you that a string enclosed in single quotes was opened on a previous line and has not yet been closed.
5. Which SQL statement selects the active database you want to work with in the MySQL CLI?
A. OPEN database_name; B. USE database_name; C. SELECT database_name; D. SWITCH database_name; Answer: B Explanation: The USE database_name; statement sets the default active database for subsequent SQL operations.
Setting Up MySQL Workbench
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Installing MySQL Server | Setting Up MySQL Workbench |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.