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 to a local MySQL server
mysql -u root -p

# Open an interactive mysql session.

Connect to a remote host and port
mysql -h db.example.com -P 3306 -u app_user -p

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

Connect directly to a database
mysql -u app_user -p app_db

# Open mysql and select the target schema immediately.

Run one query and exit
mysql -u app_user -p -e "SHOW DATABASES;"

# Execute a SQL statement non-interactively.

List databases
SHOW DATABASES;

# Display all databases visible to the current account.

Select a database
USE app_db;

# Set the active database for subsequent statements.

List tables in the current database
SHOW TABLES;

# Display tables in the selected schema.

Describe a table
DESCRIBE users;

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

Show the exact CREATE TABLE statement
SHOW CREATE TABLE users\G

# Display table DDL including indexes and engine options.

Display warnings from the last statement
SHOW WARNINGS;

# Inspect truncation, coercion, and non-fatal issues.

## script-friendly client patterns
Query without column headers
mysql -u app_user -p -N -e "SELECT id, email FROM users LIMIT 5;" app_db

# Print raw rows for scripts and pipelines.

Use batch mode for parsable output
mysql -u app_user -p -B -e "SELECT NOW();"

# Reduce formatting for machine-friendly results.

Log an interactive session to a file
tee /tmp/mysql-session.log

# Write statements and output to a transcript file.

Show connection status in the mysql client
status

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

Show active sessions
SHOW PROCESSLIST;

# Inspect current client connections and statements.

Kill a running connection
KILL 12345;

# Terminate a session by connection id.

Recommended next

No recommendations yet.