mongodb安裝Windows服務、資料備份還原、資料失效、建立索引、主從配置
阿新 • • 發佈:2018-12-27
一、操作步驟
①準備一個mongodb安裝包②在安裝目錄下建立資料夾、修改配置項③安裝、解除安裝windows服務④匯入(還原)、匯出(備份)資料(單集合、整個庫)⑤建立索引⑥設定資料自動失效⑦DDL語句⑧mongodb視覺化工具-Robomongo⑨ mongodb主從配置、加入連線許可權二、詳細步驟
1、準備一個mongodb安裝包mongodb-win32-x86_64-2008plus-ssl-3.4.0-signed.msi3、安裝、解除安裝windows服務進入bin目錄下安裝Windows服務
mongod --logpath "D:\mongodb\data\logs\mongodb.log" --logappend --dbpath "D:\mongodb\data" --directoryperdb --serviceName "MongoDB" --serviceDisplayName "MongoDB" --install
引數說明:--logpath:日誌檔案
--dbpath:資料檔案
--serviceName:windows服務名
啟動用Robomongo 工具連線測試
刪除服務
SC DELETE MongoDB
4、匯入(還原)、匯出(備份)資料(1)準備資料,建立集合:student、subject(2)執行整個庫匯出:匯出後我們刪除這個庫建立資料夾
執行匯出匯出命令-整個庫:圖1
mongodump -h 127.0.0.1:27017 -d test_import -o d:\mongodb\back
引數說明:-d 後面是匯出資料庫名 -o 後面是匯出儲存路徑匯出命令-單個集合:引數說明:--collection 後面是集合名字--db 後面是匯出資料庫名--out 後面是備份集合的儲存位置、檔名稱mongoexport --host 127.0.0.1 --port 27017 --collection student --db test_import --out d:\mongodb\back\student.json
圖1
匯出資料:
刪除資料庫
執行匯入匯入命令-整個庫:圖1
mongorestore -h 127.0.0.1:27017 -d test_import --dir d:\mongodb\back\test_import
匯入命令-單個集合:引數說明:-d 後面是設定資料庫名--dir後面是要匯入的資料庫路徑匯入命令-單個集合:mongoimport --host 127.0.0.1 --port 27017 --collection logPolicy --db test_import --file d:\mongodb\back\student.json
匯入命令-單個集合:引數說明:--collection 後面是新集合名字--db 後面是入到的資料庫名--file 後面是匯入資料的存放路徑和檔名圖1
5、建立索引語法:
db.getCollection('student').ensureIndex({"id" : 1},{name:'student_id',background:true})
6、設定資料失效時間10秒示例:
db.testCollection.insert({"Date" : new Date(),"name":"zs","age":18})
db.testCollection.ensureIndex( { "Date": 1 }, { expireAfterSeconds: 10 } )
語法:如下圖所示db.student.ensureIndex( { "Date": 1 }, { expireAfterSeconds: 10} )
7、DDL語句切換/建立資料庫, 當建立一個集合(table)的時候會自動建立當前資料庫
use test_import
建立集合:db.createCollection("student")
集合插入資料db.student.insert({"Date" : new Date(),"name":"lisi","age":28})
db.subject.insert({"Date" : new Date(),"sid":"2","sname":"數學"})
查詢db.getCollection('student').find({'name':'zhangsan'})
8、Robomongo 工具使用9、mongodb主從配置