AkJeond's picture
refactor(database): DB μŠ€ν‚€λ§ˆ μˆ˜μ • 및 μ„±λŠ₯ μ΅œμ ν™” λ¬Έμ„œ μΆ”κ°€
7aae924

πŸš€ SmartEyeSsen Backend μ„€μ • κ°€μ΄λ“œ

νŒ€μ›μ„ μœ„ν•œ 개발 ν™˜κ²½ μ„€μ • κ°€μ΄λ“œμž…λ‹ˆλ‹€.


πŸ“‹ λͺ©μ°¨

  1. μ‹œμŠ€ν…œ μš”κ΅¬μ‚¬ν•­
  2. MySQL μ„€μΉ˜ 및 μ„€μ •
  3. ν”„λ‘œμ νŠΈ 클둠 및 μ„€μ •
  4. λ°μ΄ν„°λ² μ΄μŠ€ 생성
  5. Python ν™˜κ²½ μ„€μ •
  6. μ„œλ²„ μ‹€ν–‰
  7. νŠΈλŸ¬λΈ”μŠˆνŒ…

1. μ‹œμŠ€ν…œ μš”κ΅¬μ‚¬ν•­

ν•„μˆ˜ ν”„λ‘œκ·Έλž¨

  • Python 3.9 이상
  • MySQL 8.0 이상
  • Git

μš΄μ˜μ²΄μ œλ³„ 지원

  • βœ… Windows 10/11
  • βœ… Windows + WSL2 (Ubuntu 20.04/22.04)
  • βœ… macOS
  • βœ… Linux (Ubuntu/Debian)

2. MySQL μ„€μΉ˜ 및 μ„€μ •

MySQL μ„œλΉ„μŠ€ μ‹œμž‘

# PowerShell (κ΄€λ¦¬μž κΆŒν•œ)
Start-Service MySQL80

MySQL 접속 ν…ŒμŠ€νŠΈ

mysql -u root -p
# λΉ„λ°€λ²ˆν˜Έ μž…λ ₯ ν›„ μ ‘μ†λ˜λ©΄ 성곡

2-2. Windows + WSL2 (Ubuntu)

WSL2 μ„€μΉ˜ (처음 μ‚¬μš©ν•˜λŠ” 경우)

# PowerShell (κ΄€λ¦¬μž κΆŒν•œ)
wsl --install -d Ubuntu-22.04

μž¬λΆ€νŒ… ν›„ Ubuntu μ‚¬μš©μžλͺ…/λΉ„λ°€λ²ˆν˜Έ μ„€μ •

MySQL μ„€μΉ˜ (WSL λ‚΄λΆ€)

# WSL Ubuntu 터미널
sudo apt update
sudo apt install mysql-server -y

# MySQL μ„œλΉ„μŠ€ μ‹œμž‘
sudo service mysql start

# Root λΉ„λ°€λ²ˆν˜Έ μ„€μ •
sudo mysql
-- MySQL μ½˜μ†”μ—μ„œ
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';
FLUSH PRIVILEGES;
EXIT;

MySQL 접속 ν…ŒμŠ€νŠΈ

mysql -u root -p
# λΉ„λ°€λ²ˆν˜Έ μž…λ ₯ ν›„ μ ‘μ†λ˜λ©΄ 성곡

3. ν”„λ‘œμ νŠΈ 클둠 및 μ„€μ •

3-2. ν™˜κ²½ λ³€μˆ˜ μ„€μ •

# .env 파일 생성 (Backend 폴더 λ‚΄)
cd Backend
cp .env.example .env

.env 파일 μˆ˜μ •

ν…μŠ€νŠΈ μ—λ””ν„°λ‘œ .env νŒŒμΌμ„ μ—΄κ³  λ‹€μŒ ν•­λͺ©μ„ μˆ˜μ •:

# λ°μ΄ν„°λ² μ΄μŠ€ μ„€μ •
DB_HOST=localhost
DB_PORT=3306
DB_USER=root
DB_PASSWORD=your_actual_password  # ⚠️ 여기에 μ‹€μ œ MySQL λΉ„λ°€λ²ˆν˜Έ μž…λ ₯
DB_NAME=smarteyessen_db

# OpenAI API
OPENAI_API_KEY=  # 이건 바꿔야돼!

