MongoDB——命令备份

命令: 

查询指定字段         db.basePlayer.find({}, {_id:1}).pretty()

删除指定 id 的记录  db.basePlayer.remove({"_id": "436149874459672577_1051"})

查询出一条记录    db.chunks.find().limit(1).pretty()

授权secondary节点可读   rs.secondaryOk()

查看mongodb占用内存    db.serverStatus().wiredTiger.cache         找到这一行 "maximum bytes configured" : 3564109824, 即3.5G

查看存储引擎                 db.serverStatus().storageEngine

查看副本集状态              rs.status()

集群成员状态                 db.stats()

查询chunks                   db.chunks.find({"ns":{$ne:"config.system.sessions"}},{})

逆序查询首条记录           db.changelog.find().sort({"time":-1}).limit(1)

id逆序查询前三条记录     db.chunks.find().sort({"_id":-1}).limit(3).pretty()

查看集合所占空间(byte)  db.things.dataSize()

查看集合统计信息          db.things.stats()

集合在各分片分存情况    db.userModel.getShardDistribution()

在config查看块大小        db.settings.find()

在config设置块大小        db.settings.save( { _id:"chunksize", value: 8 } )       //设置为8M,块大小默认为64M

查看副本集状态              mongo 127.0.0.1:27019 -eval "rs.status()" 

查看集合元数据              mongo 192.168.1.22:27016/admin -eval "db.printCollectionStats()" | grep "ns\""

查看当前库集合列表        mongo 192.168.1.22:27016/admin -eval "db.getCollectionNames()"

查看数据库列表              mongo 192.168.1.22:27016/admin -eval "db.runCommand({listDatabases:1})"

查看数据库列表              mongo 192.168.1.52:27017 -eval "db.adminCommand({listDatabases:1})"

 

 从docker查看mongo相关命令

docker exec -it config0 mongo 127.0.0.1:27019 -eval "rs.status();"

docker exec -it mongos mongo 192.168.1.22:27017/admin -eval "db.serverStatus().tcmalloc"

docker exec -it mongoshard1 mongo 192.168.1.21:26002/test -eval "db.serverStatus().wiredTiger.cache" | grep max

docker exec -it mongoshard1 mongo 192.168.1.21:26002/test -eval "rs.status().members"

 

把集合拷贝到另一集合

mongo 47.14.21.16/melon_log -eval "db.log_live.insertOne({});"
mongo 47.14.21.16/melon_log -eval "db.getCollectionNames()"
mongo 47.14.21.16/melon_log -eval "db.log_live.find().pretty()"
mongo 47.14.21.16/melon_log -eval "db.log_live_like.aggregate([ { $match: {} }, { $out:'log_live'} ]);   "
mongo 47.14.21.16/melon_log -eval "db.log_live.count()"
mongo 47.14.21.16/melon_log -eval "db.log_live.find().pretty()"
mongo 47.14.21.16/melon_log -eval "db.log_live.count()"

 

 

 

Memory Use

With WiredTiger, MongoDB utilizes both the WiredTiger internal cache and the filesystem cache.

Starting in 3.4, the WiredTiger internal cache, by default, will use the larger of either:

  • 50% of (RAM - 1 GB), or
  • 256 MB.

To adjust the size of the WiredTiger internal cache, see

storage.wiredTiger.engineConfig.cacheSizeGB       in config file

--wiredTigerCacheSizeGB                                           in command

Avoid increasing the WiredTiger internal cache size above its default value.

 

查看mongod -h发现mongod提供了额外的可选参数来控制WiredTiger存储引擎所占用的cache size。需要注意的是,cache size设置较低,同时mongodb复杂查询很频繁的话,会有延迟发生。

cacheSizeGB 指的就是Cache size,包括数据和索引。Mongod本身使用内存如连接池堆栈以及sorting buffer等都是额外的,不会被统计到这个数字里面。如果索引在内存,查询冷数据取决于你的IO能力。如果IO latency很低,系统也没有是高负载,那响应时间应该是毫秒级的区别。但是如果查询很频繁,又涉及到很多范围、批量查询,IOPS又跟不上,那就有可能到几百几千毫秒都有可能。

