Skip to main content

Three-Node Replica Set

Version Selection

For a new HAP deployment, you can use MongoDB 8.3 if all environments (including test environments) use cluster deployments with independently deployed MongoDB instances.

If the customer already has an environment running MongoDB 4.4, any environment uses a HAP single-node deployment, or data needs to be migrated to or from an existing HAP single-node or MongoDB 4.4 environment, continue using MongoDB 4.4 to keep database versions consistent across environments.

Server IPHost Role
192.168.10.4MongoDB Replica Set Node01
192.168.10.5MongoDB Replica Set Node02
192.168.10.6MongoDB Replica Set Node03

Start Deployment​

Where to Run These Steps

Complete this section on all three servers: Node01, Node02, and Node03. The steps and configuration are identical.

  1. Download the MongoDB binary archive and extract it to the installation directory

    1. Download the installation package

      wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel8-8.3.13.tgz
    2. Create the installation directory

      mkdir -p /usr/local/mongodb
    3. Extract to the installation directory

      tar -zxf mongodb-linux-x86_64-rhel8-8.3.13.tgz --strip-components=1 -C /usr/local/mongodb
  2. Download the mongosh binary archive

    1. Download the installation package

      wget https://downloads.mongodb.com/compass/mongosh-2.14.1-linux-x64.tgz
    2. Extract the installation package

      tar -zxf mongosh-2.14.1-linux-x64.tgz
    3. Install to /usr/local/bin

      cp mongosh-2.14.1-linux-x64/bin/mongosh /usr/local/bin/mongosh
      chmod 755 /usr/local/bin/mongosh
  3. Check the MongoDB and mongosh versions

    /usr/local/mongodb/bin/mongod --version
    mongosh --version

    Confirm that both commands display their version numbers before continuing. If either reports missing shared libraries, install the required runtime dependencies for the target system first.

  4. Create the mongodb system user

    useradd -U -M -s /sbin/nologin mongodb
  5. Create data and log directories and grant permissions

    mkdir -p /data/mongodb/ /data/logs/mongodb
    echo '1/3GWm7LjfCX/K2NOhv0evgBWan+KNX' > /data/mongodb/keyfile
    chmod 400 /data/mongodb/keyfile
    chown -R mongodb:mongodb /usr/local/mongodb/ /data/mongodb/ /data/logs/mongodb
  6. Enable Transparent Huge Pages (THP)

    MongoDB 8.3 recommends enabling THP. Configure it through a systemd service before starting MongoDB.

    cat > /etc/systemd/system/enable-thp.service <<'EOF'
    [Unit]
    Description=Enable Transparent Huge Pages (THP) for MongoDB 8.3
    DefaultDependencies=no
    After=sysinit.target local-fs.target
    Before=mongodb.service

    [Service]
    Type=oneshot
    RemainAfterExit=yes
    ExecStart=/bin/sh -c 'echo always > /sys/kernel/mm/transparent_hugepage/enabled'
    ExecStart=/bin/sh -c 'echo defer+madvise > /sys/kernel/mm/transparent_hugepage/defrag'
    ExecStart=/bin/sh -c 'echo 0 > /sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_none'
    ExecStart=/bin/sh -c 'echo 1 > /proc/sys/vm/overcommit_memory'

    [Install]
    WantedBy=basic.target
    EOF

    systemctl daemon-reload
    systemctl enable --now enable-thp
  7. Create the MongoDB configuration file

    cat > /etc/mongod.conf <<'EOF'
    systemLog:
    destination: file
    path: /data/logs/mongodb/mongodb.log
    logAppend: true
    logRotate: reopen
    storage:
    dbPath: /data/mongodb
    net:
    port: 27017
    bindIp: 0.0.0.0
    security:
    authorization: enabled
    keyFile: /data/mongodb/keyfile
    replication:
    replSetName: local-mongodb-one
    setParameter:
    disableSplitHorizonIPCheck: true
    EOF
  8. Configure the systemd service file

    cat > /etc/systemd/system/mongodb.service <<'EOF'
    [Unit]
    Description=MongoDB
    After=network-online.target enable-thp.service
    Wants=network-online.target
    Requires=enable-thp.service
    [Service]
    User=mongodb
    Group=mongodb
    Environment="GLIBC_TUNABLES=glibc.pthread.rseq=0"
    LimitNOFILE=1000000
    LimitNPROC=1000000
    LimitMEMLOCK=infinity
    ExecStart=/usr/local/mongodb/bin/mongod --config /etc/mongod.conf
    ExecStop=/usr/bin/kill $MAINPID
    Restart=on-failure
    RestartSec=5
    TimeoutStopSec=300
    [Install]
    WantedBy=multi-user.target
    EOF
  9. Enable startup at boot

    # Start the service only after completing the user creation steps below
    systemctl daemon-reload
    systemctl enable mongodb

