feat: Improve database setup process

- Update database setup to forcefully terminate connections before dropping the existing database and user, preventing errors during the process.
- Clarify comments regarding the database password and data overwriting in the setup script.
This commit is contained in:
2026-02-26 21:09:16 +08:00
parent 89948f76b7
commit 900cc5fa09

View File

@@ -99,24 +99,22 @@ install_nodejs() {
fi fi
} }
# 配置数据库(固定密码 12345678安装时覆盖旧数据 # 配置数据库(固定密码 12345678强制删除旧库与用户并重建
setup_database() { setup_database() {
echo -e "${YELLOW}[4/7] 配置数据库...${NC}" echo -e "${YELLOW}[4/7] 配置数据库...${NC}"
systemctl enable postgresql systemctl enable postgresql
systemctl start postgresql systemctl start postgresql
# 固定数据库密码,安装时覆盖旧库与用户 # 强制断开对 funmc 库的所有连接,再删除库和用户(避免 role already exists / 无法删除库)
DB_PASSWORD="12345678" sudo -u postgres psql -d postgres -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'funmc' AND pid <> pg_backend_pid();" 2>/dev/null || true
sudo -u postgres psql -d postgres -c "DROP DATABASE IF EXISTS funmc;" 2>/dev/null || true
# 先删除旧数据库和用户(覆盖旧数据) sudo -u postgres psql -d postgres -c "DROP USER IF EXISTS funmc;" 2>/dev/null || true
sudo -u postgres psql -c "DROP DATABASE IF EXISTS funmc;" 2>/dev/null || true
sudo -u postgres psql -c "DROP USER IF EXISTS funmc;" 2>/dev/null || true
# 创建用户和数据库 # 创建用户和数据库
sudo -u postgres psql -c "CREATE USER funmc WITH PASSWORD '$DB_PASSWORD';" sudo -u postgres psql -d postgres -c "CREATE USER funmc WITH PASSWORD '12345678';"
sudo -u postgres psql -c "CREATE DATABASE funmc OWNER funmc;" sudo -u postgres psql -d postgres -c "CREATE DATABASE funmc OWNER funmc;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE funmc TO funmc;" sudo -u postgres psql -d postgres -c "GRANT ALL PRIVILEGES ON DATABASE funmc TO funmc;"
# 配置 pg_hba.conf # 配置 pg_hba.conf
PG_HBA=$(sudo -u postgres psql -t -c "SHOW hba_file;" | xargs) PG_HBA=$(sudo -u postgres psql -t -c "SHOW hba_file;" | xargs)
@@ -126,8 +124,7 @@ setup_database() {
systemctl reload postgresql systemctl reload postgresql
fi fi
echo -e "${GREEN}✓ 数据库配置完成(密码已设为 12345678已覆盖旧数据${NC}" echo -e "${GREEN}✓ 数据库配置完成(密码 12345678强制覆盖旧数据)${NC}"
echo "$DB_PASSWORD" > /tmp/funmc_db_password
} }
# 下载并编译 FunMC # 下载并编译 FunMC
@@ -167,7 +164,7 @@ build_funmc() {
configure_services() { configure_services() {
echo -e "${YELLOW}[6/7] 配置服务...${NC}" echo -e "${YELLOW}[6/7] 配置服务...${NC}"
DB_PASSWORD=$(cat /tmp/funmc_db_password) DB_PASSWORD="12345678"
JWT_SECRET=$(openssl rand -base64 48 | tr -dc 'a-zA-Z0-9' | head -c 48) JWT_SECRET=$(openssl rand -base64 48 | tr -dc 'a-zA-Z0-9' | head -c 48)
ADMIN_PASSWORD=$(openssl rand -base64 16 | tr -dc 'a-zA-Z0-9' | head -c 12) ADMIN_PASSWORD=$(openssl rand -base64 16 | tr -dc 'a-zA-Z0-9' | head -c 12)