MongoDBは、オープンソースソフトウェアのドキュメント指向データベースである。C++言語で記述されており、開発とサポートはMongoDB Inc.によって行なわれている。
db.adminCommand({getParameter: 1, featureCompatibilityVersion: 1}) { featureCompatibilityVersion: { version: 'x.x' }, ...
db.system.version.find() { "_id" : "featureCompatibilityVersion", "version" : "x.x" }
mongo use admin db.system.version.update({'_id':'featureCompatibilityVersion'},{$set:{'version':"3.4"}}) # confirm db.system.version.find() { "_id" : "featureCompatibilityVersion", "version" : "3.4" }
db.system.profile.drop()
version:
問題:
未解決:
回避策:
if [ -d ~/.mongodb/mongosh ]; then find ~/.mongodb/mongosh/ -name "*_log" -type f -mtime +1 -delete fi
出典:
MongoDB can saturate all cores on a multi-core machine for read operations, but for write operations and map-reduce MongoDB can only utilize a single core per mongod process.
mongo --quiet --eval 'db.getCollectionNames().forEach(function(c) { if (c.indexOf("c_test") == 0) db[c].drop(); })' test
mongo --quiet --eval 'db.getCollectionNames().forEach(function(c) { if (c.indexOf("system.") == -1) db[c].drop(); })' test
db.collection.find()
db.collection.find().forEach(printjson)
db.collection.find().toArray()
JSON.stringify(db.collection.find().toArray())
rs0:PRIMARY> rs.stepDown() { "ok" : 0, "errmsg" : "No electable secondaries caught up as of 2021-01-02T03:04:00.000+0000", "code" : 50 }
# force: falseで試す db.adminCommand( { replSetStepDown: 120, secondaryCatchUpPeriodSecs: 15, force: false } ) # force: trueで試す。rs.status()で確認すると、成功はするがSECONDARYが2つになっていた db.adminCommand( { replSetStepDown: 120, secondaryCatchUpPeriodSecs: 15, force: true } )
sudo service mongod restart
mongostat -n 1 insert query update delete getmore command flushes vsize res qr|qw ar|aw netIn netOut conn set repl time *0 *0 *0 *0 0 1|0 0 2.1G 1.3G 0|0 0|0 79b 13k 7 rs01 SEC 07:04:46 # awkでconnを抽出 mongostat -n 1 --noheaders | awk '{print $14;}' 7 # jsonで出力 mongostat -n 1 --json {"host1":{"command":"10|0","conn":"26","delete":"*0","getmore":"8","host":"host1","insert":"*0","netIn":"11k","netOut":"24k","query":"4","repl":"PRI","res":"1.5G","set":"rs01","time":"03:54:30","update":"19","vsize":"3.9G"}} # jqで.connだけ出力 mongostat -n 1 --json | jq -r '.[].conn'
mongo --quiet --eval 'JSON.stringify(db.serverStatus())' mongo --quiet --eval 'JSON.stringify(db.serverStatus().network)' {"bytesIn":{"floatApprox":382313310},"bytesOut":{"floatApprox":834527308},"numRequests":{"floatApprox":2539018}} mongo --quiet --eval 'JSON.stringify(db.serverStatus().network.bytesIn.floatApprox)' 382279847
mongo --quiet --eval 'printjson(db.serverStatus())' { "host" : "host1", "version" : "3.0.x-1.4", "process" : "mongod", ... "connections" : { "current" : 7, "available" : 51174, "totalCreated" : NumberLong(3304436) }, ... # .networkの例 mongo --quiet --eval 'printjson(db.serverStatus().network)' { "bytesIn" : NumberLong(382298771), "bytesOut" : NumberLong(834471201), "numRequests" : NumberLong(2538920) } mongo --quiet --eval 'printjson(db.serverStatus().network.bytesIn)' NumberLong(382330737) mongo --quiet --eval 'printjson(db.serverStatus().network.bytesIn.floatApprox)' 382330737 # connections.current だけ抽出 mongo --quiet --eval 'printjson(db.serverStatus().connections.current)' # mem.resident だけ抽出 mongo --quiet --eval 'printjson(db.serverStatus().mem.resident)'
mongo --quiet --eval "printjson(db.isMaster().ismaster)" true mongo --quiet --eval "printjson(db.isMaster().secondary)" false # myState: 1=PRIMARY, 2=SECONDARY, 7=ARBITER mongo --quiet --eval "printjson(rs.status().myState)" 1
mongo --quiet --eval "printjson(db.serverStatus())"
mongo --quiet --eval "printjson(db.adminCommand( { getParameter : '*' } ))"
エンジンによって設定が異なる。出来ない事もあるようだ。
現在のメモリ使用量:
mongo --quiet --eval "printjson(db.serverStatus().mem)"
wiredTiger
PERCONA rocksdb:
--rocksdb.max-total-wal-size 1024000
--rocksdb.write-buffer-size 2048000
--rocksdb.max-write-buffer-number 2
--rocksdb.total-write-buffer-size 81920000
--rocksdb.dynamic-level-bytes false
mongodbからコレクションを消しても、OSから見た空きディスク容量は増えない。
mongo --quiet --eval "printjson(db.runCommand( { compact: '<collection name>'} ))" <db name>
{ "ok" : 0, "errmsg" : "cannot compact a capped collection" }
MONGODB_PATH=/var/lib/mongo mongo --quiet --eval 'printjson(rs.stepDown())' # "stateStr" : "SECONDARY" に変わるまで待つ mongo --quiet --eval 'printjson(rs.status())' # monitを使っている場合は無効に sudo monit unmonitor mongod sudo service mongod stop # 既存データをバックアップ。ただし、再同期に溢れる場合もあるので、空き容量が使用量の2倍以下の場合、消した方が良い事も。 sudo mv $MONGODB_PATH ${MONGODB_PATH}.$(date +%Y%m%d) sudo mkdir $MONGODB_PATH sudo chown mongod:mongod $MONGODB_PATH sudo service mongod start # monitを使っている場合は有効に sudo monit monitor mongod # 使用しているDB名でファイルができている事を確認 ls -lh $MONGODB_PATH # "stateStr" : "SECONDARY" に変わるまで待つ。データ量によっては〜数時間かかる。 mongo --quiet --eval 'printjson(rs.status())' # 古いDBファイルを削除 sudo rm -rf /var/lib/mongo.YYYYMMDD
# memだけ mongo --quiet --eval 'printjson(db.serverStatus().mem)' # connectionsだけ mongo --quiet --eval "printjson(db.serverStatus().connections)"
sudo service mongod stop sudo mongodump -v --dbpath /var/lib/mongo --out ./mongo_dump
sudo /var/lib/mongo /var/lib/mongo.$(date +%Y%m%d) sudo mkdir -p /var/lib/mongo sudo chown mongod:mongod /var/lib/mongo sudo service mongod start mongorestore -v ./mongo_dump
mongo sample --quiet --eval 'printjson(db.createCollection("test01", { capped: true, size: 8*1024*1024}))'
mongo --quiet --eval 'db.getCollectionNames().forEach(function(n){print(n + "," + db[n].stats().capped)})' sample | grep -v -E '^system|,true$' > ./mongo.capped.txt
for app_id in $(cut -d, -f 1 mongo.capped.txt); do \ echo -n "$app_id," >> ./mongo.size.txt; \ mongo --quiet --eval "printjson(db.${app_id}.stats(1024*1024).size)" sample >> ./mongo.size.txt; \ done grep -v ',0$' mongo.size.txt
mongo --quiet --eval 'printjson(db.runCommand({ convertToCapped: "test01", size: 8*1024*1024}))' sample
for app_id in $(cut -d, -f 1 mongo.capped.txt); do \ echo -n "$app_id,"; \ mongo --quiet --eval "printjson(db.runCommand({ convertToCapped: '${app_id}', size: 8*1024*1024}))" sample done
mongo --quiet --eval 'printjson(db.test01.stats(1024*1024))' sample
mongo --quiet --eval 'db.getCollectionNames().forEach(function(n){print(n + "," + db[n].stats().capped)})' sample| grep -v -E '^system|,true$'
データが多くなってくるとでデフォルトの90秒では起動しない事がある。
sudo vim /etc/systemd/system/mongod.service.d/timeout.conf -- [Service] TimeoutStartSec=3m -- sudo systemctl daemon-reload systemctl show mongod -p TimeoutStartUSec TimeoutStartUSec=3min
date" : ISODate("2020-05-01T01:03:04.317Z")
mongo --quiet --eval 'printjson(db.serverStatus())'
mongo --quiet --eval 'printjson(rs.status())'
mongo --quiet --eval 'printjson(db.runCommand( { "connPoolStats" : 1 } ))'
mongo --quiet --host localhost --eval 'db.col.count()' sample
mongo --quiet --eval "printjson(db.getMongo().getDBNames().length)"
mongo
show dbs
use <db name>
show collections
db.test01.stats(1024*1024)
db.getCollectionNames().forEach(function(n){print(n + "," + db[n].stats().storageSize/1024/1024 + " MB")})
db.getCollectionNames().forEach(function(n){print(n + "," + db[n].count())})
db.getCollectionNames().forEach(function(n){print(n); printjson(db[n].getIndexes())});
sudo cat /proc/$(pidof mongod)/limits | grep -P "open|proc" Max processes 1024 64000 processes Max open files 1024 4096 files
sudo mkdir /etc/systemd/system/mongod.service.d sudo vim /etc/systemd/system/mongod.service.d/limits.conf -- # https://docs.mongodb.com/manual/reference/ulimit/ # # The settings is what mongoDB officially recommends. # [Service] # Other directives omitted # (file size) LimitFSIZE=infinity # (cpu time) LimitCPU=infinity # (virtual memory size) LimitAS=infinity # (open files) LimitNOFILE=64000 # (processes/threads) LimitNPROC=64000 -- sudo systemctl daemon-reload sudo service mongod restart
sudo vim /etc/security/limits.d/99-mongodb-nproc.conf -- # http://docs.mongodb.org/manual/reference/ulimit/ mongod soft nproc 64000 mongod soft nofile 64000 -- sudo service mongod restart