Appendices
Appendix C. MySQL Installation and Command Reference
The MySQL side of Chapter 14, as a reference card: install routes, server control, the mysql client, configuration, backup and restore, maintenance, and monitoring — MySQL 8.0 and later.
Installation
| Platform | Route |
|---|---|
| Windows | MySQL Installer → Server only; wizard sets root password, port 3306, service |
| Debian/Ubuntu | sudo apt install mysql-server then sudo mysql_secure_installation |
| RHEL/Fedora | sudo dnf install mysql-server; sudo systemctl start mysqld |
| macOS | brew install mysql |
| Docker | docker run -e MYSQL_ROOT_PASSWORD=... -p 3306:3306 mysql:8 |
Server control and connection
sudo systemctl start | stop | restart | status mysql # unit mysql or mysqld
mysql -h host -P 3306 -u user -p university # -p prompts (never inline)
mysql -u root -p -e "SELECT VERSION();" # one-shot execution
mysqladmin -u root -p status # server status
Credential defaults: [client] sections of ~/.my.cnf (host, user, password) — the credential-file pattern.
mysql client essentials
| Statement / escape | Effect |
|---|---|
STATUS; | Session summary |
SHOW DATABASES; USE university; | List / select |
SHOW TABLES; SHOW COLUMNS FROM t; | Tables / describe |
SHOW CREATE TABLE t; \G | Full DDL / vertical output |
SHOW INDEX FROM t; | Index inventory |
SHOW GRANTS [FOR user@host]; | Privileges |
SHOW PROCESSLIST; | Who is running what |
SHOW ENGINE INNODB STATUS\G | Locks, deadlocks, history |
SHOW WARNINGS; SHOW ERRORS; | After a suspicious success |
SOURCE file.sql or \. file.sql | Run a script |
\c | Cancel half-typed statement |
quit | Exit |
Users, roles, privileges
CREATE USER 'name'@'host' IDENTIFIED BY '...';
ALTER USER 'name'@'host' IDENTIFIED BY '...'
[PASSWORD EXPIRE [INTERVAL n DAY | NEVER | DEFAULT]]
[ACCOUNT LOCK | UNLOCK];
DROP USER 'name'@'host';
CREATE ROLE 'faculty';
GRANT 'faculty' TO 'name'@'host';
SET DEFAULT ROLE 'faculty' TO 'name'@'host'; -- roles activate at login
GRANT SELECT, INSERT ON university.enrollment TO 'name'@'host';
GRANT SELECT ON university.* TO 'report'@'%';
REVOKE INSERT ON university.enrollment FROM 'name'@'host';
SHOW GRANTS FOR 'name'@'host';
Host patterns: localhost, exact IP, % (any), 10.0.0.% (subnet). Auth plugins: caching_sha2_password (default), mysql_native_password (legacy driver bridge).
Configuration (my.cnf / my.ini, section [mysqld])
| Setting | Typical value | Notes |
|---|---|---|
bind-address | 127.0.0.1 → 0.0.0.0 | Network reach (with accounts + firewall) |
port | 3306 | |
innodb_buffer_pool_size | 50–70% RAM (dedicated) | The performance setting |
max_connections | 151 default | Prefer pooling |
innodb_flush_log_at_trx_commit | 1 (default) | Durability vs write load |
event_scheduler | ON | For Chapter 16/22 events |
slow_query_log / long_query_time | ON / 0.5 | The tuning tripwire |
general_log | OFF (surgical) | Expensive |
local_infile | OFF unless needed | Client-side load security |
secure_file_priv | path or NULL | Server file area for INFILE/OUTFILE |
transaction_isolation | REPEATABLE READ | MySQL's default (Chapter 17/18) |
character-set-server | utf8mb4 | Collation utf8mb4_0900_ai_ci |
SET PERSIST name = value; writes to mysqld-auto.cnf without editing files; SET GLOBAL is session-of-server only.
Backup and restore
mysqldump --single-transaction --routines --triggers
--databases university > university.sql
mysqldump --single-transaction university table1 table2 > partial.sql
mysql -u root -p university_new < university.sql # restore via client
mysqlpump --parallel-schemas ... university > u.sql # parallel (older tool)
mysqlsh -- util dumpInstance('/path') | util loadDump(...) # MySQL Shell, parallel
Physical: the clone plugin (CLONE INSTANCE FROM 'user'@'host':3306 IDENTIFIED BY '...') and the XtraBackup family. Binary log: mysqlbinlog --start-datetime --stop-datetime binlog.0000* | mysql (PITR's replay tape; GTIDs with --source-id/auto-position).
Maintenance and monitoring
ANALYZE TABLE t; -- statistics
OPTIMIZE TABLE t; -- rebuild (InnoDB: recreates clustered index)
CHECK TABLE t;
SHOW GLOBAL STATUS LIKE 'Threads_connected';
SHOW GLOBAL STATUS LIKE 'Innodb_buffer_pool_read%'; -- hit ratio
SELECT * FROM performance_schema.data_locks; -- current locks
SELECT * FROM sys.statements_with_runtimes_in_95th_percentile; -- slow queries
SELECT * FROM information_schema.INNODB_METRICS ...; -- engine internals
SHOW REPLICA STATUS; -- replication (old spelling: SHOW SLAVE STATUS)
SELECT event_name, count_star FROM performance_schema.events_statements_summary_by_digest
ORDER BY count_star DESC LIMIT 10;
Common troubleshooting
| Symptom | Meaning / first check |
|---|---|
ERROR 2003 (HY000): Can't connect | Service/port/firewall rung |
Access denied for user 'x'@'h' | No such user@host pair, wrong password, or no grant — check all three |
ERROR 1049: Unknown database | Database name / not created / wrong instance |
ERROR 1226: User has exceeded 'max_questions' | Resource limits on the account |
Authentication plugin 'caching_sha2_password' | Old client/driver — update or mysql_native_password |
ERROR 1205: Lock wait timeout | Blocked transaction — SHOW ENGINE INNODB STATUS, data_locks |
ERROR 1213: Deadlock found | Retry the transaction (40001) — Chapter 18/23 |
ERROR 1146: Table doesn't exist | Wrong database selected (USE) / case sensitivity of the OS |