RDS 指南
实例创建
# 创建 DB 子网组
aws rds create-db-subnet-group \
--db-subnet-group-name my-subnet-group \
--db-subnet-group-description "My RDS subnet group" \
--subnet-ids subnet-private1a subnet-private1b
# 创建 RDS 实例
aws rds create-db-instance \
--db-instance-identifier my-postgres \
--db-instance-class db.t3.medium \
--engine postgres \
--engine-version 16.1 \
--master-username admin \
--master-user-password MySecurePass123! \
--allocated-storage 100 \
--storage-type gp3 \
--multi-az \
--db-subnet-group-name my-subnet-group \
--backup-retention-period 7 \
--deletion-protection \
--no-publicly-accessible
参数组
# 创建自定义参数组
aws rds create-db-parameter-group \
--db-parameter-group-name my-pg16-params \
--db-parameter-group-family postgres16 \
--description "Custom PostgreSQL 16 parameters"
# 修改参数
aws rds modify-db-parameter-group \
--db-parameter-group-name my-pg16-params \
--parameters \
"ParameterName=max_connections,ParameterValue=200,ApplyMethod=pending-reboot" \
"ParameterName=log_min_duration_statement,ParameterValue=1000,ApplyMethod=immediate"
快照与恢复
# 创建手动快照
aws rds create-db-snapshot \
--db-instance-identifier my-postgres \
--db-snapshot-identifier my-postgres-snap-20240101
# 从快照恢复到新实例
aws rds restore-db-instance-from-db-snapshot \
--db-instance-identifier my-postgres-restored \
--db-snapshot-identifier my-postgres-snap-20240101
# 时间点恢复
aws rds restore-db-instance-to-point-in-time \
--source-db-instance-identifier my-postgres \
--target-db-instance-identifier my-postgres-pit \
--restore-time 2024-01-15T03:30:00Z
只读副本
# 创建只读副本
aws rds create-db-instance-read-replica \
--db-instance-identifier my-postgres-read-1 \
--source-db-instance-identifier my-postgres \
--db-instance-class db.t3.medium
# 提升只读副本为独立实例
aws rds promote-read-replica \
--db-instance-identifier my-postgres-read-1
# 注意:只读副本使用异步复制
# 多可用区备库使用同步复制(不承载读流量)
多可用区与故障转移
# 转换为多可用区
aws rds modify-db-instance \
--db-instance-identifier my-postgres \
--multi-az \
--apply-immediately
# 强制故障转移(测试用)
aws rds reboot-db-instance \
--db-instance-identifier my-postgres \
--force-failover
加密与安全
| 功能 | 说明 |
|---|---|
| 静态加密 | 创建时启用 --storage-encrypted,使用 KMS |
| 加密未加密的 DB | 快照 → 加密复制 → 恢复 |
| 传输加密 | 下载 RDS CA 证书,设置 sslmode=verify-full |
| IAM DB 身份验证 | 使用 generate-db-auth-token 生成 token |
| 删除保护 | --deletion-protection 防止误删 |