MySQL Client and Connection Cheatsheet

mysql client commands for connecting, switching databases, inspecting metadata, and working efficiently in interactive or scripted sessions.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

mysql client basics

Connect, select a database, inspect metadata, and run SQL from the MySQL command line client.

Connect to a local MySQL server

Open an interactive mysql session.

bashANYmysqlclientconnect
bash
mysql -u root -p

Prompts for the password and connects using the default local socket or TCP settings.

Connect to a remote host and port

Specify the host, port, user, and prompt for a password.

bashANYmysqlconnectremote
bash
mysql -h db.example.com -P 3306 -u app_user -p

Useful for connecting to staging, production, or containerized MySQL instances.

Connect directly to a database

Open mysql and select the target schema immediately.

bashANYmysqldatabaseconnect
bash
mysql -u app_user -p app_db

Equivalent to connecting first and then running `USE app_db;`.

Run one query and exit

Execute a SQL statement non-interactively.

bashANYmysqlautomationquery
bash
mysql -u app_user -p -e "SHOW DATABASES;"

Good for scripts, quick checks, and automation tasks.

List databases

Display all databases visible to the current account.

sqlANYshowdatabasesmetadata
sql
SHOW DATABASES;

Visibility depends on privileges.

Select a database

Set the active database for subsequent statements.

sqlANYusedatabasecontext
sql
USE app_db;

After `USE`, unqualified table names resolve inside that schema.

List tables in the current database

Display tables in the selected schema.

sqlANYshowtables
sql
SHOW TABLES;

A quick way to inspect schema contents from the CLI.

Describe a table

Inspect columns, types, nullability, keys, and defaults.

sqlANYdescribeschemacolumns
sql
DESCRIBE users;

`DESC users;` is a common shorthand.

Show the exact CREATE TABLE statement

Display table DDL including indexes and engine options.

sqlANYddlshow-createtable
sql
SHOW CREATE TABLE users\G

Using `\G` in the mysql client prints vertical output, which is easier to read for long DDL.

Display warnings from the last statement

Inspect truncation, coercion, and non-fatal issues.

sqlANYwarningsdiagnostics
sql
SHOW WARNINGS;

Very useful after bulk imports or `INSERT ... SELECT` statements.

script-friendly client patterns

Useful flags and metadata commands for shell scripts and automation.

Query without column headers

Print raw rows for scripts and pipelines.

bashANYmysqlscriptsheaders
bash
mysql -u app_user -p -N -e "SELECT id, email FROM users LIMIT 5;" app_db

`-N` removes column names from the output.

Use batch mode for parsable output

Reduce formatting for machine-friendly results.

bashANYmysqlbatchautomation
bash
mysql -u app_user -p -B -e "SELECT NOW();"

`-B` outputs tab-separated rows that are easier to parse in shell tools.

Log an interactive session to a file

Write statements and output to a transcript file.

sqlANYmysqlteelogging
sql
tee /tmp/mysql-session.log

Inside the mysql client, `tee` starts recording output; `notee` turns it off.

Show connection status in the mysql client

Display host, connection id, server version, and current database.

sqlANYmysqlstatussession
sql
status

Useful for verifying where you are connected before running risky statements.

Show active sessions

Inspect current client connections and statements.

sqlANYprocesslistsessionstroubleshooting
sql
SHOW PROCESSLIST;

Helpful when diagnosing blocked queries or long-running sessions.

Kill a running connection

Terminate a session by connection id.

sqlANYkillprocesslistadmin
sql
KILL 12345;

Use carefully in production; get the connection id from `SHOW PROCESSLIST`.

Recommended next

No recommendations yet.