xiaso-flnxi-website/one-click-database-setup.sh
Your Name b1eab32c75 初始提交: FLNXI电商素材系统官方网站
- Next.js 项目框架
- 完整的认证系统集成
- 数据库配置和部署脚本
- 产品价格体系和文档
- Docker 部署配置
2025-12-13 21:38:32 +08:00

121 lines
3.3 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 一键PostgreSQL数据库配置脚本
# 使用方法:bash one-click-database-setup.sh
set -e
echo "🚀 一键部署 PostgreSQL 数据库..."
echo ""
SERVER="root@45.249.208.118"
SERVER_PASSWORD="lxvhNYFZ4160"
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m'
# 检查 sshpass
if ! command -v sshpass &> /dev/null; then
echo -e "${YELLOW}正在安装 sshpass...${NC}"
if [[ "$OSTYPE" == "darwin"* ]]; then
brew install hudochenkov/sshpass/sshpass || {
echo "请先安装: brew install hudochenkov/sshpass/sshpass"
exit 1
}
fi
fi
echo -e "${BLUE}连接服务器并配置数据库...${NC}"
sshpass -p "$SERVER_PASSWORD" ssh -o StrictHostKeyChecking=no $SERVER << 'ENDSSH'
set -e
cd /opt/flnxi-website
echo "📝 创建 Docker Compose 配置..."
cat > docker-compose-db.yml << 'EOF'
version: '3.8'
services:
postgres:
image: postgres:15-alpine
container_name: flnxi-postgres
environment:
POSTGRES_DB: flnxi_db
POSTGRES_USER: flnxi_user
POSTGRES_PASSWORD: Flnxi@2025!SecurePassword
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- '127.0.0.1:5433:5432'
restart: unless-stopped
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U flnxi_user']
interval: 10s
timeout: 5s
retries: 5
volumes:
postgres_data:
EOF
echo "🐳 启动 PostgreSQL..."
docker-compose -f docker-compose-db.yml down 2>/dev/null || true
docker rm -f flnxi-postgres 2>/dev/null || true
docker-compose -f docker-compose-db.yml up -d
echo "⏳ 等待数据库启动..."
sleep 20
echo "📊 创建用户表..."
docker exec -i flnxi-postgres psql -U flnxi_user -d flnxi_db << 'EOFSQL'
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
email_verified BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
CREATE INDEX IF NOT EXISTS idx_users_created_at ON users(created_at);
SELECT 'Users table created!' as status;
EOFSQL
echo "⚙️ 配置环境变量..."
cat >> .env.local << 'EOFENV'
# PostgreSQL 数据库配置
DATABASE_URL="postgresql://flnxi_user:Flnxi@2025!SecurePassword@localhost:5433/flnxi_db?schema=public"
EOFENV
echo "✅ 验证数据库..."
docker exec flnxi-postgres psql -U flnxi_user -d flnxi_db -c "SELECT current_database(), current_user;"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🎉 PostgreSQL 配置完成!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "📊 数据库信息:"
echo " 容器名: flnxi-postgres"
echo " 数据库: flnxi_db"
echo " 用户名: flnxi_user"
echo " 端口: 5433"
echo ""
ENDSSH
echo -e "${GREEN}✅ 部署完成!${NC}"
echo ""
echo -e "${YELLOW}📝 下一步:${NC}"
echo "1. 安装依赖: npm install @prisma/client bcrypt"
echo "2. 更新 API 代码使用数据库"
echo "3. 重新部署应用"
echo ""
echo -e "${YELLOW}🔍 查看数据库配置详情:${NC}"
echo "cat DATABASE_SETUP_GUIDE.md"
echo ""