feat: 补充数据库脚本和配置
This commit is contained in:
101
Document/infra/postgres_redis.md
Normal file
101
Document/infra/postgres_redis.md
Normal file
@@ -0,0 +1,101 @@
|
||||
# PostgreSQL 与 Redis 接入手册
|
||||
|
||||
> 本文档补齐 `Document/10_TODO.md` 中“Postgres/Redis 接入文档与 IaC/脚本”的要求,统一描述连接信息、账号权限、运维流程,以及可复用的部署脚本位置。
|
||||
|
||||
## 1. 运行环境总览
|
||||
|
||||
| 组件 | 地址/端口 | 主要数据库/实例 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| PostgreSQL | `120.53.222.17:5432` | `takeout_app_db`、`takeout_identity_db`、`takeout_dictionary_db`、`takeout_hangfire_db` | 线上实例,所有业务上下文共用。 |
|
||||
| Redis | `49.232.6.45:6379` | 单节点 | 业务缓存/登录限流/刷新令牌存储。 |
|
||||
|
||||
> 注意:所有业务账号都只具备既有库的读写权限,无 `CREATEDB`。若需新库,需使用平台管理员账号(`postgres`)或联系 DBA。
|
||||
|
||||
## 2. 账号与库映射
|
||||
|
||||
| 数据库 | 角色 | 密码 | 用途 |
|
||||
| --- | --- | --- | --- |
|
||||
| `takeout_app_db` | `app_user` | `AppUser112233` | 业务域 (`TakeoutAppDbContext`) |
|
||||
| `takeout_identity_db` | `identity_user` | `IdentityUser112233` | 身份域 (`IdentityDbContext`) |
|
||||
| `takeout_dictionary_db` | `dictionary_user` | `DictionaryUser112233` | 字典域 (`DictionaryDbContext`) |
|
||||
| `takeout_hangfire_db` | `hangfire_user` | `HangFire112233` | 后台调度/Hangfire |
|
||||
|
||||
Redis 密码:`MsuMshk112233`,见 `appsettings.*.json -> Redis`。
|
||||
|
||||
## 3. 环境变量/配置注入
|
||||
|
||||
### PowerShell
|
||||
|
||||
```powershell
|
||||
$env:TAKEOUTSAAS_APPSETTINGS_DIR = "D:\HAZCode\TakeOut\src\Api\TakeoutSaaS.AdminApi"
|
||||
$env:TAKEOUTSAAS_APP_CONNECTION = "Host=120.53.222.17;Port=5432;Database=takeout_app_db;Username=app_user;Password=AppUser112233;Pooling=true"
|
||||
$env:TAKEOUTSAAS_IDENTITY_CONNECTION = "Host=120.53.222.17;Port=5432;Database=takeout_identity_db;Username=identity_user;Password=IdentityUser112233;Pooling=true"
|
||||
$env:TAKEOUTSAAS_DICTIONARY_CONNECTION = "Host=120.53.222.17;Port=5432;Database=takeout_dictionary_db;Username=dictionary_user;Password=DictionaryUser112233;Pooling=true"
|
||||
```
|
||||
|
||||
### Bash
|
||||
|
||||
```bash
|
||||
export TAKEOUTSAAS_APPSETTINGS_DIR=/home/user/TakeOut/src/Api/TakeoutSaaS.AdminApi
|
||||
export TAKEOUTSAAS_APP_CONNECTION="Host=120.53.222.17;Port=5432;Database=takeout_app_db;Username=app_user;Password=AppUser112233;Pooling=true"
|
||||
export TAKEOUTSAAS_IDENTITY_CONNECTION="Host=120.53.222.17;Port=5432;Database=takeout_identity_db;Username=identity_user;Password=IdentityUser112233;Pooling=true"
|
||||
export TAKEOUTSAAS_DICTIONARY_CONNECTION="Host=120.53.222.17;Port=5432;Database=takeout_dictionary_db;Username=dictionary_user;Password=DictionaryUser112233;Pooling=true"
|
||||
```
|
||||
|
||||
Redis 连接字符串直接写入 `appsettings.*.json` 即可,如:
|
||||
|
||||
```jsonc
|
||||
"Redis": "49.232.6.45:6379,password=MsuMshk112233,abortConnect=false"
|
||||
```
|
||||
|
||||
## 4. 运维指南
|
||||
|
||||
### PostgreSQL
|
||||
|
||||
1. **只读账号验证**
|
||||
```powershell
|
||||
psql "host=120.53.222.17 port=5432 dbname=takeout_app_db user=app_user password=AppUser112233"
|
||||
```
|
||||
2. **备份**
|
||||
```bash
|
||||
pg_dump -h 120.53.222.17 -p 5432 -U postgres -F c -d takeout_app_db -f backup/takeout_app_db_$(date +%Y%m%d).dump
|
||||
pg_dumpall -h 120.53.222.17 -p 5432 -U postgres > backup/all_$(date +%Y%m%d).sql
|
||||
```
|
||||
3. **恢复**
|
||||
```bash
|
||||
pg_restore -h 120.53.222.17 -p 5432 -U postgres -d takeout_app_db backup/takeout_app_db_xxx.dump
|
||||
psql -h 120.53.222.17 -p 5432 -U postgres -f backup/all_yyyymmdd.sql
|
||||
```
|
||||
4. **账号/权限策略**
|
||||
- `app_user` / `identity_user` / `dictionary_user` 拥有 `CONNECT`、`TEMP`、Schema `public` 的 CRUD 权限。
|
||||
- `hangfire_user` 仅能访问 `takeout_hangfire_db`,不可访问业务库。
|
||||
- 创建新表/列时,通过 EF Migration 自动添加 COMMENT。
|
||||
|
||||
### Redis
|
||||
|
||||
1. **连接验证**
|
||||
```bash
|
||||
redis-cli -h 49.232.6.45 -p 6379 -a MsuMshk112233 ping
|
||||
```
|
||||
2. **备份**
|
||||
```bash
|
||||
redis-cli -h 49.232.6.45 -p 6379 -a MsuMshk112233 save # 触发 RDB
|
||||
redis-cli -h 49.232.6.45 -p 6379 -a MsuMshk112233 bgsave # 后台
|
||||
```
|
||||
RDB/AOF 文件在服务器 `redis.conf` 定义的目录(默认 `/var/lib/redis`)。
|
||||
3. **常见运维项**
|
||||
- `CONFIG GET dir` / `CONFIG GET dbfilename` 可查看持久化路径。
|
||||
- `INFO memory` 监控内存;开启 `maxmemory` + `allkeys-lru` 保护。
|
||||
|
||||
## 5. IaC / 脚本
|
||||
|
||||
| 文件 | 说明 |
|
||||
| --- | --- |
|
||||
| `deploy/postgres/create_databases.sql` | 基于 `postgres` 管理员执行,创建四个业务库及角色、授予权限、补 COMMENT。 |
|
||||
| `deploy/postgres/bootstrap.ps1` | PowerShell 包装脚本,调用 `psql` 执行上面的 SQL(默认读取 `postgres` 管理员账号)。 |
|
||||
| `deploy/postgres/README.md` | 介绍如何在本地/测试环境执行 bootstrap 并校验连接。 |
|
||||
| `deploy/redis/docker-compose.yml` | 可复用的 Redis 部署(Redis 7 + AOF),便于本地或测试环境一键拉起。 |
|
||||
| `deploy/redis/redis.conf` | compose/裸机均可共用的配置(`requirepass`、持久化等已写好)。 |
|
||||
| `deploy/redis/README.md` | 说明如何使用 compose 或将 `redis.conf` 部署到现有实例。 |
|
||||
|
||||
> 线上目前为裸机安装(非容器),如需创建新环境/快速恢复,可直接运行上述脚本达到同样配置;即使在现有机器上,也可把 SQL/配置当作“最终规范”确保环境一致性。
|
||||
Reference in New Issue
Block a user