MongoDB
MongoDB是基于分布式文件系统存储的数据库,旨在为web应用提供可扩展的高可用数据储存解决方案,它是介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最像关系数据库的,它支持的结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。
它的特定是高性能、易部署、易使用,存储非常方便。
支持的功能特性:
- 面向集合的存储,易存储对象类型的数据
- 模式自由
- 支持动态查询
- 支持完全索引,包含内部对象
- 支持查询
- 支持复制和故障恢复
- 使用高效的二进制数据存储,包括大型对象(如视频)
- 自动处理碎片,以支持云计算层次的扩展性
- 支持多种编程语言
- 文件存储为bson
- 可通过网络访问
数据存储模型
列表模型:
应用场景:在分布式文件系统之上提供随机读写的分布式数据存储
典型产品:HBase、Hypertable、Cassandra
数据模型:以’列’为中心进行存储,将同一列表数据存储在一起
优点:快速查询、高可扩展性、易于实现分布式扩展文档模型:
应用场景:非强事务需求的web应用
典型产品:MongoDB,ElasticSearch,CouchDB,CouchBase Server
数据模型:键值模型,存储为文档
优点:数据模型无须事先定义键值模型:
应用场景:内容缓存,用于大量并行数据访问高负载场景
典型产品:Redis
数据模型:基于hash表实现key-value
优点:查询迅速图式模型:
应用场景:社交网络、推荐系统、关系图谱
典型产品:Neo4j、Infinite Graph
数据模型:图示结构
优点:适合图示计算场景
mongodb配置选项
可见通过安装包安装的mongodb只是存在二进制程序,所谓的配置文件就是酱mongod -h的配置选项,常见的选项有:
1 | fork=true|false mongod是否运行在后台 |
2 | bind_ip=0.0.0.0 启动以后所监听的ip,默认监听所有 |
3 | port=27017 指定监听的端口 |
4 | maxConns 并发连接数 |
5 | logpath |
6 | logappend 日志滚动 |
7 | pidfilepath |
8 | httpinterface 28017端口会启动,会用一个web页面 |
9 | repair 启动时先修复db,进程意外断开,修复db |
10 | journal 日志启用,与事务日志相近 |
11 | journalOptions arg 日志选项 |
12 | journalCommitInterval arg 日志提交的间隔 |
13 | |
14 | slowms 界定慢查询的时间 |
15 | profile 性能剖析 |
1 | ~]# cat /etc/mongod.conf |
2 | dbpath=/data/mongodb |
3 | logpath=/var/log/mongodb.log |
4 | pidfilepath=/var/run/mongod.pid |
5 | logappend=true |
6 | repair=true |
7 | port=27017 |
8 | fork=true |
9 | auth=false |
10 | httpinterface=false |
11 | bind_ip=0.0.0.0 |
12 | journal=true |
13 | quiet=true |
14 | ~]# ./mongod -f /etc/mongodb.conf |
mongodb的常见操作
常见命令:
1 | > help |
2 | db.help() help on db methods |
3 | db.mycoll.help() help on collection methods |
4 | sh.help() sharding helpers |
5 | rs.help() replica set helpers |
6 | help admin administrative help |
7 | help connect connecting to a db help |
8 | help keys key shortcuts |
9 | help misc misc things to know |
10 | help mr mapreduce |
11 | |
12 | show dbs show database names |
13 | show collections show collections in current database |
14 | show users show users in current database |
15 | show profile show most recent system.profile entries with time >= 1ms |
16 | show logs show the accessible logger names |
17 | show log [name] prints out the last segment of log in memory, 'global' is default |
18 | use <db_name> set current database |
19 | db.foo.find() list objects in collection foo |
20 | db.foo.find( { a : 1 } ) list objects in foo where a == 1 |
21 | it result of the last line evaluated; use to further iterate |
22 | DBQuery.shellBatchSize = x set default number of items to display on shell |
23 | exit quit the mongo shell |
24 | |
25 | > db.help() |
26 | DB methods: |
27 | db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.runCommand(...) ] |
28 | db.auth(username, password) |
29 | db.cloneDatabase(fromhost) |
30 | db.commandHelp(name) returns the help for the command |
31 | db.copyDatabase(fromdb, todb, fromhost) |
32 | db.createCollection(name, { size : ..., capped : ..., max : ... } ) |
33 | db.createView(name, viewOn, [ { $operator: {...}}, ... ], { viewOptions } ) |
34 | db.createUser(userDocument) |
35 | db.currentOp() displays currently executing operations in the db |
36 | db.dropDatabase() |
37 | db.eval() - deprecated |
38 | db.fsyncLock() flush data to disk and lock server for backups |
39 | db.fsyncUnlock() unlocks server following a db.fsyncLock() |
40 | db.getCollection(cname) same as db['cname'] or db.cname |
41 | db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections |
42 | db.getCollectionNames() |
43 | db.getLastError() - just returns the err msg string |
44 | db.getLastErrorObj() - return full status object |
45 | db.getLogComponents() |
46 | db.getMongo() get the server connection object |
47 | db.getMongo().setSlaveOk() allow queries on a replication slave server |
48 | db.getName() |
49 | db.getPrevError() |
50 | db.getProfilingLevel() - deprecated |
51 | db.getProfilingStatus() - returns if profiling is on and slow threshold |
52 | db.getReplicationInfo() |
53 | db.getSiblingDB(name) get the db at the same server as this one |
54 | db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set |
55 | db.hostInfo() get details about the server's host |
56 | db.isMaster() check replica primary status |
57 | db.killOp(opid) kills the current operation in the db |
58 | db.listCommands() lists all the db commands |
59 | db.loadServerScripts() loads all the scripts in db.system.js |
60 | db.logout() |
61 | db.printCollectionStats() |
62 | db.printReplicationInfo() |
63 | db.printShardingStatus() |
64 | db.printSlaveReplicationInfo() |
65 | db.dropUser(username) |
66 | db.repairDatabase() |
67 | db.resetError() |
68 | db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into { cmdObj : 1 } |
69 | db.serverStatus() |
70 | db.setLogLevel(level,<component>) |
71 | db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all |
72 | db.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the db |
73 | db.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the db |
74 | db.setVerboseShell(flag) display extra information in shell output |
75 | db.shutdownServer() |
76 | db.stats() |
77 | db.version() current version of the server |
78 | > db.stats() |
79 | { |
80 | "db" : "test", |
81 | "collections" : 0, |
82 | "views" : 0, |
83 | "objects" : 0, |
84 | "avgObjSize" : 0, |
85 | "dataSize" : 0, |
86 | "storageSize" : 0, |
87 | "numExtents" : 0, |
88 | "indexes" : 0, |
89 | "indexSize" : 0, |
90 | "fileSize" : 0, |
91 | "ok" : 1 |
92 | } |
93 | >db.serverStatus() mongodb数据库状态 |
94 | |
95 | > show dbs |
96 | admin 0.000GB |
97 | local 0.000GB |
98 | > use testdb |
99 | switched to db testdb |
100 | > show dbs |
101 | admin 0.000GB |
102 | local 0.000GB |
103 | testdb 0.000GB |
104 | > show collections |
105 | students |
106 | testcoll |
107 | > db.getCollectionNames() |
108 | [ "students", "testcoll" ] |
109 | > db.students.stats() |
DDL,DML
find()高级用法:
- 比较操作
1
> for (i=1;i<=10000;i++) db.test.insert({name:"test"+i,age:i%120,score:i%222})
2
WriteResult({ "nInserted" : 1 })
3
> db.test.find()
4
{ "_id" : ObjectId("594c855372bb243c2b6856b5"), "name" : "test1", "age" : 1, "score" : 1 }
5
{ "_id" : ObjectId("594c855372bb243c2b6856b6"), "name" : "test2", "age" : 2, "score" : 2 }
6
{ "_id" : ObjectId("594c855372bb243c2b6856b7"), "name" : "test3", "age" : 3, "score" : 3 }
7
{ "_id" : ObjectId("594c855372bb243c2b6856b8"), "name" : "test4", "age" : 4, "score" : 4 }
8
{ "_id" : ObjectId("594c855372bb243c2b6856b9"), "name" : "test5", "age" : 5, "score" : 5 }
9
{ "_id" : ObjectId("594c855372bb243c2b6856ba"), "name" : "test6", "age" : 6, "score" : 6 }
10
{ "_id" : ObjectId("594c855372bb243c2b6856bb"), "name" : "test7", "age" : 7, "score" : 7 }
11
{ "_id" : ObjectId("594c855372bb243c2b6856bc"), "name" : "test8", "age" : 8, "score" : 8 }
12
{ "_id" : ObjectId("594c855372bb243c2b6856bd"), "name" : "test9", "age" : 9, "score" : 9 }
13
{ "_id" : ObjectId("594c855372bb243c2b6856be"), "name" : "test10", "age" : 10, "score" : 10 }
14
{ "_id" : ObjectId("594c855372bb243c2b6856bf"), "name" : "test11", "age" : 11, "score" : 11 }
15
{ "_id" : ObjectId("594c855372bb243c2b6856c0"), "name" : "test12", "age" : 12, "score" : 12 }
16
{ "_id" : ObjectId("594c855372bb243c2b6856c1"), "name" : "test13", "age" : 13, "score" : 13 }
17
{ "_id" : ObjectId("594c855372bb243c2b6856c2"), "name" : "test14", "age" : 14, "score" : 14 }
18
{ "_id" : ObjectId("594c855372bb243c2b6856c3"), "name" : "test15", "age" : 15, "score" : 15 }
19
{ "_id" : ObjectId("594c855372bb243c2b6856c4"), "name" : "test16", "age" : 16, "score" : 16 }
20
{ "_id" : ObjectId("594c855372bb243c2b6856c5"), "name" : "test17", "age" : 17, "score" : 17 }
21
{ "_id" : ObjectId("594c855372bb243c2b6856c6"), "name" : "test18", "age" : 18, "score" : 18 }
22
{ "_id" : ObjectId("594c855372bb243c2b6856c7"), "name" : "test19", "age" : 19, "score" : 19 }
23
{ "_id" : ObjectId("594c855372bb243c2b6856c8"), "name" : "test20", "age" : 20, "score" : 20 }
24
Type "it" for more
25
> db.test.findOne()
26
{
27
"_id" : ObjectId("594c855372bb243c2b6856b5"),
28
"name" : "test1",
29
"age" : 1,
30
"score" : 1
31
}
32
> db.test.find().count()
33
10000
34
> db.test.findOneAndDelete()
35
{
36
"_id" : ObjectId("594c855372bb243c2b6856b5"),
37
"name" : "test1",
38
"age" : 1,
39
"score" : 1
40
}
41
> db.test.findOne()
42
{
43
"_id" : ObjectId("594c855372bb243c2b6856b6"),
44
"name" : "test2",
45
"age" : 2,
46
"score" : 2
47
}
48
> db.test.find().count()
49
9999
50
> db.test.findOneAndReplace({name:"test2"},{name:"test22"})
51
{
52
"_id" : ObjectId("594c855372bb243c2b6856b6"),
53
"name" : "test2",
54
"age" : 2,
55
"score" : 2
56
}
57
> db.test.findOne()
58
{ "_id" : ObjectId("594c855372bb243c2b6856b6"), "name" : "test22" }
59
60
61
比较操作:
62
> db.test.find({age:{$gt:22}})
63
{ "_id" : ObjectId("594c855372bb243c2b6856cb"), "name" : "test23", "age" : 23, "score" : 23 }
64
{ "_id" : ObjectId("594c855372bb243c2b6856cc"), "name" : "test24", "age" : 24, "score" : 24 }
65
{ "_id" : ObjectId("594c855372bb243c2b6856cd"), "name" : "test25", "age" : 25, "score" : 25 }
66
{ "_id" : ObjectId("594c855372bb243c2b6856ce"), "name" : "test26", "age" : 26, "score" : 26 }
67
{ "_id" : ObjectId("594c855372bb243c2b6856cf"), "name" : "test27", "age" : 27, "score" : 27 }
68
{ "_id" : ObjectId("594c855372bb243c2b6856d0"), "name" : "test28", "age" : 28, "score" : 28 }
69
{ "_id" : ObjectId("594c855372bb243c2b6856d1"), "name" : "test29", "age" : 29, "score" : 29 }
70
{ "_id" : ObjectId("594c855372bb243c2b6856d2"), "name" : "test30", "age" : 30, "score" : 30 }
71
{ "_id" : ObjectId("594c855372bb243c2b6856d3"), "name" : "test31", "age" : 31, "score" : 31 }
72
{ "_id" : ObjectId("594c855372bb243c2b6856d4"), "name" : "test32", "age" : 32, "score" : 32 }
73
{ "_id" : ObjectId("594c855372bb243c2b6856d5"), "name" : "test33", "age" : 33, "score" : 33 }
74
{ "_id" : ObjectId("594c855372bb243c2b6856d6"), "name" : "test34", "age" : 34, "score" : 34 }
75
{ "_id" : ObjectId("594c855372bb243c2b6856d7"), "name" : "test35", "age" : 35, "score" : 35 }
76
{ "_id" : ObjectId("594c855372bb243c2b6856d8"), "name" : "test36", "age" : 36, "score" : 36 }
77
{ "_id" : ObjectId("594c855372bb243c2b6856d9"), "name" : "test37", "age" : 37, "score" : 37 }
78
{ "_id" : ObjectId("594c855372bb243c2b6856da"), "name" : "test38", "age" : 38, "score" : 38 }
79
{ "_id" : ObjectId("594c855372bb243c2b6856db"), "name" : "test39", "age" : 39, "score" : 39 }
80
{ "_id" : ObjectId("594c855372bb243c2b6856dc"), "name" : "test40", "age" : 40, "score" : 40 }
81
{ "_id" : ObjectId("594c855372bb243c2b6856dd"), "name" : "test41", "age" : 41, "score" : 41 }
82
{ "_id" : ObjectId("594c855372bb243c2b6856de"), "name" : "test42", "age" : 42, "score" : 42 }
83
Type "it" for more
84
85
$gt 大于
86
$gte 大于等于
87
$lt 小于
88
$lte 小于等于
89
$ne 等于
90
91
92
> db.test.find({age:{$in:[22,23]}})
93
{ "_id" : ObjectId("594c855372bb243c2b6856ca"), "name" : "test22", "age" : 22, "score" : 22 }
94
{ "_id" : ObjectId("594c855372bb243c2b6856cb"), "name" : "test23", "age" : 23, "score" : 23 }
95
{ "_id" : ObjectId("594c855372bb243c2b685742"), "name" : "test142", "age" : 22, "score" : 142 }
96
{ "_id" : ObjectId("594c855372bb243c2b685743"), "name" : "test143", "age" : 23, "score" : 143 }
97
{ "_id" : ObjectId("594c855372bb243c2b6857ba"), "name" : "test262", "age" : 22, "score" : 40 }
98
{ "_id" : ObjectId("594c855372bb243c2b6857bb"), "name" : "test263", "age" : 23, "score" : 41 }
99
{ "_id" : ObjectId("594c855472bb243c2b685832"), "name" : "test382", "age" : 22, "score" : 160 }
100
{ "_id" : ObjectId("594c855472bb243c2b685833"), "name" : "test383", "age" : 23, "score" : 161 }
101
{ "_id" : ObjectId("594c855472bb243c2b6858aa"), "name" : "test502", "age" : 22, "score" : 58 }
102
{ "_id" : ObjectId("594c855472bb243c2b6858ab"), "name" : "test503", "age" : 23, "score" : 59 }
103
{ "_id" : ObjectId("594c855472bb243c2b685922"), "name" : "test622", "age" : 22, "score" : 178 }
104
{ "_id" : ObjectId("594c855472bb243c2b685923"), "name" : "test623", "age" : 23, "score" : 179 }
105
{ "_id" : ObjectId("594c855472bb243c2b68599a"), "name" : "test742", "age" : 22, "score" : 76 }
106
{ "_id" : ObjectId("594c855472bb243c2b68599b"), "name" : "test743", "age" : 23, "score" : 77 }
107
{ "_id" : ObjectId("594c855472bb243c2b685a12"), "name" : "test862", "age" : 22, "score" : 196 }
108
{ "_id" : ObjectId("594c855472bb243c2b685a13"), "name" : "test863", "age" : 23, "score" : 197 }
109
{ "_id" : ObjectId("594c855472bb243c2b685a8a"), "name" : "test982", "age" : 22, "score" : 94 }
110
{ "_id" : ObjectId("594c855472bb243c2b685a8b"), "name" : "test983", "age" : 23, "score" : 95 }
111
{ "_id" : ObjectId("594c855472bb243c2b685b02"), "name" : "test1102", "age" : 22, "score" : 214 }
112
{ "_id" : ObjectId("594c855472bb243c2b685b03"), "name" : "test1103", "age" : 23, "score" : 215 }
113
114
$in
115
$nin
组合操作
1
$or:或运算,语法格式
2
$and:与运算
3
$not:非运算
4
$nor:反运算,返回不符合指定条件的所有文档
5
6
> db.test.find({$and:[{score:{$gt:100}},{score:{$lt:200}}]})
7
{ "_id" : ObjectId("594c855372bb243c2b685719"), "name" : "test101", "age" : 101, "score" : 101 }
8
{ "_id" : ObjectId("594c855372bb243c2b68571a"), "name" : "test102", "age" : 102, "score" : 102 }
9
{ "_id" : ObjectId("594c855372bb243c2b68571b"), "name" : "test103", "age" : 103, "score" : 103 }
10
{ "_id" : ObjectId("594c855372bb243c2b68571c"), "name" : "test104", "age" : 104, "score" : 104 }
11
{ "_id" : ObjectId("594c855372bb243c2b68571d"), "name" : "test105", "age" : 105, "score" : 105 }
12
{ "_id" : ObjectId("594c855372bb243c2b68571e"), "name" : "test106", "age" : 106, "score" : 106 }
13
{ "_id" : ObjectId("594c855372bb243c2b68571f"), "name" : "test107", "age" : 107, "score" : 107 }
14
{ "_id" : ObjectId("594c855372bb243c2b685720"), "name" : "test108", "age" : 108, "score" : 108 }
15
{ "_id" : ObjectId("594c855372bb243c2b685721"), "name" : "test109", "age" : 109, "score" : 109 }
16
{ "_id" : ObjectId("594c855372bb243c2b685722"), "name" : "test110", "age" : 110, "score" : 110 }
17
{ "_id" : ObjectId("594c855372bb243c2b685723"), "name" : "test111", "age" : 111, "score" : 111 }
18
{ "_id" : ObjectId("594c855372bb243c2b685724"), "name" : "test112", "age" : 112, "score" : 112 }
19
{ "_id" : ObjectId("594c855372bb243c2b685725"), "name" : "test113", "age" : 113, "score" : 113 }
20
{ "_id" : ObjectId("594c855372bb243c2b685726"), "name" : "test114", "age" : 114, "score" : 114 }
21
{ "_id" : ObjectId("594c855372bb243c2b685727"), "name" : "test115", "age" : 115, "score" : 115 }
22
{ "_id" : ObjectId("594c855372bb243c2b685728"), "name" : "test116", "age" : 116, "score" : 116 }
23
{ "_id" : ObjectId("594c855372bb243c2b685729"), "name" : "test117", "age" : 117, "score" : 117 }
24
{ "_id" : ObjectId("594c855372bb243c2b68572a"), "name" : "test118", "age" : 118, "score" : 118 }
25
{ "_id" : ObjectId("594c855372bb243c2b68572b"), "name" : "test119", "age" : 119, "score" : 119 }
26
{ "_id" : ObjectId("594c855372bb243c2b68572c"), "name" : "test120", "age" : 0, "score" : 120 }
27
Type "it" for more
元素操作
1
$exists:语法格式{$filed:{$exists:<boolean>}}
2
$mod
3
$type:返回指定字段的值的类型为指定类型的文档,语法格式为{filed:{$type:<BSON type>}}
4
Double,String,Object,Array,Binary data,Undefined,Boolean,Date,Null,Regular Expression,JavaScript,Timestamp
5
6
> db.test.insert({name:"test22222",class:"class3"}
7
... )
8
WriteResult({ "nInserted" : 1 })
9
> db.test.find({class:{$exists:true}})
10
{ "_id" : ObjectId("594c8c2072bb243c2b687dc5"), "name" : "test22222", "class" : "class3" }
更新操作
1
> db.test.update({class:"class3"},{$set:{class:"class55"}})
2
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
3
> db.test.find({class:{$exists:true}})
4
{ "_id" : ObjectId("594c8c2072bb243c2b> db.test.find({class:{$exists:true}})
5
6
7
> db.test.find({class:{$exists:true}})
8
{ "_id" : ObjectId("594c8c2072bb243c2b687dc5"), "name" : "test22222", "class" : "class55" }
9
> db.test.update({class:"class55"},{$unset:{class:true}})
10
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
11
> db.test.find({class:{$exists:true}})
12
13
> db.test.update({name:"test22222"},{$rename:{class:"classes"}})
14
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
15
> db.test.find({name:"test22222"})
16
{ "_id" : ObjectId("594c8c2072bb243c2b687dc5"), "name" : "test22222", "classes" : 1234 }
17
18
> db.test.update({name:"test22222"},{$inc:{class:1234}})
19
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
20
> db.test.find({name:"test22222"})
21
{ "_id" : ObjectId("594c8c2072bb243c2b687dc5"), "name" : "test22222", "classes" : 1234, "class" : 1234 }
删除操作:
1 | 删除记录 |
2 | db.test.remove({name:"test22222"},1) |
3 | WriteResult({ "nRemoved" : 1 }) |
4 | |
5 | 删除collection: |
6 | db.test.drop() |
7 | |
8 | 删除数据库: |
9 | db.dropDatabase() |
创建索引
1
db.mycoll.ensureIndex({name:1},{background:true}) 后台创建索引
2
db.mycoll.ensureIndex({name:1},{unique:true,dropDups:true}) 唯一索引,去掉重复的
3
db.mycoll.dropIndex(index_name)
4
db.mycoll.dropIndexes()
5
db.mycoll.getIndexed()
6
db.mycoll.reIndex()
启动与终止
1、正常启动
mongod –dbpath /usr/mongo/data –logfile /var/mongo.log
说明:
指定数据存储目录和日志目录,如果采用安全认证模式,需要加上–auth选项,如:
mongod –auth –dbpath /usr/mongo/data –logfile /var/mongo.log
2、以修复模式启动
mongod –repair
以修复模式启动数据库。
实际很可能数据库数据损坏或数据状态不一致,导致无法正常启动MongoDB服务器,根据启动信息可以看到需要进行修复。或者执行:
mongod -f /etc/mongodb.conf –repair
3、终止服务器进程
db.shutdownServer()
终止数据库服务器进程。或者,可以直接kill掉mongod进程即可。
- 安全管理
1、以安全认证模式启动
mongod –auth –dbpath /usr/mongo/data –logfile /var/mongo.log
使用–auth选项启动mongod进程即可启用认证模式。
或者,也可以修改/etc/mongodb.conf,设置auth=true,重启mongod进程。
2、添加用户
1 | use admin |
2 | db.createUser( |
3 | { |
4 | user: "myUserAdmin", |
5 | pwd: "abc123", |
6 | roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] |
7 | } |
8 | ) |
9 | |
10 | |
11 | use test |
12 | db.createUser( |
13 | { |
14 | user: "myTester", |
15 | pwd: "xyz123", |
16 | roles: [ { role: "readWrite", db: "test" }, |
17 | { role: "read", db: "reporting" } ] |
18 | } |
19 | ) |
3、认证
1 | mongo --port 27017 -u "myTester" -p "xyz123" --authenticationDatabase "test" |
2 | |
3 | use test |
4 | db.auth("myTester", "xyz123" ) |
为数据库写数据(同步到磁盘)加锁
该操作已经对数据库上锁,不允许执行写数据操作,一般在执行数据库备份时有用。1
> use admin
2
switched to db admin
3
> db.runCommand({fsync:1,lock:1})
4
{
5
"info" : "now locked against writes, use db.fsyncUnlock() to unlock",
6
"lockCount" : NumberLong(1),
7
"seeAlso" : "http://dochub.mongodb.org/core/fsynccommand",
8
"ok" : 1
9
}
10
11
查看当前锁状态:
12
> db.currentOp()
13
...
14
15
"fsyncLock" : true,
16
"info" : "use db.fsyncUnlock() to terminate the fsync write/snapshot lock",
17
"ok" : 1
18
...
19
20
解锁
21
> db.fsyncUnlock()
22
{ "info" : "fsyncUnlock completed", "lockCount" : NumberLong(0), "ok" : 1 }
基于mongo实现管理
1 | mongo -u admin -p admin 192.168.0.197:27017/pagedb |