mongo备份与恢复
# 查看mongodump与mongo版本是否一致
mongodump --version
# 版本不一致时
whereis mongodump
/usr/local/bin/mongodump
# 删除mongodump
rm -rf /usr/local/bin/mongodump
docker cp mongo_dockername:/usr/bin/mongodump /usr/local/bin
备份
# 备份指定服务器上到指定数据库到指定路径
> mongodump -h dbhost -d dbname -o dbdirectory
# 备份整个数据库
> mongodump
使用docker启动mongo时,当mongodump版本与docker镜像中当版本不一致时,需要更新当前环境中当版本 方法如下(mongorestore也一样)
恢复
# 恢复数据库
> mongorestore -h <hostname><:port> -d dbname <path>
数据导入导出
导入
mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv -f field
参数说明:
-d :数据库名
-c :collection名
--type :导入的格式默认json
-f :导入的字段名
--headerline :如果导入的格式是csv,则可以使用第一行的标题作为导入的字段
--file :要导入的文件
导出
mongoexport -d dbname -c collectionname -o file --type json/csv -f field
参数说明:
-d :数据库名
-c :collection名
-o :输出的文件名
--type : 输出的格式,默认为json
-f :输出的字段,如果-type为csv,则需要加上-f "字段名"
数据库更新
语法
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)
参数说明
- query : update的查询条件,类似sql update查询内where后面的。
- update : update的对象和一些更新的操作符(如 , , ,inc…)等,也可以理解为sql update查询内set后面的
- upsert : 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
- multi : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
- writeConcern :可选,抛出异常的级别。
示例
db.col.update({‘title’:‘MongoDB 教程’},{$set:{‘title’:‘MongoDB’}},{multi:true})
更新数组中的某个数据
db.dbName.update(
{"items.itemValue":"null"},
{$set:{'items.2.itemValue':'我被更新了'}},
{multi:false}
)
// 以上语句只会修改第一条发现的文档,如果你要修改多条相同的文档,则需要设置 multi 参数为 true。
items.2.itemValue 意思是更新items数组中的第三个元素(起始坐标为0)下的itemValue
数据库分片
查看分片
mongos> sh.status()
或
mongos> db.printShardingStatus()
mongo内存
查看内存
#
rs1:SECONDARY> db.serverStatus().mem
{
"bits" : 64, //操作系统位数
"resident" : 6750, //物理内存消耗,单位M
"virtual" : 7663, //虚拟内存消耗
"supported" : true, //为true表示支持显示额外的内存信息
"mapped" : 0, //映射内存
"mappedWithJournal" : 0
}
查看数据库存储引擎内存
# 3.4版本以后默认的数据库存储引擎是wiredTiger 查看当前占用的内存
rs1:SECONDARY> db.serverStatus().wiredTiger.cache
{
"application threads page read from disk to cache count" : 47322,
"application threads page read from disk to cache time (usecs)" : 45236008,
"application threads page write from cache to disk count" : 10,
"application threads page write from cache to disk time (usecs)" : 121,
"bytes belonging to page images in the cache" : 726601452,
"bytes currently in the cache" : 6833259939, // 当前占用的内存
"bytes not belonging to page images in the cache" : 6106658487,
"bytes read into cache" : 749959252,
"bytes written from cache" : 18975552133,
"checkpoint blocked page eviction" : 0,
"eviction calls to get a page" : 41605,
"eviction calls to get a page found queue empty" : 40147,
"eviction calls to get a page found queue empty after locking" : 430,
"eviction currently operating in aggressive mode" : 0,
"eviction empty score" : 0,
"eviction server candidate queue empty when topping up" : 7,
"eviction server candidate queue not empty when topping up" : 3,
"eviction server evicting pages" : 22,
"eviction server slept, because we did not make progress with eviction" : 866,
"eviction server unable to reach eviction goal" : 0,
"eviction state" : 16,
"eviction walks abandoned" : 17,
"eviction worker thread active" : 4,
"eviction worker thread created" : 0,
"eviction worker thread evicting pages" : 565,
"eviction worker thread removed" : 3136,
"eviction worker thread stable number" : 0,
"failed eviction of pages that exceeded the in-memory maximum" : 1421,
"files with active eviction walks" : 0,
"files with new eviction walks started" : 26,
"force re-tuning of eviction workers once in a while" : 788,
"hazard pointer blocked page eviction" : 156,
"hazard pointer check calls" : 5226,
"hazard pointer check entries walked" : 20859,
"hazard pointer maximum array length" : 2,
"in-memory page passed criteria to be split" : 1286,
"in-memory page splits" : 609,
"internal pages evicted" : 1400,
"internal pages split during eviction" : 0,
"leaf pages split during eviction" : 59,
"lookaside table insert calls" : 0,
"lookaside table remove calls" : 0,
"maximum bytes configured" : 8589934592, // 限制的最大存储引擎内存
"maximum page size at eviction" : 0,
"modified pages evicted" : 569,
"modified pages evicted by application threads" : 0,
"overflow pages read into cache" : 0,
"overflow values cached in memory" : 0,
"page split during eviction deepened the tree" : 0,
"page written requiring lookaside records" : 0,
"pages currently held in the cache" : 48652,
"pages evicted because they exceeded the in-memory maximum" : 610,
"pages evicted because they had chains of deleted items" : 2206,
"pages evicted by application threads" : 0,
"pages queued for eviction" : 1000,
"pages queued for urgent eviction" : 22,
"pages queued for urgent eviction during walk" : 22,
"pages read into cache" : 50727,
"pages read into cache requiring lookaside entries" : 0,
"pages requested from the cache" : 87877863,
"pages seen by eviction walk" : 1175,
"pages selected for eviction unable to be evicted" : 1633,
"pages walked for eviction" : 13448,
"pages written from cache" : 1304368,
"pages written requiring in-memory restoration" : 1,
"percentage overhead" : 8,
"tracked bytes belonging to internal pages in the cache" : 26468698,
"tracked bytes belonging to leaf pages in the cache" : 6806791241,
"tracked dirty bytes in the cache" : 216731145,
"tracked dirty pages in the cache" : 1589,
"unmodified pages evicted" : 2415
}