侧边栏壁纸
博主头像
★街角晚灯★博主等级

博观而约取 厚积而薄发

  • 累计撰写 437 篇文章
  • 累计创建 181 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

MongoDB语句

WinJay
2023-08-21 / 0 评论 / 0 点赞 / 106 阅读 / 11428 字 / 正在检测是否收录...
温馨提示:
文章发布较早,内容可能过时,阅读注意甄别。。。。

MongoDB语句 # mongodb # mongo

image-20230821163800084

启动服务

cat docker_start_Train-MongoDB.sh
#!/bin/bash
# author:WinJayX
# date:2020-05-07
# Maintainer WinJayX <WinJayX@Gmail.com>
# func:MongoDB Docker Container
# !!! -p 参数说明 -p :前的为宿主机的端口,后的为容器业务所需的端口!!!
#    -e MONGO_INITDB_ROOT_USERNAME=settlement \
#    -e MONGO_INITDB_ROOT_PASSWORD=vJAxnz4laVMuK_CfdF0 \
#    mongo:4.2.23 --auth
docker rm -f Train-Mongo
docker run -d --name Train-Mongo \
    -u root \
    -h MongoDB \
    -p 37017:27017 \
    --restart always \
    --volume `pwd`/MongoData:/data/db \
    --volume /etc/localtime:/etc/localtime:ro \
    mongo:4.2.23


./docker_start_Train-MongoDB.sh
Error response from daemon: No such container: Train-Mongo
aed4efe205b87d0366e7740a177c7d37b463883b491c483a65b6b24b11ab27d8

进入容器

  • 指定admin数据库
  • MongoDB V6版本以后的进入命令是docker exec -it Train-Mongo mongosh admin
docker exec -it Train-Mongo mongo admin

image-20230821145728021

docker exec -it Mongo-Dev mongo admin

image-20230821115934009

创建用户

db.createUser({user:"admin", pwd:"admin", roles:[{role:"userAdminAnyDatabase", db:"admin"}]});

image-20230821115846536

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

db.createUser({ user: "root" , pwd: "root", roles: ["root"]})

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

image-20230821151111478

db.createUser({ user:'admin',pwd:'admin',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]});

image-20230821131811969

用户示例

分类role(角色)简要说明
数据库用户角色(DB User Roles)read readWrite为某个数据库创建一个用户, 分配该数据库的读写权力
数据库管理员角色(DB Admin Roles)dbAdmin dbOwner userAdmin拥有创建数据库, 和创建用户的权力
集群管理角色(Culster Administration Roles)clusterAdmin clusterManager clusterMonitor hostManager管理员组, 针对整个系统进行管理
备份还原角色(Backup and Restoration Roles)backup restore备份数据库, 还原数据库
所有数据库角色(All-Database Roles)readAnyDatabase readWriteAnyDatabase userAdminAnyDatabase dbAdminAnyDatabase拥有对admin操作的权限
Superuser Roles(超级管理员)rootdbOwner userAdmin userAdminAnyDatabase这几个角色角色提供了任何数据任何用户的任何权限的能力,拥有这个角色的用户可以在任何数据库上定义它们自己的权限
db.createUser({user:"settlement", pwd:"vJAxnz4laVMuK_CfdF0", roles:[{role:"userAdminAnyDatabase", db:"admin"}]});

 db.createUser({user:"test1", pwd:"test", roles:[{role:"readWrite", db:"test_admin"}]});
Successfully added user: {
	"user" : "test1",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "test_admin"
		}
	]
}

db.createUser({user:"db_evaluate", pwd:"db_evaluate_hyhl", roles:[{role:"readWrite", db:"db_evaluate"}]});

image-20230821141431798

Use

db.createUser({user:"db_evaluate", pwd:"db_evaluate_hyhl", roles:[{role:"root", db:"db_evaluate"}]});
// 运行报错
E  QUERY    [js] uncaught exception: Error: couldn't add user: No role named root@db_evaluate
db.createUser({user:"db_evaluate", pwd:"db_evaluate_hyhl", roles:["root"]});
Successfully added user: { "user" : "db_evaluate", "roles" : [ "root" ] }

image-20230821140243364

验证权限:

image-20230821132711984

image-20230821133955883

查看所有用户

