MySQL Command Line (CLI) Basics
In this lesson: Log in to MySQL CLI and use essential commands — show, use, describe, source, exit — with output screenshots-style.
What is the MySQL CLI
The MySQL command line (CLI) is a text window where you type SQL commands and instantly see results. It has no buttons or menus — just you and the mysql> prompt. Learning a few core commands here makes you comfortable anywhere MySQL runs.
;, press Enter, and see the result.Logging in
mysql -u root -p
-u root is the username and -p asks for the password. After the correct password, you reach the mysql> prompt, ready for commands.
SHOW commands
SHOW DATABASES;
SHOW DATABASES; lists every database on the server. After selecting one, SHOW TABLES; lists its tables. These help you see what exists.
USE and DESCRIBE
USE school; DESCRIBE students;
USE selects a database; DESCRIBE (or DESC) shows a table's columns, types and keys — handy when you forget a table's layout.
Running a query
SELECT name, marks FROM students WHERE marks > 40;
You type a query, end it with ;, and press Enter. MySQL runs it and prints the result as a neat table right in the terminal.
Handy helpers
\c— cancel the command you are typing.system clear(Linux/Mac) — clear the screen.SELECT VERSION();— show the MySQL version.exit;orquit;— leave the MySQL CLI.
These small commands make working in the CLI smoother.
Common mistakes
- Forgetting the semicolon. The command will not run until you add
;. - Wrong username or password. Check the
-uvalue and the password. - Running queries before
USE. Select a database first. - Typing exit without a semicolon. Most versions accept
exit, butexit;is safest.
Summary
- The MySQL CLI runs SQL commands directly at the
mysql>prompt. - Log in with
mysql -u root -p. SHOW DATABASES/SHOW TABLESlist what exists.USEselects a database;DESCRIBEshows a table's columns.- End commands with
;and quit withexit;.
Next, build your first data with MySQL first commands, or use a visual tool in MySQL Workbench.
Frequently Asked Questions
What is the MySQL command line (CLI)?
How do I log in to MySQL from the command line?
mysql -u root -p and press Enter, then type your password. -u is the username and -p prompts for the password. On success you get the mysql> prompt.How do I list all databases and tables?
SHOW DATABASES; to list databases, then USE dbname; to select one, and SHOW TABLES; to list its tables. DESCRIBE tablename; shows a table's columns.How do I exit the MySQL command line?
exit; or quit; and press Enter. Both close the MySQL session and return you to the normal terminal.What does DESCRIBE do in MySQL?
DESCRIBE tablename; (or DESC) shows the structure of a table — its columns, their data types, whether they allow null, and which is the key. It is useful to remember a table's layout.