MongoDB常见使用
mongosh
[root@localhost bin]# mongosh
Current Mongosh Log ID: 66a8573ae9521fb76d482f8a
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.12
Using MongoDB: 6.0.16
Using Mongosh: 2.2.12
For mongosh info see: Welcome to MongoDB Shell (mongosh) - MongoDB Shell
------
The server generated these startup warnings when booting
2024-07-30T09:58:25.490+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2024-07-30T09:58:25.490+08:00: You are running this process as the root user, which is not recommended
2024-07-30T09:58:25.490+08:00: You are running on a NUMA machine. We suggest launching mongod like this to avoid performance problems: numactl --interleave=a ll mongod [other options]
2024-07-30T09:58:25.490+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' in this binary version
2024-07-30T09:58:25.490+08:00: Soft rlimits for open file descriptors too low
------
show dbs
test> show dbs;
admin 40.00 KiB
config 12.00 KiB
local 72.00 KiB
test> use test
already on db test
show roles
test> show roles
[
{
role: 'enableSharding',
db: 'test',
isBuiltin: true,
roles: [],
inheritedRoles: []
},
{
role: 'dbOwner',
db: 'test',
isBuiltin: true,
roles: [],
inheritedRoles: []
},
{
role: 'dbAdmin',
db: 'test',
isBuiltin: true,
roles: [],
inheritedRoles: []
},
{
role: 'readWrite',
db: 'test',
isBuiltin: true,
roles: [],
inheritedRoles: []
},
{
role: 'userAdmin',
db: 'test',
isBuiltin: true,
roles: [],
inheritedRoles: []
},
{
role: 'read',
db: 'test',
isBuiltin: true,
roles: [],
inheritedRoles: []
}
]
create user
db.createUser({user:"test",pwd:"test",roles:['dbOwner']})
test> db.createUser({user:"test",pwd:"test",roles:['dbOwner']})
{ ok: 1 }
show users
未创建前:
test> show users
[]
创建后:
test> show users
[
{
_id: 'test.test',
userId: UUID('3deb24e1-6ff2-4917-8457-cb51f396e8e1'),
user: 'test',
db: 'test',
roles: [ { role: 'dbOwner', db: 'test' } ],
mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}
]
del user
db.dropUser("用户名")