5.非关系型数据库(Nosql)之mongodb:创建集合,备份与导入导出, 数据还原,导入导出
1固定集合
固定集合值得是事先創(chuàng)建而且大小固定的集合
2固定集合的特征:固定集合很像環(huán)形隊(duì)列,如果空間不足,最早文檔就會被刪除,為新的文檔騰出空間。一般來說,固定集合適用于任何想要自動淘汰過期屬性的場景,沒有太多的操作限制。
3創(chuàng)建固定集合使用命令:
db.createCollection(“collectionName”,{capped:true,size:100000,max:100});
size:指定集合大小,單位為KB,max指定文檔的數(shù)量
當(dāng)指定文檔數(shù)量上限時(shí),必須同時(shí)指定大小。淘汰機(jī)制只有在容量還沒有滿時(shí)才會依據(jù)文檔數(shù)量來工作。要是容量滿了,淘汰機(jī)制依據(jù)容量來工作。
4創(chuàng)建一個(gè)集合:
>db.createCollection("cap1",{capped:true,size:1000,max:100});
{ "ok" : 1 }
>?
5插入數(shù)據(jù)
> for(var i=1;i<=100;i++){
...db.cap1.insert({name:"dongxue",age:i});
... }
WriteResult({ "nInserted" : 1 })
> db.cap1.find().count();
53???(大小之所以是53是因?yàn)榇笮〕^了1000)
6固定集合的應(yīng)用場景:聊天記錄,日志信息
??????淘汰機(jī)制:當(dāng)滿足size指定集合大小,不能再繼續(xù)往固定集合中加數(shù)據(jù)。
??????固定集合的容量優(yōu)先
當(dāng)文檔達(dá)到100條時(shí),再添加的時(shí)候會替換先前的
?
7備份與導(dǎo)入導(dǎo)出。
MongoDB提供了備份和回復(fù)的功能,分別是MongoDB下載目錄下的mongodump.exe和mongorestore.exe文件。
?
備份數(shù)據(jù)使用下面的命令:
mongodump –h dbhost –d dbname –o dbdirectory
-h:MonDB所在服務(wù)器地址,例如:127.0.0.1,當(dāng)然也可以指定端口號:127.0.0.1:27017,當(dāng)然該目錄需要提前創(chuàng)建,在備份完成后,系統(tǒng)自動在dump目錄下建立一個(gè)beifeng目錄,這個(gè)目錄里面存放該數(shù)據(jù)庫實(shí)例的備份數(shù)據(jù)。?
mongodump -h localhost:27017 -d toto -o f:/beifeng
-h:用來指定要輸出的數(shù)據(jù)庫所在的ip地址和端口號
-d:?指定要備份的數(shù)據(jù)庫
-o:?表示要備份到的文件目錄
執(zhí)行后的效果圖:
7另外啟動一個(gè)mongodb的客戶端,目的是將數(shù)據(jù)庫中toto數(shù)據(jù)庫刪掉
C:\Users\to-to>mongo MongoDB shell version: 2.6.4 connecting to: test > use toto; switched to db toto > db.help(); DB methods: ???????db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.r unCommand(...) ] ???????db.auth(username, password) ???????db.cloneDatabase(fromhost) ???????db.commandHelp(name) returns the help for the command ???????db.copyDatabase(fromdb, todb, fromhost) ???????db.createCollection(name, { size : ..., capped : ..., max : ... } ) ???????db.createUser(userDocument) ???????db.currentOp() displays currently executing operations in the db ???????db.dropDatabase() ???????db.eval(func, args) run code server-side ???????db.fsyncLock() flush data to disk and lock server for backups ???????db.fsyncUnlock() unlocks server following a db.fsyncLock() ???????db.getCollection(cname) same as db['cname'] or db.cname ???????db.getCollectionNames() ???????db.getLastError() - just returns the err msg string ???????db.getLastErrorObj() - return full status object ???????db.getMongo() get the server connection object ???????db.getMongo().setSlaveOk() allow queries on a replication slave server ???????db.getName() ???????db.getPrevError() ???????db.getProfilingLevel() - deprecated ???????db.getProfilingStatus() - returns if profiling is on and slow threshold ???????db.getReplicationInfo() ???????db.getSiblingDB(name) get the db at the same server as this one ???????db.getWriteConcern() - returns the write concern used for any operations on this db, inherit ed from server object if set ???????db.hostInfo() get details about the server's host ???????db.isMaster() check replica primary status ???????db.killOp(opid) kills the current operation in the db ???????db.listCommands() lists all the db commands ???????db.loadServerScripts() loads all the scripts in db.system.js ???????db.logout() ???????db.printCollectionStats() ???????db.printReplicationInfo() ???????db.printShardingStatus() ???????db.printSlaveReplicationInfo() ???????db.dropUser(username) ???????db.repairDatabase() ???????db.resetError() ???????db.runCommand(cmdObj) run a database command.? if cmdObj is a string, turns it into { cmdObj ?: 1 } ???????db.serverStatus() ???????db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all ???????db.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the db ???????db.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the db ???????db.setVerboseShell(flag) display extra information in shell output ???????db.shutdownServer() ???????db.stats() ???????db.version() current version of the server > db.dropDatabase(); { "dropped" : "toto", "ok" : 1 } >? |
8數(shù)據(jù)還原
mongorestore -h localhost:27017 -d toto -directoryperdb F:/beifeng/toto -h:MongoDB所在服務(wù)器地址,例如:127.0.0.1,當(dāng)然也可以指定端口號:127.0.0.1:27017 -d:需要備份的數(shù)據(jù)庫實(shí)例,例如toto -o:備份的數(shù)據(jù)存放位置,當(dāng)然該目錄需要提前建立,在備份完成后,系統(tǒng)自動在dump目錄下建立一個(gè)test目錄,這個(gè)目錄里面存放數(shù)據(jù)庫實(shí)例的備份數(shù)據(jù)。 |
C:\Users\to-to>mongorestore -h localhost:27017 -d toto -directoryperdb F:/beifeng/toto connected to: localhost:27017 2014-10-15T23:19:11.071+0800 F:/beifeng/toto\c3.bson 2014-10-15T23:19:11.071+0800???going into namespace [toto.c3] 2014-10-15T23:19:14.009+0800???????????Progress: 5740200/54000000????? 10%????(bytes) 2014-10-15T23:19:17.010+0800???????????Progress: 10125000/54000000???? 18%????(bytes) 2014-10-15T23:19:20.010+0800???????????Progress: 15660000/54000000???? 29%????(bytes) 2014-10-15T23:19:23.011+0800???????????Progress: 22528800/54000000???? 41%????(bytes) 2014-10-15T23:19:26.013+0800???????????Progress: 29586600/54000000???? 54%???? (bytes) 2014-10-15T23:19:29.013+0800???????????Progress: 36752400/54000000???? 68%????(bytes) 2014-10-15T23:19:32.000+0800???????????Progress: 43372800/54000000???? 80%????(bytes) 2014-10-15T23:19:35.001+0800???????????Progress: 50284800/54000000???? 93%????(bytes) 1000000 objects found 2014-10-15T23:19:36.579+0800???Creating index: { key: { _id: 1 }, name: "_id_", ns: "toto.c3" } 2014-10-15T23:19:36.641+0800???Creating index: { unique: true, key: { age: 1 }, name: "age_1", ns: "toto.c3" } 2014-10-15T23:19:41.440+0800 F:/beifeng/toto\cap1.bson 2014-10-15T23:19:41.440+0800???going into namespace [toto.cap1] 2014-10-15T23:19:41.440+0800???Created collection toto.cap1 with options: { "create" : "cap1", "cap ped" : true, "size" : 4096, "max" : 100 } 53 objects found 2014-10-15T23:19:41.440+0800???Creating index: { key: { _id: 1 }, name: "_id_", ns: "toto.cap1" } 2014-10-15T23:19:41.440+0800 F:/beifeng/toto\cap2.bson 2014-10-15T23:19:41.440+0800???going into namespace [toto.cap2] 2014-10-15T23:19:41.440+0800???Created collection toto.cap2 with options: { "create" : "cap2", "cap ped" : true, "size" : 4096, "max" : 100 } file F:/beifeng/toto\cap2.bson empty, skipping 2014-10-15T23:19:41.456+0800???Creating index: { key: { _id: 1 }, name: "_id_", ns: "toto.cap2" } ? C:\Users\to-to> ? |
?
9導(dǎo)入導(dǎo)出:
用到的應(yīng)用mongoexport,mongoimport
mongoexport –h dhost –d dbname –c collectionName –o output 參數(shù)說明: -h數(shù)據(jù)庫地址 -d指明使用的庫 -c指明要導(dǎo)出的集合 -o指明要導(dǎo)出的文件名 dname:表示要導(dǎo)出的數(shù)據(jù)庫 collectionName:表示導(dǎo)出哪個(gè)集合 output:表示導(dǎo)出到的位置。 |
C:\Users\to-to>mongoexport -h localhost:27017 -d toto -c c3 -o f:/beifen/c3.txt connected to: localhost:27017 exported 1000000 records ? C:\Users\to-to> 同樣可以數(shù)據(jù)導(dǎo)出到doc中 |
數(shù)據(jù)導(dǎo)入: mongoimport -h localhost:27017 -d toto -c ccc f:/beifen/c3.txt C:\Users\to-to>mongo MongoDB shell version: 2.6.4 connecting to: test > use toto switched to db toto > show tables; c3 cap1 cap2 ccc system.indexes > db.ccc.find(); { "_id" : ObjectId("543e7473256769913d467e75"), "name" : "zhangsan", "age" : 1 } { "_id" : ObjectId("543e7473256769913d467e76"), "name" : "zhangsan", "age" : 2 } { "_id" : ObjectId("543e7473256769913d467e77"), "name" : "zhangsan", "age" : 3 } { "_id" : ObjectId("543e7473256769913d467e78"), "name" : "zhangsan", "age" : 4 } { "_id" : ObjectId("543e7473256769913d467e79"), "name" : "zhangsan", "age" : 5 } { "_id" : ObjectId("543e7473256769913d467e7a"), "name" : "zhangsan", "age" : 6 } { "_id" : ObjectId("543e7473256769913d467e7b"), "name" : "zhangsan", "age" : 7 } { "_id" : ObjectId("543e7473256769913d467e7c"), "name" : "zhangsan", "age" : 8 } { "_id" : ObjectId("543e7473256769913d467e7d"), "name" : "zhangsan", "age" : 9 } { "_id" : ObjectId("543e7473256769913d467e7e"), "name" : "zhangsan", "age" : 10 } { "_id" : ObjectId("543e7473256769913d467e7f"), "name" : "zhangsan", "age" : 11 } { "_id" : ObjectId("543e7473256769913d467e80"), "name" : "zhangsan", "age" : 12 } { "_id" : ObjectId("543e7473256769913d467e81"), "name" : "zhangsan", "age" : 13 } { "_id" : ObjectId("543e7473256769913d467e82"), "name" : "zhangsan", "age" : 14 } { "_id" : ObjectId("543e7473256769913d467e83"), "name" : "zhangsan", "age" : 15 } { "_id" : ObjectId("543e7473256769913d467e84"), "name" : "zhangsan", "age" : 16 } { "_id" : ObjectId("543e7473256769913d467e85"), "name" : "zhangsan", "age" : 17 } { "_id" : ObjectId("543e7473256769913d467e86"), "name" : "zhangsan", "age" : 18 } { "_id" : ObjectId("543e7473256769913d467e87"), "name" : "zhangsan", "age" : 19 } { "_id" : ObjectId("543e7473256769913d467e88"), "name" : "zhangsan", "age" : 20 } Type "it" for more >? 上面自動隱式創(chuàng)建了一個(gè)ccc集合。 |
?
9 mongodb安全認(rèn)證
每個(gè)mongodb實(shí)例中的數(shù)據(jù)庫都可以有許多用戶,如果開啟了安全性檢查,只有數(shù)據(jù)庫認(rèn)證用戶才能執(zhí)行讀或者寫操作。在認(rèn)證的上下文中,MongoDB會將普通的數(shù)據(jù)作為admin
數(shù)據(jù)庫處理。Admin數(shù)據(jù)庫中的用戶被視為超級用戶(即:管理員)
在認(rèn)證之后,管理員可以讀寫所有數(shù)據(jù)庫,執(zhí)行特定的管理員命令,執(zhí)行listDatabase和shutdown.
在開啟安全檢查之前,一定要至少一個(gè)管理員賬號。
最少得保證有一個(gè)管理員賬號(admin 數(shù)據(jù)庫當(dāng)中的用戶都是管理員)
use admin
db.addUser(“username”,”password”);
2.有了管理員賬號,就可以為其它的數(shù)據(jù)庫分配用戶。
???2.1 首先要跳轉(zhuǎn)到被分配的數(shù)據(jù)庫
3.需要重新啟動mongodb服務(wù),開啟安全檢查
4.接下來的客戶端連接mongodb,需要登錄才能執(zhí)行相應(yīng)的操作。
C:\Users\to-to>mongo localhost:27017/admin MongoDB shell version: 2.6.4 connecting to: localhost:27017/admin > db admin |
?
總結(jié)
以上是生活随笔為你收集整理的5.非关系型数据库(Nosql)之mongodb:创建集合,备份与导入导出, 数据还原,导入导出的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 国产汽车质量排名前五(国产汽车质量排名)
- 下一篇: 6.非关系型数据库(Nosql)之mon