第 17.11 节 MongoDB 80
安装
# 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 6.0 and up do not include the 'mongo' CLI shell anymore. You can
use the MongoDB Shell (https://github.com/mongodb-js/mongosh).
# pkg install npm
$ npm install mongosh
$ 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>
参考文献
宝塔面板 使用MongoDB一些教程 PHP7,本文主要基于此
mongodb添加管理员和用户,各种角色划分参见此处
最后更新于