Skip to content

21.5 MongoDB

MongoDB 是采用 BSON 格式存储的 NoSQL 文档型数据库,具备动态模式与水平扩展能力。本节说明 FreeBSD 上 MongoDB 8.0 的 pkg 安装、启动配置及基础操作流程。

21.5.1 安装

使用 pkg 安装 MongoDB:

sh
# pkg install mongodb80

或者使用 ports 安装:

sh
# cd /usr/ports/databases/mongodb80/
# make install clean

查看 MongoDB 安装后的说明信息:

sh
# pkg info -D mongodb80

21.5.2 服务

安装完成后,启动 MongoDB 服务:

sh
# service mongod enable   # 设置 MongoDB 服务开机自启
# service mongod start    # 启动 MongoDB 服务

21.5.3 mongosh(MongoDB 官方 Shell CLI)

MongoDB 6.0 及更高版本不再包含传统的 mongo 命令行工具,取而代之的是官方的 mongosh。在 FreeBSD Ports 源中已经提供 mongosh,无需额外操作。

21.5.3.1 安装 mongosh

使用 pkg 安装:

sh
# pkg install mongosh

或者使用 ports 安装:

sh
# cd /usr/ports/databases/mongosh/
# make install clean

21.5.3.2 测试连接 MongoDB

使用 mongosh 连接到本地 MongoDB 服务:

sh
# mongosh mongodb://127.0.0.1:27017
Current Mongosh Log ID:	67bca91ccf5bbd1a232d5665
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.5
Using MongoDB:		8.0.4
Using Mongosh:		2.2.5

For mongosh info see: https://docs.mongodb.com/mongodb-shell/


To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

------
   The server generated these startup warnings when booting
   2025-02-25T01:08:25.311+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------

test>

21.5.4 配置文件

相关项目文件结构:

sh
/
└── usr/
    ├── local/
   └── etc/
       ├── mongodb.conf        # MongoDB 配置文件
       └── mongodb.conf.sample # MongoDB 配置文件示例
    └── ports/
        └── databases/
            ├── mongodb80/  # MongoDB 8.0 Port
            └── mongosh/    # MongoDB Shell Port

如需进一步配置 MongoDB,可以修改其配置文件。

MongoDB 8.0 的配置文件位于 /usr/local/etc/mongodb.conf,配置模板文件位于 /usr/local/etc/mongodb.conf.sample

21.5.5 创建用户和密码

MongoDB 默认不启用访问控制,任何人均可连接和操作数据库。建议创建用户并设置密码。

使用 mongosh 连接本地 MongoDB 服务:

sql
# mongosh mongodb://127.0.0.1:27017
Current Mongosh Log ID:	67bcb0e5337ba1c7d62d5665
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.5
Using MongoDB:		8.0.4
Using Mongosh:		2.2.5
mongosh 2.4.0 is available for download: https://www.mongodb.com/try/download/shell

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

test> show dbs   // 列出 MongoDB 实例中的所有数据库
admin   180.00 KiB
config   92.00 KiB
local    72.00 KiB
test> use admin   // 切换到 admin 数据库,也可以创建并使用其他数据库
switched to db admin
admin> db.createUser({   // 创建新用户
...   user: 'admin',     // 自定义用户名
...   pwd: '<强密码>',           // 自定义密码,应使用强密码
...   roles:[{
...     role: 'root',      // 授予超级管理员角色,可管理所有数据库
...     db: 'admin'        // 指定用户所在数据库
...   }]
... })
{ ok: 1 }
admin> quit   // 退出 MongoDB shell

可将以下文本直接复制至命令行:

sql
db.createUser({
   user: 'admin',
   pwd: '<你的强密码>',
   roles:[{
     role: 'root',
     db: 'admin'
   }]
 })

创建用户后,启用密码验证:

编辑 /usr/local/etc/mongodb.conf 文件:

去掉 #security: 前的注释符号 #,然后在下一行加入 authorization: enabled,如下所示:

ini
security:
  authorization: enabled   # 启用 MongoDB 用户认证和访问控制

重启 MongoDB 服务使配置生效:

sh
# service mongod restart

21.5.6 登录方式

启用访问控制后,需使用用户名和密码登录,方式如下:

21.5.6.1 登录方式 ①

使用 mongosh 连接到本地 MongoDB 服务:

sql
# mongosh mongodb://127.0.0.1:27017
Current Mongosh Log ID:	67bcb1bafa0080a2132d5665
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.5
Using MongoDB:		8.0.4
Using Mongosh:		2.2.5
mongosh 2.4.0 is available for download: https://www.mongodb.com/try/download/shell

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

test> use admin // 必须先切换到数据库 admin
switched to db admin
admin> db.auth('admin', '<你的密码>') // 使用用户名和密码登录 MongoDB 数据库
{ ok: 1 }
admin>

21.5.6.2 登录方式 ②

使用用户名 admin 和密码 z 登录 MongoDB:

sql
# mongosh -u admin -p z
Current Mongosh Log ID:	67bcb200cbc3b601fb2d5665
Connecting to:		mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.5
Using MongoDB:		8.0.4
Using Mongosh:		2.2.5
mongosh 2.4.0 is available for download: https://www.mongodb.com/try/download/shell

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

test>

21.5.7 参考文献