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.
mysql -u <user> -pConnect as a user, prompting for the passwordmysql -h <host> -P 3306 -u <user> -p <db>Connect to a host and port, selecting a databasemysql -u root -pConnect as the root userSHOW DATABASES;List all databases on the serverUSE <db>;Switch to a databaseSHOW TABLES;List tables in the current databaseDESCRIBE <table>;Show a table's columns and typesDESC <table>;Shorthand for DESCRIBESHOW COLUMNS FROM <table>;List columns in a tableSHOW INDEX FROM <table>;List the indexes defined on a tableSHOW CREATE TABLE <table>;Show the CREATE TABLE statement for a tableSHOW STATUS;Show server status counters and variablesSHOW VARIABLES;Show server system variablesSHOW PROCESSLIST;List active client connections and queriesSELECT VERSION();Show the MySQL server versionSHOW GRANTS;Show privileges for the current userSHOW ENGINE INNODB STATUS;Show detailed InnoDB engine statusmysql -u <user> -p <db> < file.sqlImport a .sql file from the shellsource file.sqlRun a .sql file from inside the client\. file.sqlShorthand for sourcemysql -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) outputpager lessSend query output through a pager like less\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 statusWhat 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.