mysql

MySQL 数据库管理与运维

chaterm/terminal-skills96 installsApache-2.0Synced Aug 26

Works with

Claude CodeCursorCodex CLIGitHub CopilotGemini CLI
---
name: mysql
description: MySQL 数据库管理与运维
license: Apache-2.0
---

# MySQL 数据库管理

## 概述
MySQL/MariaDB 数据库的日常管理、备份恢复、性能调优等运维技能。

## 连接管理

```bash
# 本地连接
mysql -u root -p

# 远程连接
mysql -h hostname -P 3306 -u user -p database

# 执行 SQL 文件
mysql -u user -p database < script.sql

# 执行单条命令
mysql -u user -p -e "SHOW DATABASES;"
```

## 用户与权限

```sql
-- 查看用户
SELECT user, host FROM mysql.user;

-- 创建用户
CREATE USER 'username'@'%' IDENTIFIED BY 'password';

-- 授权
GRANT ALL PRIVILEGES ON database.* TO 'username'@'%';
GRANT SELECT, INSERT ON database.table TO 'username'@'%';

-- 刷新权限
FLUSH PRIVILEGES;

-- 查看权限
SHOW GRANTS FOR 'username'@'%';
```

## 数据库操作

```sql
-- 数据库管理
SHOW DATABASES;
CREATE DATABASE dbname CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
DROP DATABASE dbname;
USE dbname;

-- 表管理
SHOW TABLES;
DESCRIBE tablename;
SHOW CREATE TABLE tablename;
```

## 备份与恢复

### mysqldump 备份
```bash
# 备份单个数据库
mysqldump -u root -p database > backup.sql

# 备份所有数据库
mysqldump -u root -p --all-databases > all_backup.sql

# 备份表结构
mysqldump -u root -p --no-data database > schema.sql

# 压缩备份
mysqldump -u root -p database | gzip > backup.sql.gz
```

### 恢复
```bash
# 恢复数据库
mysql -u root -p database < backup.sql

# 从压缩文件恢复
gunzip < backup.sql.gz | mysql -u root -p database
```

## 性能监控

```sql
-- 查看进程
SHOW PROCESSLIST;
SHOW FULL PROCESSLIST;

-- 查看状态
SHOW STATUS;
SHOW GLOBAL STATUS LIKE 'Threads%';
SHOW GLOBAL STATUS LIKE 'Connections';

-- 查看变量
SHOW VARIABLES LIKE 'max_connections';
SHOW VARIABLES LIKE '%buffer%';

-- 慢查询
SHOW VARIABLES LIKE 'slow_query%';
SHOW GLOBAL STATUS LIKE 'Slow_queries';
```

## 常见场景

### 场景 1:排查慢查询
```sql
-- 开启慢查询日志
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 1;

-- 查看慢查询日志位置
SHOW VARIABLES LIKE 'slow_query_log_file';

-- 分析执行计划
EXPLAIN SELECT * FROM table WHERE condition;
EXPLAIN ANALYZE SELECT * FROM table WHERE condition;
```

### 场景 2:锁问题排查
```sql
-- 查看锁等待
SHOW ENGINE INNODB STATUS\G

-- 查看当前锁
SELECT * FROM information_schema.INNODB_LOCKS;
SELECT * FROM information_schema.INNODB_LOCK_WAITS;

-- 查看事务
SELECT * FROM information_schema.INNODB_TRX;
```

### 场景 3:主从复制状态
```sql
-- 主库状态
SHOW MASTER STATUS;

-- 从库状态
SHOW SLAVE STATUS\G

-- 关键指标
-- Slave_IO_Running: Yes
-- Slave_SQL_Running: Yes
-- Seconds_Behind_Master: 0
```

## 故障排查

| 问题 | 排查方法 |
|------|----------|
| 连接数过多 | `SHOW PROCESSLIST`, 检查 max_connections |
| 查询慢 | `EXPLAIN`, 检查索引 |
| 锁等待 | `SHOW ENGINE INNODB STATUS` |
| 复制延迟 | `SHOW SLAVE STATUS`, 检查网络和负载 |
| 磁盘满 | 检查 binlog, 清理旧日志 |

More Database skills

supabase-postgres-best-practices

supabase/agent-skills

Postgres best practices maintained by Supabase, for Postgres running anywhere. Load this skill BEFORE writing or changing anything that lives in a Postgres database: creating or altering tables and columns (including choosing column types), schema design, migrations and declarative schema files, RLS policies and the tests that verify them, indexes, triggers, database functions, queues and scheduled jobs (pg_cron, pgmq), vector/semantic search (pgvector), and restoring dumps (pg_restore) or importing data. Also load it when diagnosing slow queries, high CPU, timeouts, EXPLAIN plans, connection exhaustion, locking, bloat, or rows visible to the wrong user or tenant. This is not just a performance guide — schema, migration, security, and SQL authoring tasks need these rules too, even for a one-column change or a single query.

372.5k

prisma-database-setup

prisma/skills

Guides for configuring Prisma with different database providers (PostgreSQL, MySQL, SQLite, MongoDB, etc.). Use when setting up a new project, changing databases, or troubleshooting connection issues. Triggers on "configure postgres", "connect to mysql", "setup mongodb", "sqlite setup".

241.5k

prisma-postgres

prisma/skills

Prisma Postgres setup and operations guidance across Console, create-db CLI, Management API, and Management API SDK. Use when creating Prisma Postgres databases, working in Prisma Console, provisioning with create-db/create-pg/create-postgres, or integrating programmatic provisioning with service tokens or OAuth.

235.0k

← All Database skills

Check your AI visibility

One URL in, a 0–100 score and the exact fixes out.

RUN THE CHECK

Browse all the tools

15 tools across six categories
13 of them never send your data anywhere

Free · No signup · No trial clock

SEE THE DIRECTORY