§2023-07-24

  1. setup mongodb user
$ sudo useradd -u 966  -d /var/lib/mongodb -s /usr/bin/nologin mongodb
useradd: Warning: missing or non-executable shell '/usr/bin/nologin'

$ grep mongodb /etc/passwd
mongodb:x:966:1027::/var/lib/mongodb:/usr/bin/nologin
$ grep mongodb /etc/group
mongodb:x:1027:
$ sudo groupmod -g 966 mongodb
$ id mongodb
uid=966(mongodb) gid=966(mongodb) groups=966(mongodb)

$ ls -l /var/lib/mongodb  --> why it is not created ?? 
ls: cannot access '/var/lib/mongodb': No such file or directory
$ sudo mkdir  /var/lib/mongodb 
$ sudo chown mongodb:mongodb -R /var/lib/mongodb/
  1. /etc/mongodb.conf as
# mongodb.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  
  # if your MongoDB version is 4.0 or newer, you can safely remove the "storage.journal.enabled" 
  # option from your configuration file.
  # journal:
    # enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.


#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:
$ ls -l /etc/mongodb.conf 
-rw-r--r-- 1 root root 870 Jul 24 07:48 /etc/mongodb.conf
  1. /usr/lib/systemd/system/mongodb.service as
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network-online.target
Wants=network-online.target

[Service]
TimeoutStartSec=infinity
User=mongodb
Group=mongodb
Environment="OPTIONS=-f /etc/mongodb.conf"
Environment="MONGODB_CONFIG_OVERRIDE_NOFORK=1"
ExecStart=/usr/local/bin/mongod $OPTIONS
RuntimeDirectory=mongodb
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings

[Install]
WantedBy=multi-user.target
  1. copy mongod into /usr/local/bin

sudo touch /var/log/mongod.log && sudo chown mongodb:mongodb /var/log/mongod.log first before you start mongod.service or else

alexlai@hc4Jammy:~$ journalctl -xfu mongodb.service
Jul 24 07:55:07 hc4Jammy mongod[1444]: {"t":{"$date":"2023-07-23T23:55:07.539Z"},"s":"F",  "c":"CONTROL",  "id":20574,  
"ctx":"main","msg":"Error during global initialization","attr":{"error":{"code":38,"codeName":"FileNotOpen",
"errmsg":"Can't initialize rotatable log file :: caused by :: Failed to open /var/log/mongod.log"}}}
Jul 24 07:55:07 hc4Jammy systemd[1]: mongodb.service: Main process exited, code=exited, status=1/FAILURE
  1. start mongodb.service
$ sudo systemctl restart mongodb.service

$ sudo systemctl status  mongodb.service
● mongodb.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongodb.service; disabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-07-24 07:56:49 CST; 19s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 1487 (mongod)
     Memory: 80.3M
        CPU: 1.572s
     CGroup: /system.slice/mongodb.service
             └─1487 /usr/bin/mongod -f /etc/mongodb.conf

Jul 24 07:56:49 hc4Jammy systemd[1]: Started MongoDB Database Server.
Jul 24 07:56:50 hc4Jammy mongod[1487]: {"t":{"$date":"2023-07-23T23:56:50.021Z"},"s":"I",  "c":"CONTROL",  "id":7484500, "ctx":"main","msg":"Environment variable MONGODB_CONFIG_OVERRIDE_NOFORK == 1, overriding \"processManagement.fork\" to false"}
  1. To be done