Create Database Users​

Run on MongoDB Replica Set Node01.

  1. Temporarily start MongoDB without authentication

    su -c '/usr/local/mongodb/bin/mongod --fork --logpath /data/logs/mongodb/mongodb.log --logappend --dbpath /data/mongodb --noauth --port 27017 --bind_ip 127.0.0.1' -s /bin/bash mongodb
  2. Log in to MongoDB

    Run in the system terminal:

    mongosh --host 127.0.0.1 --port 27017

    Run the following user creation statements in mongosh.

  3. Create the administrator user

    db.getSiblingDB('admin').createUser({
    user: 'root',
    pwd: '12345678',
    roles: [{role: 'root', db: 'admin'}],
    });
    • The example password for the root user in the MongoDB admin database is 12345678. Replace it with a strong password for your deployment.
  4. Create business database users

    var businessUser = "hap";
    var businessPassword = "123456";
    var databases = [
    "MDLicense",
    "ClientLicense",
    "commonbase",
    "MDAlert",
    "mdapproles",
    "mdapprove",
    "mdapps",
    "mdattachment",
    "mdcalendar",
    "mdcategory",
    "MDChatTop",
    "mdcheck",
    "mddossier",
    "mdemail",
    "mdform",
    "MDGroup",
    "mdgroups",
    "MDHistory",
    "mdIdentification",
    "mdinbox",
    "mdkc",
    "mdmap",
    "mdmobileaddress",
    "MDNotification",
    "mdpost",
    "mdreportdata",
    "mdroles",
    "mdsearch",
    "mdservicedata",
    "mdsms",
    "MDSso",
    "mdtag",
    "mdtransfer",
    "MDUser",
    "mdworkflow",
    "mdworksheet",
    "mdworkweixin",
    "mdwsrows",
    "pushlog",
    "taskcenter",
    "mdintegration",
    "mdactionlog",
    "mdworksheetlog",
    "mdworksheetsearch",
    "mddatapipeline",
    "mdwfplugin",
    "mdpayment",
    "mdwfai",
    "mdopenauth",
    "mdaisearch",
    "MDAgentInfra"
    ];
    for (var i = 0; i < databases.length; i++) {
    var database = databases[i];
    db.getSiblingDB(database).createUser({
    user: businessUser,
    pwd: businessPassword,
    roles: [{role: "readWrite", db: database}]
    });
    }
    • The example password for the hap user is 123456. All business databases use the same username and password. Replace the password with a strong one for your deployment.
    • If the password contains special characters, use only - or _. Do not use characters such as @ ! # & to avoid compatibility issues.
  5. Exit mongosh

    exit();
  6. Stop the temporary MongoDB instance

    Return to the system terminal and run:

    su -c '/usr/local/mongodb/bin/mongod --shutdown --dbpath /data/mongodb' -s /bin/bash mongodb

Start MongoDB​

Run on all three MongoDB servers.

systemctl start mongodb

Configure the Replica Set​

