33.5 MongoDB
最后更新于
# pkg install mongosh# cd /usr/ports/databases/mongosh/
# make install clean# 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>/
└── usr/
├── local/
│ └── etc/
│ ├── mongodb.conf # MongoDB 配置文件
│ └── mongodb.conf.sample # MongoDB 配置文件示例
└── ports/
└── databases/
├── mongodb80/ # MongoDB 8.0 Port
└── mongosh/ # MongoDB Shell Port# 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 shelldb.createUser({
user: 'admin',
pwd: '<你的强密码>',
roles:[{
role: 'root',
db: 'admin'
}]
})security:
authorization: enabled # 启用 MongoDB 用户认证和访问控制# service mongod restart# 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># 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>