cgroup限制mongodb进程内存大小
以限制mongodb的内存大小为例。
mkdir/cgroup/memory/test/ echo50M>/cgroup/memory/test/memory.limit_in_bytes echo50M>/cgroup/memory/test/memory.memsw.limit_in_bytes cgexec-gmemory:testmongod-port27017--bind_ip127.0.0.1--dbpath/var/lib/mongo
通过cgroup限制后,当内存达到限额,进程会被kill。
[root@centosmongo]#cgexec-gmemory:testmongod-port27017--bind_ip127.0.0.1--dbpath/var/lib/mongo 2014-07-18T23:20:53.228+0800[initandlisten]MongoDBstarting:pid=2529port=27017dbpath=/var/lib/mongo64-bithost=centos 2014-07-18T23:20:53.228+0800[initandlisten]dbversionv2.6.3 2014-07-18T23:20:53.228+0800[initandlisten]gitversion:255f67a66f9603c59380b2a389e386910bbb52cb 2014-07-18T23:20:53.228+0800[initandlisten]buildinfo:Linuxbuild12.nj1.10gen.cc2.6.32-431.3.1.el6.x86_64#1SMPFriJan321:39:27UTC2014x86_64BOOST_LIB_VERSION=1_49 2014-07-18T23:20:53.228+0800[initandlisten]allocator:tcmalloc 2014-07-18T23:20:53.228+0800[initandlisten]options:{net:{bindIp:"127.0.0.1",port:27017},storage:{dbPath:"/var/lib/mongo"}} 2014-07-18T23:20:53.304+0800[initandlisten]journaldir=/var/lib/mongo/journal 2014-07-18T23:20:53.304+0800[initandlisten]recover:nojournalfilespresent,norecoveryneeded 2014-07-18T23:20:53.374+0800[initandlisten]waitingforconnectionsonport27017 2014-07-18T23:20:57.838+0800[initandlisten]connectionacceptedfrom127.0.0.1:36712#1(1connectionnowopen) 2014-07-18T23:21:15.077+0800[initandlisten]connectionacceptedfrom127.0.0.1:36713#2(2connectionsnowopen) 2014-07-18T23:21:52.342+0800[conn2]getmoretest.my_collectioncursorid:34538199491ntoreturn:0keyUpdates:0numYields:39locks(micros)r:121572nreturned:95052reslen:4194299202ms 2014-07-18T23:21:53.376+0800[clientcursormon]mem(MB)res:136virt:12809 2014-07-18T23:21:53.376+0800[clientcursormon]mapped(incljournalview):12508 2014-07-18T23:21:53.376+0800[clientcursormon]connections:2 2014-07-18T23:21:56.790+0800[conn2]getmoretest.my_collectioncursorid:34538199491ntoreturn:0keyUpdates:0numYields:88locks(micros)r:142113nreturned:95595reslen:4194301244ms Killed
数据查询脚本:
[root@centosdata]#catmongotestList.py importpymongo importtime client=pymongo.MongoClient("localhost",27017) db=client.test printdb.name printdb.my_collection foritemindb.my_collection.find(): printitem
数据插入脚本:
[root@centosdata]#catmongotest2.py importpymongo importtime client=pymongo.MongoClient("localhost",27017) db=client.test printdb.name printdb.my_collection whileTrue: db.my_collection.save({time.ctime():time.time()})