Relational Database Systems Concepts, Design, SQL, PostgreSQL, MySQL, and Applications

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

PlatformRoute
WindowsMySQL Installer → Server only; wizard sets root password, port 3306, service
Debian/Ubuntusudo apt install mysql-server then sudo mysql_secure_installation
RHEL/Fedorasudo dnf install mysql-server; sudo systemctl start mysqld
macOSbrew install mysql
Dockerdocker 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 / escapeEffect
STATUS;Session summary
SHOW DATABASES; USE university;List / select
SHOW TABLES; SHOW COLUMNS FROM t;Tables / describe
SHOW CREATE TABLE t; \GFull 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\GLocks, deadlocks, history
SHOW WARNINGS; SHOW ERRORS;After a suspicious success
SOURCE file.sql or \. file.sqlRun a script
\cCancel half-typed statement
quitExit

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])

SettingTypical valueNotes
bind-address127.0.0.1 → 0.0.0.0Network reach (with accounts + firewall)
port3306
innodb_buffer_pool_size50–70% RAM (dedicated)The performance setting
max_connections151 defaultPrefer pooling
innodb_flush_log_at_trx_commit1 (default)Durability vs write load
event_schedulerONFor Chapter 16/22 events
slow_query_log / long_query_timeON / 0.5The tuning tripwire
general_logOFF (surgical)Expensive
local_infileOFF unless neededClient-side load security
secure_file_privpath or NULLServer file area for INFILE/OUTFILE
transaction_isolationREPEATABLE READMySQL's default (Chapter 17/18)
character-set-serverutf8mb4Collation 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

SymptomMeaning / first check
ERROR 2003 (HY000): Can't connectService/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 databaseDatabase 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 timeoutBlocked transaction — SHOW ENGINE INNODB STATUS, data_locks
ERROR 1213: Deadlock foundRetry the transaction (40001) — Chapter 18/23
ERROR 1146: Table doesn't existWrong database selected (USE) / case sensitivity of the OS