mysql Commands

mysql commands cheatsheet.

Connect, inspect databases and tables, run .sql files and use client meta-commands — the mysql CLI commands you reach for every day. Tap to copy.

Connect
mysql -u <user> -pConnect as a user, prompting for the password
mysql -h <host> -P 3306 -u <user> -p <db>Connect to a host and port, selecting a database
mysql -u root -pConnect as the root user
Databases & tables
SHOW DATABASES;List all databases on the server
USE <db>;Switch to a database
SHOW TABLES;List tables in the current database
DESCRIBE <table>;Show a table's columns and types
DESC <table>;Shorthand for DESCRIBE
SHOW COLUMNS FROM <table>;List columns in a table
SHOW INDEX FROM <table>;List the indexes defined on a table
SHOW CREATE TABLE <table>;Show the CREATE TABLE statement for a table
Server info
SHOW STATUS;Show server status counters and variables
SHOW VARIABLES;Show server system variables
SHOW PROCESSLIST;List active client connections and queries
SELECT VERSION();Show the MySQL server version
SHOW GRANTS;Show privileges for the current user
SHOW ENGINE INNODB STATUS;Show detailed InnoDB engine status
Import, run & output
mysql -u <user> -p <db> < file.sqlImport a .sql file from the shell
source file.sqlRun a .sql file from inside the client
\. file.sqlShorthand for source
mysql -u <user> -p -e "SQL" <db>Run one SQL statement and exit (non-interactive)
mysqldump -u <user> -p <db> > dump.sqlExport a database to a .sql file
\GEnd a statement with \G for vertical (row-per-line) output
pager lessSend query output through a pager like less
Client meta-commands
\hShow mysql client help
\qQuit the client (same as exit)
\cCancel the current input statement
\u <db>Switch database from inside the client (like USE)
statusShow connection and server status
\sShorthand for status

What the mysql client is

The mysql command-line client is the interactive terminal program that ships with MySQL (and MariaDB) for talking to a database server. This page is about driving that client — connecting, moving between databases, inspecting schema and using its meta-commands — rather than the SQL language itself. For SELECT, INSERT, JOIN and the rest, see the general SQL commands cheatsheet. You start a session from your shell with something like mysql -u <user> -p, which prompts for the password and drops you at the mysql> prompt where statements run until you type \q.

Finding your way around

Once connected, a handful of SHOW statements do most of the navigation. SHOW DATABASES; lists everything on the server, USE <db>; switches into one, and SHOW TABLES; lists its tables. To inspect a single table, DESCRIBE <table>; (or its shorthand DESC) prints its columns and types, while SHOW CREATE TABLE <table>; reveals the full definition including indexes and engine. SHOW INDEX FROM <table>; focuses on just the indexes — handy when tuning queries.

Running files and one-off queries

You rarely type long scripts by hand. From inside the client, source file.sql (or the shorthand \. file.sql) executes a file of statements. From the shell you can redirect a file straight in with mysql -u <user> -p <db> < file.sql, or run a single statement non-interactively with mysql -e "SQL" <db>. Going the other way, mysqldump exports a database to a portable .sql file you can commit, move or restore elsewhere.

Client tricks worth knowing

A few client-only touches make sessions far more pleasant. Ending a statement with \G instead of a semicolon prints each row vertically, which is invaluable for wide tables. SHOW PROCESSLIST; reveals what other connections are doing, status (or \s) summarises your connection, and \h lists every client command. These pair naturally with the Docker commands when your database runs in a container and the psql commands if you also work with PostgreSQL.

FAQ

How do I list databases in the mysql client?
Run SHOW DATABASES; at the mysql> prompt to list every database on the server, then USE <db>; to switch into one and SHOW TABLES; to see its tables.
How do I run a .sql file from the mysql client?
From inside the client use source file.sql (or its shorthand \. file.sql). From your shell, pipe it in with mysql -u user -p dbname < file.sql.

More cheatsheets