# μ„œλ²„ μ„€μ •
API_HOST=0.0.0.0
API_PORT=8000

4. λ°μ΄ν„°λ² μ΄μŠ€ 생성

4-1. μŠ€ν‚€λ§ˆ 적용 (λ°μ΄ν„°λ² μ΄μŠ€ μžλ™ 생성)

erd_schema.sql νŒŒμΌμ—λŠ” λ°μ΄ν„°λ² μ΄μŠ€ 생성뢀터 ν…Œμ΄λΈ” 생성, 초기 데이터 μ‚½μž…κΉŒμ§€ λͺ¨λ“  μž‘μ—…μ΄ ν¬ν•¨λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

# ν”„λ‘œμ νŠΈ 루트 λ””λ ‰ν† λ¦¬μ—μ„œ μ‹€ν–‰
# (SmartEye-FrontWeb 폴더)
mysql -u root -p < Project/DB/erd_schema.sql

4-4. ν…Œμ΄λΈ” 생성 확인

mysql -u root -p smarteyessen_db
-- 12개 ν…Œμ΄λΈ” 확인
SHOW TABLES;

-- μ˜ˆμƒ κ²°κ³Ό:
-- +------------------------------+
-- | Tables_in_smarteyessen_db    |
-- +------------------------------+
-- | ai_descriptions              |
-- | combined_results             |
-- | document_types               |
-- | formatting_rules             |
-- | layout_elements              |
-- | pages                        |
-- | projects                     |
-- | question_elements            |
-- | question_groups              |
-- | text_contents                |
-- | text_versions                |
-- | users                        |
-- +------------------------------+

-- 초기 데이터 확인
SELECT * FROM document_types;
-- 2개 행이 μ‘°νšŒλ˜μ–΄μ•Ό 함 (worksheet, document)

EXIT;


### **5-2. μ˜μ‘΄μ„± μ„€μΉ˜**

```bash
# Backend ν΄λ”λ‘œ 이동
cd Backend

# νŒ¨ν‚€μ§€ μ„€μΉ˜
pip install -r requirements.txt

6. μ„œλ²„ μ‹€ν–‰

6-1. μ„œλ²„ μ‹œμž‘

Windows (PowerShell) κ΄€ν•™ μ‹€ν–‰ 방법

# pytorch ν™˜κ²½ ν™œμ„±ν™”
conda activate pytorch

# Backend ν΄λ”λ‘œ 이동
cd Backend

# μ„œλ²„ μ‹€ν–‰
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

WSL/macOS/Linux μ’…μ˜ 싀행방법

Backend ν΄λ”λ‘œ 이동

cd Backend

μ„œλ²„ μ‹€ν–‰

uvicorn app.main:app --reload --host 0.0.0.0 --port 8000


### **6-2. μ„œλ²„ μ‹€ν–‰ 확인**

λ‹€μŒκ³Ό 같은 좜λ ₯이 λ‚˜νƒ€λ‚˜λ©΄ 성곡:

INFO: Will watch for changes in these directories: ['C:\git\Smart-Eye-OCR\SmartEye-FrontWeb\Backend'] INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit) INFO: Started reloader process [12345] using WatchFiles INFO: Started server process [67890] INFO: Waiting for application startup.

πŸš€ SmartEyeSsen Backend Starting...

βœ… Database connection successful! βœ… Database connection successful βœ… Database tables created successfully! βœ… Database tables initialized

βœ… SmartEyeSsen Backend Ready! πŸ“– API Docs: http://localhost:8000/docs

INFO: Application startup complete.


### **6-3. API λ¬Έμ„œ 접속**

λΈŒλΌμš°μ €μ—μ„œ λ‹€μŒ URL둜 접속:

- 🏠 **메인**: http://localhost:8000
- πŸ“– **API λ¬Έμ„œ (Swagger UI)**: http://localhost:8000/docs
- πŸ“š **API λ¬Έμ„œ (ReDoc)**: http://localhost:8000/redoc
- ❀️ **ν—¬μŠ€ 체크**: http://localhost:8000/health

**ν—¬μŠ€ 체크 응닡 μ˜ˆμ‹œ:**

```json
{
  "status": "healthy",
  "message": "SmartEyeSsen Backend is running",
  "database": "connected",
  "timestamp": "2025-01-22T15:30:00.123456"
}