>
> db.system.users.find().pretty()
{
	"_id" : "admin.settlement",
	"userId" : UUID("b48978c9-e5ed-43ad-9ce5-aa80d4c765af"),
	"user" : "settlement",
	"db" : "admin",
	"credentials" : {
		"SCRAM-SHA-1" : {
			"iterationCount" : 10000,
			"salt" : "2fOLYwk4TRpKAg9BqkzraA==",
			"storedKey" : "tylBJAKKxbpb+sfr5VnWnR+5DoI=",
			"serverKey" : "4VHfRB9KvgVPNsGW9AxtiL5RNFs="
		},
		"SCRAM-SHA-256" : {
			"iterationCount" : 15000,
			"salt" : "SMsJdXsMkWW6RQyPHFjn5Ax+I9nkzW+cdd7KQw==",
			"storedKey" : "FWlbQJoMGQLtldYZfd1LCVBXMqYqXRbq/RWi3b/3cVE=",
			"serverKey" : "4EX77k7qHS4boN3uzdcEKaAIcV+SKTuWnL5g/SdMIt0="
		}
	},
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}
{
	"_id" : "admin.admin",
	"userId" : UUID("f96d541f-9f99-4fba-bd48-d05d8f3d9d36"),
	"user" : "admin",
	"db" : "admin",
	"credentials" : {
		"SCRAM-SHA-1" : {
			"iterationCount" : 10000,
			"salt" : "6RVDKlksfNK5DTM/E9v5Ww==",
			"storedKey" : "2NfW+BnRFmBNEioq1GqQOFk2LMc=",
			"serverKey" : "TE8w6QhFAXrWuQMHPvWmheiNq/A="
		},
		"SCRAM-SHA-256" : {
			"iterationCount" : 15000,
			"salt" : "kACnxi/VJk0ugFU62YDDGe7IYSglx58/JGY5kw==",
			"storedKey" : "r6Wh2+TsyCOviLIfuIRXceJsz20DDQTMqTdUT/JQpKs=",
			"serverKey" : "Xe+ccfJyOvlubOx3sIrf+aBDg2N2vpwMqDpEYesNVyc="
		}
	},
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}
{
	"_id" : "test_admin.test",
	"userId" : UUID("15e297fc-9a8b-481c-acf2-75e7b9f53af7"),
	"user" : "test",
	"db" : "test_admin",
	"credentials" : {
		"SCRAM-SHA-1" : {
			"iterationCount" : 10000,
			"salt" : "w1lxE/pWevBI2tlYc2FbQg==",
			"storedKey" : "o85OWf9XyWHP9qvoynbyrdcFquY=",
			"serverKey" : "9Yx/FJDtoIIiOoJGniOMnZOunHI="
		},
		"SCRAM-SHA-256" : {
			"iterationCount" : 15000,
			"salt" : "dD35EHNgt19yUI19ySQVGPNBc/IAgeu766c7/w==",
			"storedKey" : "SM+fNJLbtp3dDPYHwTjede0ki7ReVXucQHlQvrhPvvo=",
			"serverKey" : "mJq8tJwDjMpHV6F0dGop3XhfzmE0Mj+QwHiG9PvQ/aA="
		}
	},
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "test_admin"
		}
	]
}
{
	"_id" : "admin.test1",
	"userId" : UUID("ccc796f3-29f4-4513-a0b3-d3d775f3612a"),
	"user" : "test1",
	"db" : "admin",
	"credentials" : {
		"SCRAM-SHA-1" : {
			"iterationCount" : 10000,
			"salt" : "OiPWsU6XPR+o7LUW87wufA==",
			"storedKey" : "N8cWsnWmBzxNsxYjTqJTthJCU1w=",
			"serverKey" : "9feX9Qnr7wJEurICUL4g7I30e8U="
		},
		"SCRAM-SHA-256" : {
			"iterationCount" : 15000,
			"salt" : "SXqZX7vcM6xyuH7zw6dXDRriovmf+cET2B0wpQ==",
			"storedKey" : "2L9pcfYmpHHs84M02w3Dmm3hAs4Q6n8k9MDVYPEXh/E=",
			"serverKey" : "37vo1wfGIsFb0neZK4cQ1g7paz40txVKraDTL2v5Y8k="
		}
	},
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "test_admin"
		}
	]
}
{
	"_id" : "admin.db_evaluate",
	"userId" : UUID("cb7943cf-7670-4c53-afec-b45135a6ef94"),
	"user" : "db_evaluate",
	"db" : "admin",
	"credentials" : {
		"SCRAM-SHA-1" : {
			"iterationCount" : 10000,
			"salt" : "EkmDlo7zqKnufjPZNvzgrA==",
			"storedKey" : "W1sC//fGavbQ4D0SV1P6oYfO7IM=",
			"serverKey" : "3LUZ2Qb4LvwWxm5DbgeuTn7o14c="
		},
		"SCRAM-SHA-256" : {
			"iterationCount" : 15000,
			"salt" : "xwy3AbhHpF7noP5MIiyJHekL9eFtojQOJwpeuw==",
			"storedKey" : "P4pOuS30z1JbjbiMJjfKhgOCwyDgLzjqYnsYOmT+4rI=",
			"serverKey" : "6kHG4iY+zA34/rR7ZpMvmXFKg3ASqYizMgK/E9abvtk="
		}
	},
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}

删除用户

image-20230821135652377

> db.dropUser('db_evaluate')
true
# db_evaluate 用户名
db.createUser({
  user: "litsoft",
  pwd: "litsoft2023",
  roles: [ { role: "root", db: "admin" }]
})

db.createUser({ user: "litsoft" , pwd: "litsoft2023", roles: ["root"]})



use lit_db_test

show dbs

db.db_evaluate.insertOne({"name":"联和利泰"})


db.dropDatabase()

删除数据库

use lit_db_test
db.dropDatabase()
use lit_db_test


show dbs

db.lit_db_test.insertOne({"name":"联和利泰"})


db.dropDatabase()
use lit_db_test


show dbs


db.lit_db_test.insertOne({"name":"联和利泰"})
0

评论区