e-scooter-rental-system/docs/MongoDB安装指南.md

150 lines
3.0 KiB
Markdown
Raw Permalink 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.

# MongoDB 安装指南
## 为什么需要 MongoDB
两轮电动车租赁管理系统使用 MongoDB 作为数据库,用于存储车辆、订单、客户等数据。
## 安装方法
### 方法一:下载安装包 (推荐)
#### 1. 下载 MongoDB
- 访问: https://www.mongodb.com/try/download/community
- 选择 **Windows** 版本
- 下载 **MongoDB Community Server**
#### 2. 安装 MongoDB
- 运行下载的安装程序
- 选择 **Complete** 安装类型
- 勾选 **Install MongoDB as a Service**
- 点击 **Install** 开始安装
#### 3. 配置数据目录
- 默认路径: `C:\data\db`
- 如果目录不存在,手动创建:
```bash
mkdir C:\data\db
```
#### 4. 验证安装
打开命令提示符,输入:
```bash
mongod --version
```
如果显示版本信息,说明安装成功。
### 方法二:使用 Docker (推荐)
#### 1. 安装 Docker Desktop
- 访问: https://www.docker.com/products/docker-desktop/
- 下载并安装 Docker Desktop for Windows
- 启动 Docker Desktop
#### 2. 拉取 MongoDB 镜像
```bash
docker pull mongo
```
#### 3. 启动 MongoDB 容器
```bash
docker run -d -p 27017:27017 --name mongodb mongo
```
#### 4. 验证运行
```bash
docker ps
```
应该看到 mongodb 容器正在运行。
### 方法三:使用 MongoDB Compass (图形界面)
#### 1. 下载 MongoDB Compass
- 访问: https://www.mongodb.com/try/download/compass
- 下载 Windows 版本
#### 2. 安装 MongoDB Compass
- 运行安装程序
- 按照提示完成安装
#### 3. 连接到本地 MongoDB
- 打开 MongoDB Compass
- 连接字符串: `mongodb://localhost:27017`
- 点击 **Connect**
## 启动 MongoDB
### Windows 服务方式
如果安装时选择了 "Install MongoDB as a Service"MongoDB 会自动启动。
### 手动启动
```bash
mongod --dbpath C:\data\db
```
### Docker 方式
```bash
docker start mongodb
```
## 验证 MongoDB 运行
### 方法一:使用命令行
```bash
mongosh
```
如果进入 MongoDB Shell说明运行正常。
### 方法二:使用 MongoDB Compass
- 打开 MongoDB Compass
- 连接到 `mongodb://localhost:27017`
- 如果连接成功,说明运行正常
## 常见问题
### 1. 端口被占用
如果 27017 端口被占用,可以修改端口:
```bash
mongod --port 27018 --dbpath C:\data\db
```
同时需要修改项目中的 `.env` 文件:
```
MONGODB_URI=mongodb://localhost:27018/e-scooter-rental
```
### 2. 数据目录权限问题
如果无法创建数据目录,尝试以管理员身份运行命令提示符。
### 3. Docker 连接失败
确保 Docker Desktop 正在运行,并且没有其他容器占用 27017 端口。
## 启动项目
### 1. 启动 MongoDB
确保 MongoDB 正在运行。
### 2. 启动后端服务
```bash
cd E:\code\e-scooter-rental-system
npm run dev
```
### 3. 生成测试数据
```bash
npm run seed
```
### 4. 测试 API
```bash
curl http://localhost:3000/health
```
## 下一步
安装完成后,按照以下顺序操作:
1. ✅ 安装 MongoDB
2. ✅ 启动 MongoDB
3. ✅ 启动后端服务
4. ✅ 生成测试数据
5. ✅ 测试 API 接口
6. ⏳ 开发前端页面