Run on MongoDB Replica Set Node01.

  1. Log in to MongoDB

    Replace the username and password with the values used when creating the administrator user.

    mongosh --host 127.0.0.1 --port 27017 -u root -p 12345678 --authenticationDatabase admin
  2. Configure the Replica Set

    Before running the command, replace the member IP addresses with the actual server addresses and confirm that all three servers can reach each other on TCP port 27017.

    rs.initiate({
    _id: 'local-mongodb-one',
    members: [
    {_id: 1, host: '192.168.10.4:27017'},
    {_id: 2, host: '192.168.10.5:27017'},
    {_id: 3, host: '192.168.10.6:27017'},
    ],
    });
  3. Check the replica set status

    rs.status();

    Wait for the election and synchronization to complete. The expected state is one PRIMARY and two SECONDARY members.

  4. Exit the administrator session

    exit();
  5. Test a business database connection

    Run in the system terminal. Replace the connection addresses with the actual IP addresses and use the credentials set when creating the business database users.

    mongosh 'mongodb://192.168.10.4:27017,192.168.10.5:27017,192.168.10.6:27017/?replicaSet=local-mongodb-one' -u hap -p 123456 --authenticationDatabase mdwsrows
  6. Exit mongosh

    exit();

Configure Automatic Log Rotation​

Configure automatic log rotation on all three servers: Node01, Node02, and Node03. The steps and configuration are identical.

  1. Check the required commands

    /usr/sbin/logrotate --version
    gzip --version
    /usr/bin/systemctl --version
  2. Create the log rotation configuration

    Rotated logs are named with the date of the day before rotation. Up to 180 files are retained; the oldest is automatically deleted when this limit is exceeded. The most recent rotated file remains uncompressed, while older files are automatically compressed with gzip.

    cat > /etc/mongodb-logrotate.conf <<'EOF'
    /data/logs/mongodb/mongodb.log {
    daily
    rotate 180
    dateext
    dateformat -%Y-%m-%d
    dateyesterday
    missingok
    notifempty
    compress
    delaycompress
    su mongodb mongodb
    create 0640 mongodb mongodb
    sharedscripts
    postrotate
    if /usr/bin/systemctl is-active --quiet mongodb.service; then
    /usr/bin/systemctl kill --kill-who=main --signal=SIGUSR1 mongodb.service
    fi
    endscript
    }
    EOF
  3. Create the log rotation service

    cat > /etc/systemd/system/mongodb-logrotate.service <<'EOF'
    [Unit]
    Description=Rotate MongoDB logs
    After=mongodb.service

    [Service]
    Type=oneshot
    ExecStart=/usr/sbin/logrotate --state /var/lib/mongodb-logrotate.status /etc/mongodb-logrotate.conf
    EOF
  4. Create the timer

    The task runs once a day around 00:00 in the server's local time. If a scheduled run is missed while the server is off, it runs once when the timer becomes active again.

    cat > /etc/systemd/system/mongodb-logrotate.timer <<'EOF'
    [Unit]
    Description=Rotate MongoDB logs daily

    [Timer]
    OnCalendar=*-*-* 00:00:00
    Persistent=true

    [Install]
    WantedBy=timers.target
    EOF
  5. Check the configuration and verify rotation

    Check the systemd service and timer configuration:

    systemd-analyze verify /etc/systemd/system/mongodb-logrotate.service /etc/systemd/system/mongodb-logrotate.timer

    Check the rotation configuration without modifying any logs:

    /usr/sbin/logrotate --debug --state /var/lib/mongodb-logrotate.status /etc/mongodb-logrotate.conf

    After the checks pass, rotate the log manually once. Do not repeat this if the log has already been rotated that day, to avoid a conflict with the dated filename:

    /usr/sbin/logrotate --force --verbose --state /var/lib/mongodb-logrotate.status /etc/mongodb-logrotate.conf

    List the log files and confirm that mongodb.log-YYYY-MM-DD has been created:

    ls -lh /data/logs/mongodb/mongodb.log*

    For example, rotation at midnight on November 5 creates mongodb.log-2026-11-04. At the next rotation, it is compressed to mongodb.log-2026-11-04.gz.

    Follow the current log to confirm that new entries are written to the new mongodb.log:

    tail -f /data/logs/mongodb/mongodb.log

    After new log entries appear, press Ctrl+C to stop following the log.

  6. Enable the timer

    systemctl daemon-reload
    systemctl enable --now mongodb-logrotate.timer

    List the timer and confirm that the NEXT column shows the next scheduled run:

    systemctl list-timers mongodb-logrotate.timer

    After the timer has triggered the task, view its execution records:

    journalctl -u mongodb-logrotate.service -n 20 --no-pager