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