* 文檔
文檔是MongoDB的核心概念,文檔就是鍵值對的一個有序集
文檔一般有多個鍵值對,一個文檔中的值可以有多種不同的數據類型,文檔中的鍵是字符串,鍵不能含有空字符$
注意
1)MongoDB不但要區分類型,也要區分大小寫
2)MongoDB的文檔不能有重復的鍵
3)文檔中的鍵值對是有序的
* 集合
集合是一組文檔,如果將MongoDB中一個文檔看作關系型數據庫的一行,那么一個集合就相當于一張表
動態集合:
特點:
集合里面的文檔可以是各式各樣的
我們會把相關類型的文檔組合在一起
* 命名
1)集合名的命名滿足如下條件
不能是空字符串
不能包含空字符
不能以system.開頭
不能在集合名中含有$字符
2)子集合
組織集合的一種慣例是使用"."來分隔不同命名空間的子集合。
* 數據庫
在MongoDB中,多個文檔合成集合,多個集合組合成數據庫,一個mongoDB實例中可以含有多個數據庫,每個數據庫可以含有多個集合,每個數據庫有獨立的權限,不同的數據庫存儲在不同的文件中。
二)mongod的常用選項
* fork={true|false} mongod是否允許在后臺
* bind_ip 指定監聽地址
* port 27017
* maxConns 最大的并發連接數,默認為1000000
* logappend 有這些才能實現日志滾動
* --slowms arg (=100) 慢查詢,多少時間才判斷為慢查詢
*
三)Mongodb安全
https://docs.mongodb.com/manual/reference/built-in-roles/
安全參考:
1、內置角色
數據庫用戶角色
* read 提供對所有讀取數據的能力

* readWrite:提供read角色的所有權限
數據庫管理角色

群集管理角色

備份和恢復角色

1)啟用身份驗證
* 創建用戶
> use admin
switched to db admin
> db.createUser({user: "mytest",pwd:"abc123",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
Successfully added user: {
"user" : "mytest",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
創建用戶管理員。
在admin數據庫中,添加具有userAdminAnyDatabase角色的用戶
* 修改配置文件/etc/mongod.conf
#security:
security:
authorization: enabled
然后重啟mongod(systemctl restart mongod)
[root@node1 ~]# mongo --host 10.2.13.187 --port 27017 -u "mytest" -p "abc123" --authenticationDatabase "admin"
* 然后創建一個授權,授權用戶myTester可以對person和foo數據庫的權限
*> db.createUser({user:"myTester",pwd:"xyz123",roles:[{role:"readWrite",db:"person"},{role:"readWrite",db:"foo"}]})
Successfully added user: {
"user" : "myTester",
"roles" : [
{
"role" : "readWrite",
"db" : "person"
},
{
"role" : "readWrite",
"db" : "foo"
}
]
}
然后測試
~~~
[root@node1 ~]# mongo --host 10.2.13.187 --port 27017 -u "myTester" -p "xyz123"
MongoDB shell version v3.6.3
connecting to: mongodb://10.2.13.187:27017/
MongoDB server version: 3.6.3
> show dbs (報錯,發現沒有權限)
2018-03-09T16:53:34.174+0800 E QUERY [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0, $db: \"admin\" }",
"code" : 13,
"codeName" : "Unauthorized"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1
shellHelper.show@src/mongo/shell/utils.js:816:19
shellHelper@src/mongo/shell/utils.js:706:15
@(shellhelp2):1:1
> use foo
switched to db foo
> show collections;
blog
email
foo
games
> db.foo.find()
{ "_id" : ObjectId("5a97a6d0c51b3387784da133"), "name" : "joe", "age" : 35 }
{ "_id" : ObjectId("5a97a6dec51b3387784da134"), "name" : "alex", "age" : 30 }
{ "_id" : ObjectId("5a97a6e2c51b3387784da135"), "name" : "yuki", "age" : 20 }
{ "_id" : ObjectId("5a97a6e7c51b3387784da136"), "name" : "louis", "age" : 20 }
> use person
switched to db person
> show collections;
classes
foo.batchinsert
food
mycoll
person
products
records
> db.mycoll.find()
{ "_id" : ObjectId("5aa0c8890f4928baa57db04d"), "name" : "louis", "age" : 20 }
{ "_id" : ObjectId("5aa0c8a20f4928baa57db04e"), "name" : "alex", "age" : 22 }
{ "_id" : ObjectId("5aa0c8ac0f4928baa57db04f"), "name" : "yuki", "age" : 27 }
~~~