配置:

#设置最大占用内存

如下配置文件仅对 wiredTiger 引擎生效(3.0 以上版本) 

storage:
  dbPath: /data/mongodb/db
  journal:
    enabled: true
  engine: wiredTiger
  wiredTiger:
     engineConfig:
          cacheSizeGB: 5

配置mongoDB限制使用最大内存 命令行

启动参数:./mongod -f config.cnf  --storageEngine wiredTiger --wiredTigerEngineConfigString="cache_size=300M"

shell查看是否生效: db.serverStatus().wiredTiger.cache 

 

docker方式启动一个片,指定内存占用,容器和mongo都指定, 60% docker内容为宜

表示此容器最大占1G内存,禁用swap,数据和索引cache占0.6G内存

docker run -d --name mongoshard2 \
              -p 26003:27018  \
              -v /root/data/soft/mongo/shard2db:/data/db \
              -m 1G --memory-swap 1G \
              mongo:4.0.24 \
              --shardsvr \
              --replSet "replshard2" \
              --bind_ip_all \
              --storageEngine wiredTiger \
              --wiredTigerCacheSizeGB 0.6

 

 

 

单实例mongodb.conf

dbpath=/root/data/soft/mongodb4.4/mdb4.4/db/ 

logpath=/root/data/soft/mongodb4.4/mdb4.4/log/mongodb.log  

pidfilepath=/root/data/soft/mongodb4.4/mdb4.4/m.pid

logappend=true  

port=26016  

fork=true  

maxConns = 1000

auth=false 

 

mongo分片集群环境下查询语句

docker exec -it mongo27017 mongo 192.168.0.54/dragon_1283 -eval "db.getCollectionNames()"

docker exec -it mongo27017 mongo 192.168.0.54/dragon_1283 -eval "db.basePlayer.find({'_id':'4398046511710_1283'})"

mongo 192.168.1.22:27017/dragon_1251 -eval "db.getCollectionNames()" 

mongo 192.168.1.22:27017/dragon_1251 -eval "db.basePlayer.find({'_id':'4398046511710_1251'}, {'_id':1, 'nickname':1, 'level':1})" 

docker exec -it mongo27017  mongodump -h 192.168.0.54 -d dragon_1283 -c basePlayer  -o /data/backup/

docker cp mongo27017:/data/backup/dragon_1283 /root/data/acgn/backup/

docker exec -it mongo27017  mongodump -h 192.168.0.54 -d dragon_1283 -c basePlayer -q '{"_id":"4398046511710_1283"}' -o /data/backup/

 

mac下mongodb环境变量添加:

export MONGODB=/Users/admin/Documents/soft/mongodb/mongodb-osx-x86_64-3.4.24/bin
export PATH=$MONGODB:$PATH

 

备份与恢复

mongodump    --host 4.14.21.19   --port 27017  --db melon_log --out E:\code\douyin\mongo\
mongorestore --host 192.168.1.16 --port 27017 --db melon_log_restored E:\code\douyin\mongo\melon_log

 

mongodb --version

mongod --help

mongod --dbpath w:\soft\mongodb\mdb4.2.6

mongod --dbpath w:\soft\mongodb\mdb3.4.24

mongo 192.168.1.16:27017 -eval "db.adminCommand({'listDatabases':1})"

 

mongodump -h 127.0.0.1 -d loginserver -o D:\soft\mongodb\db-backup\

mongodump -h 127.0.0.1 -d loginsaveserver -o D:\soft\mongodb\db-backup\

 

mongoexport -h 127.0.0.1 -d loginserver -c users --type=json -o D:\soft\mongodb\db-backup\users.json

mongoexport -h 127.0.0.1 -d loginsaveserver -c 2020-5-16-10168 --type=json -o D:\soft\mongodb\db-backup\2020-5-16-10168.json

 

 

posted @ 2020-11-05 10:35  会飞的斧头  阅读(260)  评论(0编辑  收藏  举报