Skip to content

切换库

在mongodb中和mysql中,都是使用use进行切换数据库,在mongodb中,如果在一个数据库不存在的情况下,去进入它,相当于创建这个数据库

mongodb最开始的创建用户

在最开始的时候,mongodb是默认不开启认证的,不需要密码就可以直接进入mongo,在配置文件开启后,需要自行创建用户,在创建用户之前,需要将切换到admin下

bash
use admin
db.createUser({
    user:"fsl", 
    pwd:"fsl123",
    roles:[{    
        role:"userAdminAnyDatabase", db:"admin"   # 这里的db是指的是在指定db下,有指定的权限
    }]
})

这里的角色有很多,创建一个这个用户后,可以通过这个用户,去赋予其他用户数据库的其他权限

第一种认证方式

在连接之后再进行验证

bash
[root@template ~]# mongosh 
Current Mongosh Log ID:	64170878f84b735d876594c4
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.0
Using MongoDB:		6.0.5
Using Mongosh:		1.8.0

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

test> use admin  #在认证之前,需要切换到对应的认证库下,不然无法进行认证
switched to db admin
admin> db.auth("fsl","fsl123")
{ ok: 1 }
admin> show dbs
admin   132.00 KiB
config   12.00 KiB
local    40.00 KiB
admin>

第二种认证方式

在连接之前进行验证

bash
[root@template ~]# mongosh -u fsl -p fsl123 --authenticationDatabase admin
Current Mongosh Log ID:	641709bbbeeeae4dbf644bcf
Connecting to:		mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&authSource=admin&appName=mongosh+1.8.0
Using MongoDB:		6.0.5
Using Mongosh:		1.8.0

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

test> show dbs
admin   132.00 KiB
config   12.00 KiB
local    40.00 KiB

连接到mongodb

需要注意的是,mongodb中的认证库指定的是,这个用户是在那个库下创建的用户,那么这个库就是他的认证库,而不是他拥有那个库的权限,比如说

test用户在admin库下创建的,但是它具有test库的权限,他在登录时,依然,需要指定的认证库为admin,而不是test

Connect to a Deployment — MongoDB Shell

网上好多的都是使用mongo进行对mongodb的管理,在5.0版本之后,都是推荐使用的是mongosh,但是需要自己安装,但是我使用docker安装的mongodb6.0.5版本,自带的mongosh为1.8.0

支持版本

支持4.2以上或者更高的版本

连接

bash
# 以下两种方式是等同的
mongosh
=
mongosh "mongodb://localhost:27017"

# 下面都是连接到mongodb0.example.com:28015
mongosh "mongodb://mongodb0.example.com:28015"
=
mongosh --host mongodb0.example.com --port 28015

用户相关操作

mongosh Methods — MongoDB Manual

查看用户

查看所有用户

bash
admin> db.getUsers()
{
  users: [
    {
      _id: 'admin.fsl',
      userId: new UUID("c901ec8e-0b5a-460f-b78b-d8b55fe3734a"),
      user: 'fsl',
      db: 'admin',
      roles: [ { role: 'userAdminAnyDatabase', db: 'admin' } ],
      mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
    },
    {
      _id: 'admin.test',
      userId: new UUID("0a6e412d-010b-4c73-9d04-ffa53fe63aa1"),
      user: 'test',
      db: 'admin',
      roles: [ { role: 'readWrite', db: 'test' } ],
      mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
    }
  ],
  ok: 1
}

查看当前库下的用户

查看具体用户

bash
test> db.getUser("user01")
{
  _id: 'test.user01',
  userId: new UUID("7bbcf429-6606-4201-bee2-a51915a367fc"),
  user: 'user01',
  db: 'test',
  roles: [ { role: 'readWrite', db: 'test' } ],
  mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}

创建

想要创建一个用户的话,需要注意验证库的问题,这里角色指定的db不是验证,在那个库下创建的就是验证库

bash
db.createUser({
    user:"test", 
    pwd:"test123",
    roles:[{    
        role:"readWrite", db:"test"    ## 指定这个角色权限所对应的库
    }]
})
# 或者
admin> use test
switched to db test
test> db.createUser({
...     user:"test", 
...     pwd:"test123",
...     roles:["readWrite","userAdmin"] ## 这里不声明db,默认指的db为创建用户的db,也就是说,在test下创建,那么db指的是test
... })
{ ok: 1 }
test> db.getUser("test")
{
  _id: 'test.test',
  userId: new UUID("2683b650-7438-4c9c-bdd5-688071cb4548"),
  user: 'test',
  db: 'test',
  roles: [
    { role: 'readWrite', db: 'test' },
    { role: 'userAdmin', db: 'test' }
  ],
  mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}

删除

删除一个用户时,需要到创建它的库下去删除,也就是去他的验证库删除

bash
test> use admin
admin> db.getUsers()
{
  users: [
    {
      _id: 'admin.fsl',
      userId: new UUID("c901ec8e-0b5a-460f-b78b-d8b55fe3734a"),
      user: 'fsl',
      db: 'admin',
      roles: [ { role: 'userAdminAnyDatabase', db: 'admin' } ],
      mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
    },
    {
      _id: 'admin.test',
      userId: new UUID("0a6e412d-010b-4c73-9d04-ffa53fe63aa1"),
      user: 'test',
      db: 'admin',
      roles: [ { role: 'readWrite', db: 'test' } ],
      mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
    }
  ],
  ok: 1
}
admin> db.dropUser("test")
{ ok: 1 }

给用户授权或者收回授权

给用户授权

bash
# 有一个用户的角色如下角色:
"roles" : [
    { "role" : "read",
      "db" : "test"
    }
]

# 现在想让他在products库有readWrite角色,在stock下有read权限,可以这么写
use test
db.grantRolesToUser(
   "test",
   [ "readWrite" , { role: "read", db: "stock" } ]
)

# 添加完成之后,权限是这样的
{
  _id: 'test.test',
  userId: new UUID("aed5f9db-b2d9-4e5f-81ae-a96738f74a78"),
  user: 'test',
  db: 'test',
  roles: [
    { role: 'readWrite', db: 'test' },
    { role: 'read', db: 'stock' },
    { role: 'read', db: 'test' }
  ],
  mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}

收回授权

以上面的作为案例,收回对 { role: 'readWrite', db: 'test' },{ role: 'read', db: 'stock' }的权限

bash
use test
db.revokeRolesFromUser( "test",[ { role: "read", db: "stock" }, "readWrite" ])

test> db.getUser("test")
{
  _id: 'test.test',
  userId: new UUID("aed5f9db-b2d9-4e5f-81ae-a96738f74a78"),
  user: 'test',
  db: 'test',
  roles: [ { role: 'read', db: 'test' } ],
  mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}