SadServers
  • Scenarios
  • Labs
    All Labs Linux & Bash Web Servers Databases Data Processing Docker Kubernetes CI/CD Infrastructure as Code Tooling / Applications
  • Dashboard
  • Solutions
    For Individuals For Businesses
  • Ranking
  • Newsletter
  • Documentation
    FAQ Support Pro Accounts Pro+ Accounts Business Accounts Gift API CLI/TUI Privacy Troubleshooting Interviews
  • Blog
  • Pricing
  • Gift
    Gift Purchase Gift Redeem
  • About
Log In - Sign Up
  1. Labs
  2. MySQL
  3. Cheatsheet

Guide

Concepts and learning path

Troubleshooting

Failure modes and fixes

Cheatsheet

Commands to keep handy

MySQL cheatsheet

mysql client

CommandDescription
mysql -h host -u user -pConnect to server
mysql -u root -p -e "SELECT 1"Run single query
SHOW DATABASES;List databases
SHOW TABLES FROM db;List tables
DESCRIBE table_name;Column definitions
SHOW PROCESSLIST;Active sessions

Service and health

CommandDescription
systemctl status mysqlService status (MySQL)
systemctl status mariadbService status (MariaDB)
mysqladmin ping -h localhostCheck if server is up
ss -tlnp | grep 3306Confirm listener
tail -f /var/log/mysql/error.logFollow error log

Session and locks

SQLDescription
KILL QUERY id;Cancel running query
KILL id;Terminate connection
SHOW ENGINE INNODB STATUS\GInnoDB locks, deadlocks, buffer pool
SHOW VARIABLES LIKE 'max_connections';Connection limit
EXPLAIN SELECT ...;Query execution plan

Replication — primary / source

SQLDescription
SHOW MASTER STATUS;Binlog file and position (legacy)
SHOW BINARY LOGS;List binlog files
SHOW BINLOG EVENTS IN 'binlog.000003';Inspect binlog events
SELECT @@gtid_executed;Applied GTID set (if enabled)

Replication — replica

SHOW REPLICA STATUS\G -- Legacy: SHOW SLAVE STATUS\G -- Key fields: Replica_IO_Running, Replica_SQL_Running, -- Last_Error, Seconds_Behind_Source START REPLICA; -- or START SLAVE; STOP REPLICA; RESET REPLICA ALL; -- careful: removes relay logs and connection settings

Set up replication (GTID example)

-- On replica (after backup restore): CHANGE REPLICATION SOURCE TO SOURCE_HOST='primary.example.com', SOURCE_USER='repl', SOURCE_PASSWORD='secret', SOURCE_AUTO_POSITION=1; START REPLICA;

Database size

SELECT table_schema, ROUND(SUM(data_length + index_length)/1024/1024, 1) AS mb FROM information_schema.tables GROUP BY table_schema ORDER BY mb DESC;

Backup and restore

CommandDescription
mysqldump --single-transaction mydb > mydb.sqlConsistent InnoDB logical backup
mysqldump --all-databases > all.sqlFull server logical backup
mysql mydb < mydb.sqlRestore from SQL dump
mariabackup --backup --target-dir=/backup/fullPhysical hot backup (MariaDB)
mysqlbinlog binlog.000012 > replay.sqlExtract binlog for PITR

Pro tips

  • MariaDB uses the same client — service name may be mariadb not mysql
  • SHOW REPLICA STATUS\G — always check Last_Error when replication stops
  • Prefer binlog_format=ROW for fewer replication surprises
  • Never write to a read replica — set read_only=1 and super_read_only=1
  • Replicas are not backups — use mysqldump --single-transaction or mariabackup; test restores

Practice scenarios

Hands-on MySQL scenarios on live Linux VMs: mysql

SadServersSadServers

Real-world Linux and DevOps scenarios for hands-on learning and technical assessment.

Uptime Robot ratio (30 days)
Product
  • Scenarios
  • For Individuals
  • For Businesses
  • Pricing
Resources
  • FAQ
  • Blog
  • Newsletter
Company
  • About Us
  • Support
  • Privacy Policy
  • Terms of Service
  • Contact
Connect With Us
info@sadservers.com

Made in Canada 🇨🇦
Updated: 2026-06-13 16:06 UTC – 2d2950a