標籤:
目錄:
一.mongoDB 啟動配置
二.匯出,匯入,運行時備份
三.Fsync鎖,資料修複
四.使用者管理,安全認證
一、啟動項 mongod --help
C:\Windows\system32>mongod --help
1.常用配置項
--dbpath |
指定資料庫的目錄,預設在window下是c:\data\db\ |
--port |
指定伺服器監聽的連接埠號碼碼,預設是27017 |
--fork |
用守護進程的方式啟動mongoDB |
--logpath |
指定日誌的輸出路徑,預設是控制台 |
--config |
指定啟動項用檔案的路徑 |
--auth |
用安全認證方式啟動資料庫(MongoDB預設不啟用) |
2.利用config設定檔來啟動資料庫改變連接埠為8888
①建立一個mongodb.config檔案
指定資料庫檔案的目錄和要使用的連接埠號碼
dbpath = D:\MongoDBData
port = 8888
②輸入mongod --config D:\MongoDB\mongodb.config 啟用配置
③啟動MongoDB
Cmd中輸入 mongo 127.0.0.1:8888/admin
3.停止mongoDB服務
①ctrl+c 按鍵組合可以關閉資料庫
②admin資料庫命令關閉資料
二、匯出,匯入,運行時備份
1.匯出資料(中斷其他動作)
Mongodb中的mongoexport工具可以把一個collection匯出成JSON格式或CSV格式的檔案。可以通過參數指定匯出的資料項目,也可以根據指定的條件匯出資料。
匯出命令選項說明:
- -h:指明資料庫宿主機的IP
- -u:指明資料庫的使用者名稱
- -p:指明資料庫的密碼
- -d:指明資料庫的名字
- -c:指明collection的名字
- -f:指明要匯出那些列
例:
①把資料庫mongoDBTest中的Students匯出
使用mongoexport命令進行匯出資料,注意不添加host和port,預設使用的是host=127.0.0.1、port=27017
最後查看匯出的路徑是否有剛剛操作後的檔案。
預設是匯出json,但是也可以指定匯出csv檔案,這個就要使用--csv參數和-f參數進行組合
第一次失敗的原因在於,沒有指定需要匯出的列,所以要使用-f來進行指定。
2.匯入資料(中斷其他動作)
Mongodb中的mongoimport工具可以把一個特定格式檔案中的內容匯入到指定的collection中。該工具可以匯入JSON格式資料,也可以匯入CSV格式資料
匯入命令選項說明:
- -h:指明資料庫宿主機的IP
- -u:指明資料庫的使用者名稱
- -p:指明資料庫的密碼
- -d:指明資料庫的名字
- -c:指明collection的名字
- -f:指明要匯入那些列
例:
①把文檔Students匯入到mongoDBTest資料庫
先把mongoDBTest中的Students進行刪除
> db.Students.drop()
匯入剛剛的Students.cvs檔案
參數說明:
--type:指明要匯入的檔案格式
--headerline:指明第一行是列名,不需要匯入
--file:指明要匯入的檔案
查看匯入完成的結果
3.MongoDB的備份
用mongodump 來做MongoDB 的庫或表層級的備份
- -h:指明資料庫宿主機的IP
- -u:指明資料庫的使用者名稱
- -p:指明資料庫的密碼
- -d:指明資料庫的名字
- -c:指明collection的名字
- -o:輸出目錄
- -q:json query(json查詢)
例:
備份MongoDBTest資料庫
C:\Windows\system32>mongodump -d mongoDBTest -o D:\mongoDBTest
D:\mongoDBTest是存放備份資料的目錄
4.MongoDB的恢複
用mongorestore 來做MongoDB 的庫或表層級的資料恢複
- -u:指明資料庫的使用者名稱
- -p:指明資料庫的密碼
- -d:指明資料庫的名字
- -c:指明collection的名字
例:
先刪除MongoDBTest然後在進行恢複
> db.dropDatabase()
{ "dropped" : "mongoDBTest", "ok" : 1 }
恢複:
C:\Windows\system32>mongorestore -d mongoDBTest D:\mongoDBTest\mongoDBTest
三、Fsync鎖和資料修複
為什麼有鎖的概念呢,這個和MongoDB的是實現機理有關,在資料庫和使用者讀寫直接有一個緩衝池,當使用者進行備份的時候只備份資料庫中的內容,緩衝池中的內容就不進行備份了,這時鎖的作用就體現了,上鎖之後可以將緩衝池的資料全部先放入資料庫,這樣就保證了資料的完整性。
、
2.上鎖和解鎖
注意:上鎖和解鎖要切換到admin資料庫
上鎖
db.runCommand({fsync:1,lock:1});
解鎖
db.currentOp()
3.資料修複
當停電等無法復原轉災難來臨的時候,由於mongodb的儲存結構導致會產生垃圾資料,在資料恢複以後這垃圾資料依然存在,這是資料庫提供一個自我修複的能力.使用起來很簡單
db.repairDatabase()
四、使用者管理和安全認證
1.添加一個使用者
db.addUser(“名稱”,”密碼”);
2.啟用使用者
db.auth(“名稱”,”密碼”)
3.安全檢查 --auth
例:
①建立一個屬於admin資料庫的使用者
> db.addUser("Li","123");
WARNING: The ‘addUser‘ shell helper is DEPRECATED. Please use ‘createUser‘ instead
Successfully added user: { "user" : "Li", "roles" : [ "root" ] }
②建立一個資料庫mongoDBTest的使用者
> use mongoDBTest
switched to db mongoDBTest
> db.addUser("LiCheng","123");
WARNING: The ‘addUser‘ shell helper is DEPRECATED. Please use ‘createUser‘ instead
Successfully added user: { "user" : "LiCheng", "roles" : [ "dbOwner" ] }
③首先啟用安全檢查
修改mongodb.config為如下:
dbpath = D:\MongoDBData
auth=true
啟動MongoDB服務
C:\Windows\system32>mongod --config D:\MongoDB\mongodb.config
④test使用者非mongoDBTest是不能操作資料庫的
使用test使用者登入,並切換到mongoDBTest資料庫,查看Students文檔的內容
C:\Windows\system32>mongo 127.0.0.1:27017/test
MongoDB shell version: 2.6.5
connecting to: 127.0.0.1:27017/test
> use mongoDBTest
switched to db mongoDBTest
> db.Students.find()
error: { "$err" : "not authorized for query on mongoDBTest.Students", "code" : 13 }
>
⑤切換使用者(啟用自己的使用者才能訪問)
> db.auth("LiCheng","123")
1
> db.Students.find()
{ "_id" : 1, "name" : "Zhao", "age" : 25, "country" : "USA", "sex" : "M" }
{ "_id" : 2, "name" : "Qian", "age" : 22, "country" : "USA", "sex" : "M" }
{ "_id" : 3, "name" : "Sun", "age" : 26, "country" : "USA", "sex" : "M" }
{ "_id" : 4, "name" : "Li", "age" : 27, "country" : "China", "sex" : "" }
{ "_id" : 5, "name" : "Zhou", "age" : 30, "country" : "China", "sex" : "" }
{ "_id" : 6, "name" : "Wu", "age" : 27, "country" : "Japan", "sex" : "" }
{ "_id" : 7, "name" : "Zheng", "age" : 27, "country" : "UK", "sex" : "" }
{ "_id" : 8, "name" : "Wang", "age" : 26, "country" : "Korea", "sex" : "" }
{ "_id" : 9, "name" : "Xu", "age" : 26, "country" : "Japan", "sex" : "" }
>
⑥非admin資料庫的使用者不能使用資料庫命令
繼續使用LiCheng使用者
> show dbs
2016-03-28T11:47:21.698+0800 listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases:1.0 }",
"code" : 13
} at src/mongo/shell/mongo.js:47
>
⑦admin資料庫中的資料經過認證為管理使用者
切換到admin使用者登入,再啟用Li使用者
C:\Windows\system32>mongo 127.0.0.1:27017/admin
MongoDB shell version: 2.6.5
connecting to: 127.0.0.1:27017/admin
> db.auth("Li","123")
1
> show dbs
admin 0.078GB
local 0.078GB
mongoDBTest 0.078GB
>
⑧.使用者刪除操作
db.system.users.remove({user:"LiCheng"});
MongoDB啟動配置等