> ## Content Index
> Fetch the complete content index at: https://yinguobing.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# MongoDB基本操作
- URL: https://yinguobing.com/blog/mongodb-basics/
- Published: 2022-01-15T11:51:52.000Z
- Updated: 2022-05-29T02:32:11.000Z
- Description: 从安装到导入数据
- Author: 尹国冰

这篇文章记录了MongoDB从安装到创建第一个数据集合的过程，备忘。

MongoDB版本：4.4

### 安装

相对简单，按照官方给出的指导一步一步来即可。

[Install MongoDB![](https://docs.mongodb.com/assets/favicon.ico)![](https://docs.mongodb.com/assets/meta_generic.png)](https://docs.mongodb.com/v4.4/installation/?ref=yinguobing.com)

### 允许远程访问

修改配置文件：

```bash
sudo vim /etc/mongod.conf
```

设定允许监听的IP地址：

```yaml
# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0
```

重启服务：

```shell
sudo systemctl restart mongod
```

### 创建用户

以测试为目的时，创建一个高权限用户：

```mongo
use admin
db.createUser({ user: "mongoadmin" , pwd: "mongoadmin", roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase"]})
```

⚠️

这样做很危险，请三思。

### 远程访问

最简单的做法是使用MongoDB compass。

[MongoDB CompassExplore and interact with your data using Compass, the GUI for MongoDB. Query, modify, delete, and more — all from one interface.![](https://www.mongodb.com/assets/images/global/favicon.ico)MongoDB![](https://webimages.mongodb.com/_com_assets/cms/kuzt9r42or1fxvlq2-Meta_Generic.png)](https://www.mongodb.com/products/compass?ref=yinguobing.com)

安装完成后填写地址、端口、用户名与密码即可连接。

### 恢复数据

连接后新建一个db，然后建立一个collection，之后便可以导入数据。

完成。