fix(mpc-system): 修复 PostgreSQL 认证配置问题

问题原因:
- 部署脚本使用 POSTGRES_PASSWORD 环境变量
- Go 代码使用 MPC_DATABASE_PASSWORD 环境变量 (Viper 前缀)
- 变量名不匹配导致数据库认证失败

修复内容:
- configure_postgres() 改用 MPC_DATABASE_* 环境变量
- 添加 pg_hba.conf 配置,将 peer/scram-sha-256 改为 md5 认证
- 添加 GRANT ALL ON SCHEMA public 权限
- 使用 127.0.0.1 而非 localhost 进行连接
- 新增 reconfigure 命令用于修复现有安装

服务器修复步骤:
  sudo ./scripts/deploy.sh reconfigure
  sudo ./scripts/deploy.sh restart

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Developer 2025-12-02 00:13:24 -08:00
parent 70e6391691
commit 0604255ba8
1 changed files with 59 additions and 16 deletions

View File

@ -145,23 +145,42 @@ setup_directories() {
configure_postgres() {
log_info "Configuring PostgreSQL..."
# Load environment variables
# Load environment variables - use MPC_ prefix variables (same as Go code uses)
source "$CONFIG_DIR/mpc.env" 2>/dev/null || true
local DB_USER="${POSTGRES_USER:-mpc_user}"
local DB_PASS="${POSTGRES_PASSWORD:-mpc_secret_password}"
local DB_NAME="mpc_system"
local DB_USER="${MPC_DATABASE_USER:-mpc_user}"
local DB_PASS="${MPC_DATABASE_PASSWORD:-your_secure_postgres_password_here}"
local DB_NAME="${MPC_DATABASE_DBNAME:-mpc_system}"
# Configure pg_hba.conf to allow password authentication for local connections
local PG_HBA="/etc/postgresql/*/main/pg_hba.conf"
for hba_file in $PG_HBA; do
if [ -f "$hba_file" ]; then
# Backup original
cp "$hba_file" "${hba_file}.bak" 2>/dev/null || true
# Change 'peer' to 'md5' for local connections to allow password auth
sed -i 's/local all all peer/local all all md5/' "$hba_file"
sed -i 's/host all all 127.0.0.1\/32 scram-sha-256/host all all 127.0.0.1\/32 md5/' "$hba_file"
sed -i 's/host all all ::1\/128 scram-sha-256/host all all ::1\/128 md5/' "$hba_file"
log_info "Updated pg_hba.conf at $hba_file"
fi
done
# Reload PostgreSQL to apply pg_hba.conf changes
systemctl reload postgresql 2>/dev/null || systemctl restart postgresql
# Create database and user
sudo -u postgres psql -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASS';" 2>/dev/null || true
sudo -u postgres psql -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASS';" 2>/dev/null || \
sudo -u postgres psql -c "ALTER USER $DB_USER WITH PASSWORD '$DB_PASS';"
sudo -u postgres psql -c "CREATE DATABASE $DB_NAME OWNER $DB_USER;" 2>/dev/null || true
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;" 2>/dev/null || true
sudo -u postgres psql -d "$DB_NAME" -c "GRANT ALL ON SCHEMA public TO $DB_USER;" 2>/dev/null || true
# Run migrations
log_info "Running database migrations..."
PGPASSWORD="$DB_PASS" psql -h localhost -U "$DB_USER" -d "$DB_NAME" -f "$MPC_HOME/migrations/001_init_schema.up.sql" 2>/dev/null || log_warn "Migration may have already been applied"
PGPASSWORD="$DB_PASS" psql -h 127.0.0.1 -U "$DB_USER" -d "$DB_NAME" -f "$MPC_HOME/migrations/001_init_schema.up.sql" 2>/dev/null || log_warn "Migration may have already been applied"
log_info "PostgreSQL configured"
log_info "PostgreSQL configured with user '$DB_USER' and database '$DB_NAME'"
}
configure_redis() {
@ -507,6 +526,22 @@ build() {
log_info "Start services with: $0 start"
}
# ============================================
# Reconfigure Command (fix existing installation)
# ============================================
reconfigure() {
check_root
log_info "Reconfiguring MPC-System infrastructure..."
configure_postgres
configure_redis
configure_rabbitmq
log_info "Reconfiguration complete!"
log_info "Restart services with: $0 restart"
}
# ============================================
# Uninstall Command
# ============================================
@ -550,6 +585,9 @@ case "${1:-}" in
build)
build
;;
reconfigure)
reconfigure
;;
start)
start_services
;;
@ -571,17 +609,18 @@ case "${1:-}" in
*)
echo "MPC-System Deployment Script"
echo ""
echo "Usage: $0 {install|build|start|stop|restart|status|logs|uninstall}"
echo "Usage: $0 {install|build|reconfigure|start|stop|restart|status|logs|uninstall}"
echo ""
echo "Commands:"
echo " install - Install system dependencies (Go, PostgreSQL, Redis, RabbitMQ)"
echo " build - Build services and configure infrastructure"
echo " start - Start all MPC services"
echo " stop - Stop all MPC services"
echo " restart - Restart all MPC services"
echo " status - Show service status"
echo " logs - View service logs (use: $0 logs [service-name])"
echo " uninstall - Remove MPC services"
echo " install - Install system dependencies (Go, PostgreSQL, Redis, RabbitMQ)"
echo " build - Build services and configure infrastructure"
echo " reconfigure - Reconfigure PostgreSQL/Redis/RabbitMQ (fix auth issues)"
echo " start - Start all MPC services"
echo " stop - Stop all MPC services"
echo " restart - Restart all MPC services"
echo " status - Show service status"
echo " logs - View service logs (use: $0 logs [service-name])"
echo " uninstall - Remove MPC services"
echo ""
echo "Example:"
echo " $0 install # First time setup"
@ -590,6 +629,10 @@ case "${1:-}" in
echo " $0 start # Start services"
echo " $0 status # Check status"
echo ""
echo "Troubleshooting:"
echo " $0 reconfigure # Fix database authentication issues"
echo " $0 restart # Then restart services"
echo ""
exit 1
;;
esac