mongodb memory problem

Problem overview:

   The new project was deployed on the server. The server ran a mongodb process and a project deployed by tomcat. At first, I didn't know that mongodb ate the memory problem, and the memory given to the server was only 10G. Later, when 300w pieces of data were queried, the tomcat project exited and the mongodb process crashed.

Operation 1:

So when I increase the memory from 10G to 32G, I use some commands to understand mongodb memory:

> db.serverStatus()
{
    "ok" : 0,
    "errmsg" : "not authorized on admin to execute command { serverStatus: 1.0 }",
    "code" : 13,
    "codeName" : "Unauthorized"
}
(The root cause of this problem is that I am creating admin The role given to users is not root,But userAdminAnyDatabase,Therefore, many operations are limited, so I will do the operation assigned to the current user.)
> db.grantRolesToUser( "admin" , [ { role: "root", db: "admin" } ]) 
> show users
{
    "_id" : "admin.admin",
    "user" : "admin",
    "db" : "admin",
    "roles" : [
        {
            "role" : "root",
            "db" : "admin"
        },
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}
> db.serverStatus().mem
{
    "bits" : 64, //Operating system bits
    "resident" : 1996, //Physical memory consumption, M
    "virtual" : 2936, //Virtual memory consumption
    "supported" : true, //true indicates support for displaying additional memory information
    "mapped" : 0, //Map memory 
    "mappedWithJournal" : 0 //In addition to mapping memory, it also includes mapping memory consumed by journal logs
}
> db.stats()
{
    "db" : "admin",
    "collections" : 2,
    "views" : 0,
    "objects" : 4,
    "avgObjSize" : 184.25,
    "dataSize" : 737,
    "storageSize" : 69632,
    "numExtents" : 0,
    "indexes" : 3,
    "indexSize" : 49152,
    "ok" : 1
}
> db.serverStatus().connections
{ "current" : 11,  //Current connections
"available" : 808, //Number of connections available
"totalCreated" : 32  //Total connections created so far
}
> show dbs
admin     0.000GB
local     0.000GB
stdvTest  0.511GB
> use stdvTest
switched to db stdvTest
> db.auth("root","123456")
1
> db.stats()
{
    "db" : "stdvTest",
    "collections" : 8,
    "views" : 0,
    "objects" : 4027236,
    "avgObjSize" : 746.8825003054204,
    "dataSize" : 3007872093,
    "storageSize" : 450211840,
    "numExtents" : 0,
    "indexes" : 9,
    "indexSize" : 98750464,
    "ok" : 1
}
(Can give stats Incoming scale factor: KB The value of is1024,MB by1024*1024)
> db.stats(1024*1024)
{
    "db" : "stdvTest",
    "collections" : 8,
    "views" : 0,
    "objects" : 4115181,
    "avgObjSize" : 746.9989572755123,
    "dataSize" : 2931.6291007995605,
    "storageSize" : 439.08984375,
    "numExtents" : 0,
    "indexes" : 9,
    "indexSize" : 96.390625,
    "ok" : 1
}
> db.serverStatus().connections
{ "current" : 12, "available" : 807, "totalCreated" : 40 }

As the data is mainly stored in the stdvTest database, it can be seen from db.stats() "dataSize" (total data size) is close to 3G, "indexSize" (index size) is about 0.09G. When the dataSize exceeds the memory size, it is better to increase the memory (memory > index + hot data).

Operation 2:

//Check whether the current virtual machine memory is limited: "unlimited", that is, there is no limit
root@ubuntu:/usr/local/mongodb/bin#  ulimit -a | grep 'virtual'
virtual memory          (kbytes, -v) unlimited
//If there are restrictions, you can use: ulimit -m unlimited and ulimit -v unlimited to make changes

//Check the default stack size under Linux (this can be traced back to the number of connections above. Each connection in mongo is a thread and requires a stack)
root@ubuntu:/usr/local/mongodb/bin# ulimit -a | grep stack
stack size              (kbytes, -s) 8192
//View the Stack size of mongodb (command: cat / proc / $(shadow mongod) / limits | grep Stack | awk - f 'size' {print int ($NF) / 1024} ')
root@ubuntu:/usr/local/mongodb/bin# cat /proc/1798/limits | grep stack | awk -F 'size' '{print int($NF)/1024}' 
8192
//If the stack is too large, more than 10240K, we can adjust the stack size by the following command: ulimit -s 1024
//View the memory usage of mongodb through top-p + process number
root@ubuntu:/usr/local/mongodb/bin# top -p 1798
top - 12:23:04 up 18:54,  4 users,  load average: 0.16, 0.19, 0.18
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
%Cpu(s):  6.5 us,  0.8 sy,  0.0 ni, 92.5 id,  0.0 wa,  0.0 hi,  0.2 si,  0.0 st
KiB Mem:  30979848 total,  3658780 used, 27321068 free,    97740 buffers
KiB Swap:  4192252 total,        0 used,  4192252 free.   900308 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                                                   
 1798 root      20   0 3074852 2.064g  24760 S   0.7  7.0   1:09.21 mongod   

//View free memory through free -m
root@ubuntu:/usr/local/mongodb/bin# free -m
             total       used       free     shared    buffers     cached
Mem:         30253       3573      26680          0         95        879
-/+ buffers/cache:       2598      27655
Swap:         4093          0       4093

The above operations are the steps I take when encountering problems. They are just for recording and can be skipped directly. Here are two great God Blogs:
https://www.cnblogs.com/ljhdo/archive/2016/10/30/4947357.html
This article explains the storage engine of mongodb 3.2 and above in detail.
http://blog.csdn.net/xiaoql520/article/details/76973887
Explain why mongodb uses WT engine in detail (the answer to memory problem is also mentioned in it)
This record is only for future reference

Tags: MongoDB Ubuntu Tomcat Database

Posted on Tue, 05 May 2020 15:42:15 -0400 by midgar777