Database cheatsheet
MySQL 8.4 Cheatsheet
mysql client, backup and maintenance commands for MySQL 8.4.
Connecting
- Check the client version — Print the version of the mysql client.
- Connect to a server — Open the mysql client as a user, prompting for the password.
- Connect to a specific host and database — Open the client on a given host, port and database.
- Check the server version — Run inside the client to see the server version.
- Run a SQL file — Execute the statements in a file against a database.
- Quit the client — Leave the mysql prompt and return to the shell.
Inspecting the database
- List databases — Show the databases you have access to.
- Switch database — Make a database the default for the following statements.
- List tables — Show the tables in the current database.
- Describe a table — Show a table's columns, types and keys.
- Show the CREATE TABLE statement — Print the statement that would recreate a table, including indexes and constraints.
- List a table's indexes — Show each index and the columns it covers.
- Check database sizes — Add up data and index sizes for each database, in MB.
Databases and users
- Create a database — Create a database that uses the utf8mb4 character set.
- Drop a database — Delete a database and everything in it.
- Create a user — Create an account that can connect from the local machine. Use a real password.
- Grant privileges on a database — Let a user read and write the tables in one database.
- Show a user's privileges — List the grants an account has.
Backup and restore
- Back up a database — Write a SQL dump of one database. --single-transaction gives a consistent snapshot of InnoDB tables without locking them.
- Back up every database — Write one SQL dump containing all databases.
- Restore a dump — Replay a SQL dump into a database that already exists.
Performance and maintenance
- See a query's plan — Show how MySQL plans to run a query.
- Run a query and see real timings — Execute the query and show the plan with actual row counts and times.
- Create an index — Add an index to speed up lookups on a column.
- Refresh index statistics — Update the key distribution statistics the optimizer uses.
- See running queries — List the connections and the statements they are running.
- Check a server setting — Show the value of a system variable, such as the connection limit.
- Count open connections — Show how many client connections are open right now.
Related cheatsheets
- DuckDB 2 cheatsheet — Query CSV, Parquet and DataFrames with SQL from Python or the duckdb command line, without running a server.
- Elasticsearch 9 cheatsheet — curl requests for indexes, documents, searches, bulk loads and cluster health. Security is on by default, so the examples authenticate.
- MongoDB 8 cheatsheet — mongosh shell commands for documents, queries, aggregation, indexes, backups and administration.
- PostgreSQL 18 cheatsheet — psql, backup and maintenance commands for PostgreSQL 18.
- Redis 8 cheatsheet — redis-cli and core commands for keys, data structures and server administration.
- SQLite 3 cheatsheet — sqlite3 shell dot-commands, pragmas, import and export, and the SQL you use most.