19.5 MongoDB 8.0

安装

# pkg ins mongodb80

或者:

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

查看安装后说明:

root@ykla:~ # pkg info -D mongodb80
mongodb80-8.0.4_3:
On install:
MongoDB on Raspberry Pi can work but is unsupported upstream.
Please read https://jira.mongodb.org/browse/SERVER-71772 and enable option
ARMV80A if you run this on a non-LSE ARM cpu like Raspberry Pi 4.
# MongoDB 在 Raspberry Pi 上可以运行,但官方不支持。
# 请阅读 https://jira.mongodb.org/browse/SERVER-71772 并在 Raspberry Pi 4 等非 LSE ARM CPU 上启用 ARMV80A 选项。

MongoDB 6.0 and up do not include the 'mongo' CLI shell anymore. You can
use the MongoDB Shell (https://github.com/mongodb-js/mongosh).
# MongoDB 6.0 及更高版本不再包括 'mongo' CLI shell。
# 你可以使用 MongoDB Shell(https://github.com/mongodb-js/mongosh)。

# pkg install npm
$ npm install mongosh
$ npx mongosh mongodb://127.0.0.1:27017/
# 安装 npm 并使用以下命令安装 mongosh:
# $ npm install mongosh
# 然后使用 npx 启动 mongosh 连接到本地 MongoDB 实例:
# $ npx mongosh mongodb://127.0.0.1:27017/

服务

root@ykla:~ # service mongod enable
mongod_enable:  -> YES
root@ykla:~ # service mongod start
Starting mongod.

mongosh(MongoDB 官方 Shell CLI)

Ports 源里就有 mongosh,不需要额外操作。

安装 mongosh

# pkg install mongosh

或者:

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

测试链接 MongoDB

root@ykla:~ # 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>

配置文件

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

创建用户和密码

root@ykla:~ # 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
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: 'z',  // 自定义密码,此处我设置密码为 z
...   roles:[{
...     role: 'root',   // 使用超级管理员,可以管理所有数据库,拥有最高权限
...     db: 'admin'     // 指定数据库
...   }]
... })
{ ok: 1 }
admin> quit // 退出

你可以直接复制下面的文本到命令行中:

db.createUser({ 
   user: 'admin',    
   pwd: 'z', 
   roles:[{
     role: 'root',   
     db: 'admin'   
   }]
 })

然后开启密码验证:

编辑 /usr/local/etc/mongodb.conf

删掉 #security: 前面的 #,然后在下行加入 authorization: enabled,即:

security:
  authorization: enabled

重启服务:

# service mongod restart

登录方式

登录方式 ①

root@ykla:~ # 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', 'z') // 登录数据库
{ ok: 1 }
admin>

登录方式 ②

root@ykla:~ # 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>

参考文献

最后更新于