Kamixitong/MYSQL_CONFIG_GUIDE.md
2025-11-16 19:06:49 +08:00

96 lines
1.7 KiB
Markdown
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.

# MySQL 数据库配置与初始化指南
## 目录
1. [环境要求](#环境要求)
2. [安装依赖](#安装依赖)
3. [配置数据库连接](#配置数据库连接)
4. [创建数据库](#创建数据库)
5. [初始化数据库](#初始化数据库)
6. [启动应用](#启动应用)
## 环境要求
- MySQL 5.7 或更高版本
- Python 3.8 或更高版本
- PyMySQL 驱动
## 安装依赖
```bash
pip install PyMySQL
```
## 配置数据库连接
复制 [.env.example](.env.example) 文件为 .env 并修改数据库配置:
```bash
cp .env.example .env
```
在 .env 文件中修改数据库连接配置:
```env
DATABASE_URL=mysql+pymysql://用户名:密码@主机:端口/数据库名
```
示例:
```env
DATABASE_URL=mysql+pymysql://root:password@localhost:3306/kamaxitong
```
## 创建数据库
在 MySQL 中手动创建数据库:
```sql
CREATE DATABASE kamaxitong CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
```
或者使用提供的脚本自动创建:
```bash
python setup_mysql.py
```
## 初始化数据库
有两种方式初始化数据库:
### 方式一:使用专用初始化脚本(推荐)
```bash
python init_db_mysql.py
```
该脚本将:
1. 从 .env 文件读取数据库配置
2. 清理现有表结构
3. 创建所有数据表
4. 插入初始数据(管理员账号、示例产品等)
### 方式二:使用通用配置脚本
```bash
python setup_mysql.py
```
## 启动应用
```bash
python run.py
```
```bash
python start.py
```
访问地址http://localhost:5000
默认管理员账号:
- 超级管理员: admin / admin123
- 普通管理员: test_admin / test123
⚠️ 请在生产环境中修改默认密码!