当前位置: 首页 > 工具软件 > pam-mongodb > 使用案例 >

限制MongoDB内存使用(RHEL7)

艾志尚
2023-12-01

1. 修改MongoDB配置文件

vim /opt/xx/conf/mongo/mongo_standalone.conf

添加MongoDB参数:storage.wiredTiger.engineConfig.cacheSizeGB

systemLog:
   destination: file
   path: "/opt/logs/mongo/mongo_standalone.log"
   logAppend: true
storage:
   dbPath: "/opt/db/mongo/mongo_standalone"
   journal:
      enabled: true
   engine: wiredTiger
   wiredTiger:
      engineConfig:
         cacheSizeGB: 14
processManagement:
   fork: true
   pidFilePath: "/opt/run/mongo/mongo_standalone.pid"
net:
   bindIp: 0.0.0.0
   port: 27017

2. 配置MongoDB使用systemctl启动

添加mongodb.service文件:

vim /etc/systemd/system/mongodb.service 
[Unit]
Description=MongoDB_standalone
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/opt/xxxx/run/mongo/mongo_standalone.pid
ExecStart=/opt/xxxx/init.d/mongo start
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/opt/xxxx/init.d/mongo stop
#ExecStop=/bin/kill -s QUIT $MAINPID
#ExecStop=/opt/bxixxx/mongodb/bin/mongod --shutdown --config /opt/bxxxix/conf/mongo/mongo_standalone.conf 
PrivateTmp=true

[Install]
WantedBy=multi-user.target

3. 限制MongoDB内存使用

/etc/systemd/system/mongodb.service文件中,[Service]部分,添加MemoryLimit(蓝色部分):

cat /etc/systemd/system/mongodb.service 
[Unit]
Description=MongoDB_standalone
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/opt/xxx/run/mongo/mongo_standalone.pid
ExecStart=/opt/bxxxxix/init.d/mongo start
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/opt/xxxx/init.d/mongo stop
PrivateTmp=true
MemoryLimit=20G

[Install]
WantedBy=multi-user.target

4. 启动MongoDB

重新加载systemctl,并用systemctl 启动MongoDB:

# systemctl daemon-reload 

# systemctl start mongodb.service

# systemctl status mongodb
● mongodb.service - MongoDB_standalone
Loaded: loaded (/etc/systemd/system/mongodb.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2019-04-01 10:00:54 CST; 3 days ago
Process: 29132 ExecStart=/opt/xxxeix/init.d/mongo start (code=exited, status=0/SUCCESS)
Main PID: 29139 (mongod)
Memory: 18.0G (limit: 20.0G)
CGroup: /system.slice/mongodb.service
└─29139 /opt/bxxxx/mongo/bin/mongod --config /opt/xxxix/conf/mongo/mongo_standalone.conf

Apr 01 10:00:53 shcp-bdu04 systemd[1]: Starting MongoDB_standalone...
Apr 01 10:00:53 shcp-bdu04 runuser[29135]: pam_unix(runuser:session): session opened for user xxxeset by (uid=0)
Apr 01 10:00:53 shcp-bdu04 mongo[29132]: about to fork child process, waiting until server is ready for connections.
Apr 01 10:00:53 shcp-bdu04 mongo[29132]: forked process: 29139
Apr 01 10:00:54 shcp-bdu04 mongo[29132]: child process started successfully, parent exiting
Apr 01 10:00:54 shcp-bdu04 runuser[29135]: pam_unix(runuser:session): session closed for user bxxxeset
Apr 01 10:00:54 shcp-bdu04 mongo[29132]: [45B blob data]
Apr 01 10:00:54 shcp-bdu04 systemd[1]: Started MongoDB_standalone.

 类似资料: