Skip to main content

💻 Lesson 3: MySQL CLI & phpMyAdmin

You've got MySQL installed. Now let's learn the two main tools you'll use to interact with it — the command-line interface for power and precision, and phpMyAdmin for visual exploration.

🎯 Learning Objectives

By the end of this lesson, you will be able to:

  • Log in and out of the MySQL command-line client
  • Navigate databases and tables using CLI commands
  • Understand the difference between SQL statements and MySQL-specific commands
  • Use phpMyAdmin to browse databases, run queries, and view table structures
  • Know when to use the CLI vs. phpMyAdmin

Estimated Time: 45 minutes

Prerequisites: MySQL installed and running (Lesson 2)

📑 In This Lesson

Two Tools, One Database

There are many ways to interact with a MySQL server, but the two most common for learners and developers are:

graph LR A["You"] -->|"type commands"| B["MySQL CLI
Terminal-based"] A -->|"click & type"| C["phpMyAdmin
Browser-based"] B -->|"SQL"| D["MySQL Server"] C -->|"SQL"| D

Both tools send the exact same SQL to the exact same MySQL server — they're just different interfaces. Think of it like driving a car with a manual transmission vs. an automatic: the engine underneath is identical, but the controls feel different.

Feature MySQL CLI phpMyAdmin
Interface Text-based terminal Visual web browser
Speed Fast once you know the commands More clicks, but discoverable
Scripting Excellent — run .sql files, pipe commands Limited — can import .sql files
Visual browsing Plain text tables Rich HTML tables, sortable columns
Learning curve Steeper — must memorize commands Gentler — menus guide you
Available everywhere Yes — any machine with MySQL installed Only if Apache/PHP are set up

✅ Our Approach

Throughout this course, we'll use the CLI as the primary tool because it teaches you SQL directly and works everywhere. We'll show phpMyAdmin alongside it so you can visualize what's happening. Professional developers use both — the CLI for scripting and automation, phpMyAdmin (or similar tools) for quick visual checks.

The MySQL Command-Line Client

Logging In

Open your terminal (Ubuntu/WSL, macOS Terminal, or Linux terminal) and connect to MySQL:


mysql -u root -p
                

Let's break down those flags:

Flag Meaning
mysql The MySQL command-line client program
-u root Connect as the user named root
-p Prompt for the password (don't type the password on the same line — it would be visible in your shell history)

After entering your password, you'll see the MySQL prompt:

Output:


Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.xx-0ubuntu0.24.04.1 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
                    

That mysql> prompt means you're inside the MySQL client. Everything you type here is sent to the MySQL server.

Logging Out

Any of these commands will disconnect you and return to your regular terminal:


EXIT;
QUIT;
\q
                

💡 Connecting to a Specific Database Directly

You can also specify a database when you log in:


mysql -u root -p my_database
                    

This is equivalent to logging in and then running USE my_database;.

Navigating with CLI Commands

MySQL has two kinds of commands you'll type at the prompt:

  1. SQL statements — standard SQL that works on any database (SELECT, INSERT, CREATE, etc.)
  2. MySQL-specific commands — shortcuts that only work in the MySQL CLI (SHOW, DESCRIBE, USE, etc.)

Let's walk through the essential navigation commands. Try each one as you read!

SHOW DATABASES — List All Databases


SHOW DATABASES;
                

Output:


+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)
                    

These are MySQL's built-in system databases. You'll add your own databases starting in Lesson 4.

USE — Select a Database

Before you can look at tables, you need to tell MySQL which database to work with:


USE mysql;
                

Output:


Database changed
                    

Now all commands will operate on the mysql system database (until you USE a different one).

SHOW TABLES — List Tables in the Current Database


SHOW TABLES;
                

Output:


+------------------------------------------------------+
| Tables_in_mysql                                      |
+------------------------------------------------------+
| columns_priv                                         |
| component                                            |
| db                                                   |
| default_roles                                        |
| ...                                                  |
| user                                                 |
+------------------------------------------------------+
                    

The mysql system database contains tables that store user accounts, permissions, and server settings. You'll rarely interact with these directly, but it's good to know they exist.

DESCRIBE — Show a Table's Structure

DESCRIBE (or its shorthand DESC) shows the columns, data types, and constraints for a table:


DESCRIBE user;
                

Output (abbreviated):


+-----------+-----------------------------------+------+-----+---------+-------+
| Field     | Type                              | Null | Key | Default | Extra |
+-----------+-----------------------------------+------+-----+---------+-------+
| Host      | char(255)                         | NO   | PRI |         |       |
| User      | char(32)                          | NO   | PRI |         |       |
| ...       | ...                               | ...  | ... | ...     | ...   |
+-----------+-----------------------------------+------+-----+---------+-------+
                    

This is incredibly useful — you'll use DESCRIBE constantly to remind yourself what columns a table has.

SELECT — Query Data

And of course, you can run any SQL query. Here's a quick peek at the MySQL user accounts:


SELECT User, Host FROM user;
                

Output:


+------------------+-----------+
| User             | Host      |
+------------------+-----------+
| debian-sys-maint | localhost |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| phpmyadmin       | localhost |
| root             | localhost |
+------------------+-----------+
6 rows in set (0.00 sec)
                    

Navigation Command Reference

Command What It Does Example
SHOW DATABASES; List all databases SHOW DATABASES;
USE Switch to a database USE my_database;
SHOW TABLES; List tables in current database SHOW TABLES;
DESCRIBE Show a table's column structure DESCRIBE customers;
SHOW CREATE TABLE Show the SQL used to create a table SHOW CREATE TABLE customers;
SELECT DATABASE(); Show currently selected database SELECT DATABASE();
STATUS; Show connection and server info STATUS;

CLI Tips and Tricks

Every Statement Ends with a Semicolon

SQL statements in the CLI must end with a semicolon (;). If you press Enter without one, MySQL thinks you're still typing and shows a continuation prompt:


mysql> SELECT
    -> VERSION()
    -> ;
                

The -> prompt means "I'm waiting for more input." Just type the semicolon and press Enter to execute.

⚠️ Stuck at the -> Prompt?

If you see -> and can't figure out what's missing, type ; and press Enter. If you see '> or ">, you have an unclosed quote — type the matching quote then a semicolon: '; or ";. To abandon the command entirely, type \c and press Enter.

Multi-Line Statements

You can (and should) write longer SQL across multiple lines for readability. MySQL doesn't execute until it sees the semicolon:


SELECT
    User,
    Host
FROM
    user
WHERE
    Host = 'localhost';
                

This is much easier to read than cramming everything on one line.

Command History

Press the Up Arrow key to cycle through previous commands — just like in your regular terminal. This saves a lot of retyping.

Clearing the Screen


\! clear
                

Or on Windows/WSL, you can use SYSTEM clear — the \! prefix runs a shell command from inside MySQL.

Getting Help


-- General help
HELP;

-- Help on a specific topic
HELP SELECT;
HELP CREATE TABLE;
                

Useful Shorthand Commands

Shorthand Full Command What It Does
\q EXIT; Quit the MySQL client
\c Cancel the current command
\s STATUS; Server status and connection info
\h HELP; Show help
\G Display results vertically (great for wide rows)
\! cmd SYSTEM cmd Run a shell command without leaving MySQL

Vertical Output with \G

When a table has many columns, the default horizontal output wraps and becomes hard to read. Use \G instead of ; to display each row vertically:


SELECT * FROM user WHERE User = 'root'\G
                

Output (abbreviated):


*************************** 1. row ***************************
                 Host: localhost
                 User: root
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: Y
          ...
1 row in set (0.00 sec)
                    

Each column is listed on its own line with its value — much easier to read for wide tables.

phpMyAdmin: A Guided Tour

Open your browser and go to http://localhost/phpmyadmin. Log in with your root credentials. Here's what you'll see:

📖 No phpMyAdmin? No Problem.

If you didn't install phpMyAdmin or you're using MySQL Workbench (macOS), you can still follow along — the concepts are the same, just the interface looks different. All SQL in this course works identically in any tool.

The Interface Layout

graph TB subgraph phpMyAdmin["phpMyAdmin Interface"] direction TB A["🔝 Top Bar
Server info, user, settings, logout"] B["📂 Left Sidebar
Database & table tree
(click to navigate)"] C["📄 Main Panel
Tables, query results,
forms, structure"] D["📑 Tab Bar
Structure | SQL | Search | Insert | Export | Import | ..."] end
  • Left Sidebar — shows all your databases as an expandable tree. Click a database name to select it, then expand it to see its tables.
  • Tab Bar — appears when you've selected a database or table. Key tabs:
    • Structure — view and modify columns, indexes, and keys
    • SQL — type and execute raw SQL queries
    • Search — build queries visually with dropdowns and filters
    • Insert — add new rows via a form
    • Export / Import — dump or load data (CSV, SQL, etc.)
  • Main Panel — displays results, forms, table data, and structure depending on which tab you're on.

Browsing a Database

  1. In the left sidebar, click on mysql (the system database)
  2. The main panel shows a list of all tables in that database
  3. Click on the user table
  4. You'll see the table's data displayed in a grid — this is the same data you'd see with SELECT * FROM user; in the CLI

Viewing Table Structure

  1. With the user table selected, click the Structure tab
  2. You'll see every column listed with its type, null setting, key status, default value, and extras
  3. This is the visual equivalent of DESCRIBE user;

Running a Query

  1. Click on mysql in the sidebar (to select the database)
  2. Click the SQL tab
  3. Type a query in the text box:
    
    SELECT User, Host FROM user;
                            
  4. Click Go (or press Ctrl+Enter)
  5. The results appear below in a formatted table

✅ Same SQL, Different View

The SQL you type in phpMyAdmin is identical to what you'd type in the CLI. phpMyAdmin just provides a nicer visual wrapper around the results. This is important: you're learning SQL, not phpMyAdmin-specific commands.

Running Queries in Both Tools

Let's run the same commands side by side to reinforce that both tools do the same thing.

Example 1: Check the MySQL Version

CLI phpMyAdmin
Type SELECT VERSION(); at the mysql> prompt Click SQL tab → type SELECT VERSION(); → click Go

Both return the same version number.

Example 2: List Databases

CLI phpMyAdmin
Type SHOW DATABASES; Look at the left sidebar — all databases are listed there. Or: SQL tab → SHOW DATABASES; → Go

Example 3: Select a Database and View Tables

CLI phpMyAdmin
USE mysql; then SHOW TABLES; Click mysql in the sidebar — the table list appears automatically

Example 4: Describe a Table

CLI phpMyAdmin
DESCRIBE user; Click user table → Structure tab

💡 Tip: Use Both Together

A great workflow is to write and test SQL in the CLI (it's faster and builds muscle memory), then switch to phpMyAdmin to visually inspect the results when you want to double-check your data. Many developers keep both open side by side.

CLI vs. phpMyAdmin: When to Use Which

Task Better Tool Why
Quick data check ("how many rows?") CLI Faster to type a query than click through menus
Browsing table data visually phpMyAdmin Sortable columns, pagination, inline editing
Writing and testing complex queries CLI (or SQL tab) Multi-line editing, command history
Designing table structure phpMyAdmin Visual form for columns, types, keys
Running a .sql script CLI SOURCE file.sql; — one command
Importing/exporting data Either phpMyAdmin has a nice UI; CLI has mysqldump
Remote server (SSH only) CLI phpMyAdmin may not be installed on production servers
Learning SQL syntax CLI Forces you to type SQL by hand — no GUI shortcuts

📖 The Professional Reality

In the real world, most backend developers and DBAs primarily use the command line (or a desktop client like MySQL Workbench, DataGrip, or DBeaver). phpMyAdmin is popular in shared hosting environments and for quick admin tasks. Learning the CLI first means you'll be comfortable in any environment.

Saving and Running SQL Files

As your queries get longer, typing them into the CLI every time becomes tedious. A better approach is to save your SQL in .sql files and run them from the command line.

Creating a SQL File

Open your text editor (VS Code recommended) and create a file called test.sql:


-- test.sql
-- A simple test script

SELECT VERSION();
SELECT NOW();
SHOW DATABASES;
                

Save it somewhere accessible (e.g., your home directory or a project folder).

Running a SQL File from the CLI

There are two ways:

Method 1: SOURCE command (from inside MySQL)


-- Log in first, then:
SOURCE /path/to/test.sql;
                

Method 2: Redirect input (from the terminal, before logging in)


mysql -u root -p < /path/to/test.sql
                

Method 2 is especially useful for automation — you can run SQL scripts from bash scripts, cron jobs, or deployment pipelines.

Running a SQL File in phpMyAdmin

  1. Click the Import tab
  2. Click Choose File and select your .sql file
  3. Click Go

✅ Pro Tip: Build a SQL Library

As you work through this course, save your SQL in organized files — one per lesson or one per topic. This gives you a personal reference library of tested SQL that you can reuse later. For example: lesson_04_create_tables.sql, lesson_06_crud.sql, etc.

Exercises

🏋️ Exercise 1: CLI Exploration

Log into MySQL from the CLI and perform the following tasks:

  1. Run STATUS; — what MySQL version and connection type do you see?
  2. Run SHOW DATABASES; — list all databases
  3. Run USE information_schema; — switch to the information_schema database
  4. Run SHOW TABLES; — how many tables does information_schema have?
  5. Run DESCRIBE TABLES; — look at the structure of the TABLES table
  6. Run SELECT DATABASE(); — confirm which database is selected
  7. Exit with \q
✅ What You Should See
  • STATUS; shows your MySQL version, connection ID, current database, and character set
  • SHOW DATABASES; lists 4 system databases (information_schema, mysql, performance_schema, sys)
  • information_schema typically has 70+ tables — they contain metadata about all databases and tables on the server
  • SELECT DATABASE(); returns information_schema

🏋️ Exercise 2: phpMyAdmin Scavenger Hunt

Open phpMyAdmin and find the answers to these questions (no CLI allowed!):

  1. How many databases are listed in the sidebar?
  2. Click on the mysql database — how many tables does it contain?
  3. Click on the user table, then the Structure tab — what data type is the User column?
  4. Click the SQL tab and run: SELECT User, Host FROM user; — how many users are listed?
  5. Find the phpMyAdmin version (hint: look at the bottom of any page or the home screen)
✅ Expected Answers
  • You should see 4–5 databases (the 4 system databases + possibly phpmyadmin's own database)
  • The mysql database has around 37 tables (varies by version)
  • The User column is char(32)
  • You'll see the users you created plus system accounts (typically 5–6 total)
  • The phpMyAdmin version appears at the bottom-right of every page

🏋️ Exercise 3: Create and Run a SQL File

  1. Create a file called my_first_script.sql in your text editor
  2. Add these queries:
    
    -- my_first_script.sql
    -- Practice running SQL from a file
    
    SELECT 'Hello from a SQL file!' AS message;
    SELECT VERSION() AS mysql_version;
    SELECT NOW() AS current_time;
    SELECT USER() AS logged_in_as;
    SHOW DATABASES;
                                
  3. Save the file
  4. Run it from inside MySQL: SOURCE /path/to/my_first_script.sql;
  5. Exit and run it from the terminal: mysql -u root -p < /path/to/my_first_script.sql
✅ What You Should See

Both methods produce the same output: a greeting message, your MySQL version, the current date/time, your login username, and a list of databases. The only difference is that the terminal redirect method exits immediately after running (no interactive prompt).

🎯 Quick Quiz

Question 1: What happens if you press Enter without a semicolon in the MySQL CLI?

Question 2: Which command shows the column structure of a table?

Question 3: How do you run a .sql file from inside the MySQL CLI?

Summary

🎉 Key Takeaways

  • The MySQL CLI and phpMyAdmin are two interfaces to the same database — use both
  • Log in with mysql -u root -p; log out with EXIT; or \q
  • Navigate with: SHOW DATABASES, USE, SHOW TABLES, DESCRIBE
  • Every SQL statement ends with a semicolon — no semicolon means MySQL keeps waiting
  • Use \G instead of ; for vertical output on wide tables
  • phpMyAdmin's key tabs: Structure, SQL, Search, Insert, Export/Import
  • Save SQL in .sql files and run with SOURCE or input redirection
  • CLI builds SQL muscle memory; phpMyAdmin gives visual feedback — use both strategically

🚀 What's Next?

You're comfortable navigating MySQL with both tools. In the next lesson, you'll start building your own databases and tables — creating the structures that will hold your data. This is where it gets really fun!

🎉 Module 1 Complete!

You've finished the Getting Started module. You understand what databases are, you've installed MySQL, and you can navigate it confidently with the CLI and phpMyAdmin. Time to start building!