文档库 最新最全的文档下载
当前位置:文档库 › mongodb 常用命令总结转载

mongodb 常用命令总结转载

以下说的表就是mongodb 中的collection
查询语句:
db.exam.remove({"_id":{$exists:true}}) 清空exam 表中的所有数据
db.exam.find() ; 查找exam表中的20条记录
db.exam.find().limit(10) 查找exam表中的10条记录
db.exam.drop() 删除此表
db.exam.remove({}) 清空exam 表中的所有数据
db.createCollection("event") 建立 event 表
维护语句:
use admin
1. 配置mongodb 集群中一个Replica Sets模式的shard,并初始化
config = {_id: 'shard4', members: [
{_id: 0, host: '10.181.49.224:27001'},
{_id: 1, host: '10.181.49.221:27002'},
{_id: 2, host: '10.181.49.222:27003'}]
}
rs.initiate(config)
2. 将一个shard 加入到集群中
db.runCommand({addshard:"shard4/10.181.49.224:27001,10.181.49.221:27002,10.181.49.224:27002",name:"shard2"})
3. 查看shard 信息
db.runCommand({listshards:1})
4. 将一个库使用shard
db.runCommand({ enablesharding:"okooo" })
5. 查看replica set 的状态
rs.status()
state 字段说明:
0 Starting up, phase 1
1 Primary
2 Secondary
3 Recovering
4 Fatal error
5 Starting up, phase 2
6 Unknown state
7 Arbiter
8 Down
health 字段说明:
0 Server is down
1 Server is up
6. 查看replica set 中是否是主节点
rs.isMaster()
7.查看当前数据库的数据同步状态
db.printReplicationInfo()
configured oplog size: 1024MB --oplog 大小
log length start to end: 210789secs (58.55hrs) --oplog的时间段
oplog first event time: Wed Oct 19 2011 04:39:59 GMT+0800 (CST) --第一个事务的时间
oplog last event time: Fri Oct 21 2011 15:13:08 GMT+0800 (CST) --最后一个事务的时间
now: Fri Oct 21 2011 15:13:08 GMT+0800 (CST) --现在的时间
8. 查看整个shard的同步状态
db.printSlaveReplicationInfo()
source: 192.168.8.2:27002 --目标服务器
syncedTo: Sat Oct 15 2011 18:18:53 GMT+0800 (CST) --延时主机情况
= 507496secs ago (140.97hrs)
source: 192.168.8.3:27003 --目标服务器
syncedTo: Fri Oct 21 2011 15:17:09 GMT+0800 (CST) --延时主机情况
= 0secs ago (0hrs)
9. 当前运行的进程
db.currentOp();
10. 杀掉可疑进程
db.killOp(opid)
11. 看数据库信息
db.stats()
12. 看数据实例信息
db.serverStatus()
13. (在主库上执行)增加一个从库
rs.add("192.168.8.226:27004")
14. (在主库上执行)减少一个从库
rs.remove("192.168.8.226:27004")
15. 看表的

状态
https://www.wendangku.net/doc/6f11310892.html,ers.stats()
16. 看shard 状态
printShardingStatus() db.printShardingStatus( true )
17. 对已有的表进行shard
db.runCommand({ shardcollection: "https://www.wendangku.net/doc/6f11310892.html,ers_2", key: { _id:1 }}) --users_2表根据_id 键分区
18. 移除一个shard
db.runCommand({"removeshard" : "localhost:20002"});
19. 从库支持查询
db.getMongo().setSlaveOk()
20. 查看慢查询
db.system.profile.find()
21. 打开profile
打开profile有两种
一种直接在启动参数里直接设置,启动 MongoDB时加上 --profile=级别即可。
另一种就是客服端调用后db.setProfilingLevel(级别,秒数) ,但是这只影响本次会话。
22. db.adminCommand({"enableSharding" : "blog"}) 在blog 库上建立sharding
23. db.adminCommand({"shardCollection" : "blog.posts", key : {"date" : 1, "author" : 1}} 在blog库上的posts表上利用date和author两个字段建立sharding

相关文档