MongoDB创建用户设置密码和基本命令的使用

第一步 连接本机

# mongo ‐‐host  ‐‐port 
mongo #我的是默认本机,所以不用跟参数

第二步 设置密码

> use admin
switched to db admin
> db.createUser({
... user: "allen",
... pwd: "111111",
... roles: [ "root" ]
... })
Successfully added user: { "user" : "allen", "roles" : [ "root" ] }

查看用户

> show users
{
	"_id" : "admin.allen",
	"userId" : UUID("f54e1859-80bd-4e9f-b8fb-fb4b805bd68e"),
	"user" : "allen",
	"db" : "admin",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}

第三步 重启服务

> db.shutdownServer()
server should be down...
> exit
bye

第四步 已授权模式启动,用密码登录

[allen@localhost db]$ mongod --auth --fork --logpath /data/db/logpath/oup.out
about to fork child process, waiting until server is ready for connections.
forked process: 78172
child process started successfully, parent exiting
[allen@localhost db]$ mongo -u allen
MongoDB shell version v4.4.6
Enter password: 
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("e0d7bf13-89d0-4e14-a41b-666d4521bf75") }
MongoDB server version: 4.4.6

基本使用:

> db
test
> show databases
admin   0.000GB
config  0.000GB
local   0.000GB
> use demo
switched to db demo
> db.members.insertOne({"name":"zhang3",age:20})  #插入一条数据
> db.members.insertOne({"_id":"0001","name":"zhang3",age:20}) #插入一条自定义id的数据
> show tables #查看所有table
members
> db.members.find() #查询table的数据
{ "_id" : ObjectId("60d337588f6e6b6a144f7fa8"), "name" : "zhang3", "age" : 20 }
{ "_id" : "0001", "name" : "zhang3", "age" : 20 }
> ObjectId("60d337588f6e6b6a144f7fa8").getTimestamp() #根据id查看时间
ISODate("2021-06-23T13:30:00Z")
> function add(a,b){return a*b ;}         #定义方法
> print (add(200,5))
1000

一次插入多条数据,如果有一条失败,后面的就会失败。

db.members.insertMany([{"_id":"0002","name":"zhang2",age:20},{"_id":"0003","name":"zhang30",age:20},{"_id":"0001","name":"zhang3",age:20},{"_id":"0004","name":"zhang4",age:20}],{orderd:true}) 

> db.members.insertMany([{"_id":"0002","name":"zhang2",age:20},{"_id":"0003","name":"zhang30",age:20},{"_id":"0001","name":"zhang3",age:20},{"_id":"0004","name":"zhang4",age:20}],{orderd:true})
uncaught exception: BulkWriteError({
	"writeErrors" : [
		{
			"index" : 2,
			"code" : 11000,
			"errmsg" : "E11000 duplicate key error collection: demo.members index: _id_ dup key: { _id: \"0001\" }",
			"op" : {
				"_id" : "0001",
				"name" : "zhang3",
				"age" : 20
			}
		}
	],
	"writeConcernErrors" : [ ],
	"nInserted" : 2,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
}) :
BulkWriteError({
	"writeErrors" : [
		{
			"index" : 2,
			"code" : 11000,
			"errmsg" : "E11000 duplicate key error collection: demo.members index: _id_ dup key: { _id: \"0001\" }",
			"op" : {
				"_id" : "0001",
				"name" : "zhang3",
				"age" : 20
			}
		}
	],
	"writeConcernErrors" : [ ],
	"nInserted" : 2,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})

你可能感兴趣的:(Mongo,分布式系统,mongo)