§2023-08-01

$ lsblk -f
NAME      FSTYPE FSVER LABEL      UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
sda                                                                                   
├─sda1    vfat   FAT16 BOOT_MNJRO D021-FF72                             396.8M    13% /boot
├─sda2    xfs                     3ab22824-0e63-4210-8ded-af6ce3cd0d4b    208G     3% /
└─sda3    swap   1                767bebf4-0844-4b73-a4d6-a050e634af7e            

¶Step 1. setup mongodb user

[alexlai@hc4MnMin ~]$ grep mongodb /etc/passwd
[alexlai@hc4MnMin ~]$ sudo useradd -u 966  -d /var/lib/mongodb -s /usr/bin/nologin mongodb
[sudo] password for alexlai: 
useradd warning: mongodb's uid 966 outside of the UID_MIN 1000 and UID_MAX 60000 range.
[alexlai@hc4MnMin ~]$ grep mongodb /etc/passwd
mongodb:x:966:1001::/var/lib/mongodb:/usr/bin/nologin
[alexlai@hc4MnMin ~]$ grep mongodb /etc/group
mongodb:x:1001:
[alexlai@hc4MnMin ~]$ sudo groupmod -g 966 mongodb
[alexlai@hc4MnMin ~]$ id mongodb
uid=966(mongodb) gid=966(mongodb) groups=966(mongodb)
[alexlai@hc4MnMin ~]$ ls -l /var/lib/mongodb
ls: cannot access '/var/lib/mongodb': No such file or directory
[alexlai@hc4MnMin ~]$ sudo mkdir  /var/lib/mongodb 
[alexlai@hc4MnMin ~]$ sudo chown mongodb:mongodb -R /var/lib/mongodb/

¶Step 2.

$ lsblk -f
NAME      FSTYPE FSVER LABEL      UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
sda                                                                                   
├─sda1    vfat   FAT16 BOOT_MNJRO D021-FF72                             396.8M    13% /boot
├─sda2    xfs                     3ab22824-0e63-4210-8ded-af6ce3cd0d4b  207.7G     3% /
└─sda3    swap   1                767bebf4-0844-4b73-a4d6-a050e634af7e                

$ sudo mkdir -p /opt/xfs/mongodb/{data,log}
$ sudo chown -R mongodb:mongodb /opt/xfs
$ ls -l  /opt/xfs
drwxr-xr-x 4 mongodb mongodb 29 Aug  1 10:11 mongodb
$ ls -l  /opt/xfs/mongodb/
drwxr-xr-x 2 mongodb mongodb 6 Aug  1 10:11 data
drwxr-xr-x 2 mongodb mongodb 6 Aug  1 10:11 log

¶ Step 3, generate x.509

  1. /etc/mongodb.conf
replication:
  replSetName: "MuneTakaShard"    <-- a member of shard
  
sharding:
  clusterRole: "shardsvr"         <-- clusterRole
# 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