Single-Node Replica Set
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 IP | Host Role |
|---|---|
| 192.168.10.4 | MongoDB Single-Node Replica Set |
Start Deployment
-
Download the MongoDB binary archive and extract it to the installation directory
- RHEL 8 x64
- RHEL 9.3 x64
- RHEL 10 x64
- Debian 12 x64
- Ubuntu 24.04 x64
- Other
-
Download the installation package
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel8-8.3.13.tgz -
Create the installation directory
mkdir -p /usr/local/mongodb -
Extract to the installation directory
tar -zxf mongodb-linux-x86_64-rhel8-8.3.13.tgz --strip-components=1 -C /usr/local/mongodb
-
Download the installation package
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel93-8.3.13.tgz -
Create the installation directory
mkdir -p /usr/local/mongodb -
Extract to the installation directory
tar -zxf mongodb-linux-x86_64-rhel93-8.3.13.tgz --strip-components=1 -C /usr/local/mongodb
-
Download the installation package
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel10-8.3.13.tgz -
Create the installation directory
mkdir -p /usr/local/mongodb -
Extract to the installation directory
tar -zxf mongodb-linux-x86_64-rhel10-8.3.13.tgz --strip-components=1 -C /usr/local/mongodb
-
Download the installation package
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-debian12-8.3.13.tgz -
Create the installation directory
mkdir -p /usr/local/mongodb -
Extract to the installation directory
tar -zxf mongodb-linux-x86_64-debian12-8.3.13.tgz --strip-components=1 -C /usr/local/mongodb
-
Download the installation package
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2404-8.3.13.tgz -
Create the installation directory
mkdir -p /usr/local/mongodb -
Extract to the installation directory
tar -zxf mongodb-linux-x86_64-ubuntu2404-8.3.13.tgz --strip-components=1 -C /usr/local/mongodb
-
On Debian 13, you can try the Debian 12 package and test the deployment.
-
On Ubuntu 26.04, you can try the Ubuntu 24.04 package and test the deployment.
-
Compatibility with Chinese Linux distributions: For Kylin, UnionTech UOS, Euler, and similar distributions whose installed version is compatible with RHEL 8, first try the following binary archive for the matching architecture and test it in the target environment.
-
For binary archives for other operating systems or architectures, visit the MongoDB Community Edition Download Center.
-
Download the mongosh binary archive
- AMD64
- ARM64
-
Download the installation package
wget https://downloads.mongodb.com/compass/mongosh-2.14.1-linux-x64.tgz -
Extract the installation package
tar -zxf mongosh-2.14.1-linux-x64.tgz -
Install to
/usr/local/bincp mongosh-2.14.1-linux-x64/bin/mongosh /usr/local/bin/mongoshchmod 755 /usr/local/bin/mongosh
-
Download the installation package
wget https://downloads.mongodb.com/compass/mongosh-2.14.1-linux-arm64.tgz -
Extract the installation package
tar -zxf mongosh-2.14.1-linux-arm64.tgz -
Install to
/usr/local/bincp mongosh-2.14.1-linux-arm64/bin/mongosh /usr/local/bin/mongoshchmod 755 /usr/local/bin/mongosh
-
Check the MongoDB and mongosh versions
/usr/local/mongodb/bin/mongod --versionmongosh --versionConfirm 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.
-
Create the mongodb system user
useradd -U -M -s /sbin/nologin mongodb -
Create data and log directories and grant permissions
mkdir -p /data/mongodb/ /data/logs/mongodbecho '1/3GWm7LjfCX/K2NOhv0evgBWan+KNX' > /data/mongodb/keyfilechmod 400 /data/mongodb/keyfilechown -R mongodb:mongodb /usr/local/mongodb/ /data/mongodb/ /data/logs/mongodb -
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.3DefaultDependencies=noAfter=sysinit.target local-fs.targetBefore=mongodb.service[Service]Type=oneshotRemainAfterExit=yesExecStart=/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.targetEOFsystemctl daemon-reloadsystemctl enable --now enable-thp -
Create the MongoDB configuration file
cat > /etc/mongod.conf <<'EOF'systemLog:destination: filepath: /data/logs/mongodb/mongodb.loglogAppend: truelogRotate: reopenstorage:dbPath: /data/mongodbnet:port: 27017bindIp: 0.0.0.0security:authorization: enabledkeyFile: /data/mongodb/keyfilereplication:replSetName: local-mongodb-onesetParameter:disableSplitHorizonIPCheck: trueEOF -
Configure the systemd service file
cat > /etc/systemd/system/mongodb.service <<'EOF'[Unit]Description=MongoDBAfter=network-online.target enable-thp.serviceWants=network-online.targetRequires=enable-thp.service[Service]User=mongodbGroup=mongodbEnvironment="GLIBC_TUNABLES=glibc.pthread.rseq=0"LimitNOFILE=1000000LimitNPROC=1000000LimitMEMLOCK=infinityExecStart=/usr/local/mongodb/bin/mongod --config /etc/mongod.confExecStop=/usr/bin/kill $MAINPIDRestart=on-failureRestartSec=5TimeoutStopSec=300[Install]WantedBy=multi-user.targetEOF -
Enable startup at boot
# Start the service only after completing the user creation steps belowsystemctl daemon-reloadsystemctl enable mongodb
Create Database Users
-
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 -
Log in to MongoDB
Run in the system terminal:
mongosh --host 127.0.0.1 --port 27017Run the following user creation statements in mongosh.
-
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.
- The example password for the root user in the MongoDB admin database is
-
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.
- The example password for the hap user is
-
Exit mongosh
exit(); -
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
systemctl start mongodb
Configure the Replica Set
-
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 -
Initialize the single-node replica set
Run in mongosh, replacing the IP address with this server's actual private IP address.
rs.initiate({_id: 'local-mongodb-one',members: [{_id: 1, host: '192.168.10.4:27017'}],}); -
Check the replica set status
rs.status();Confirm that
memberscontains only this server, and wait until itsstateStrbecomesPRIMARY. -
Exit mongosh
exit();
Verify Service Status
-
Check the MongoDB service status
systemctl status mongodb -
Test a business database connection
Replace the connection address with this server's actual IP address and use the credentials set when creating the business database users.
mongosh 'mongodb://192.168.10.4:27017/?replicaSet=local-mongodb-one' -u hap -p 123456 --authenticationDatabase mdwsrows -
Exit mongosh
exit();
Configure Automatic Log Rotation
-
Check the required commands
/usr/sbin/logrotate --versiongzip --version/usr/bin/systemctl --version -
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 {dailyrotate 180dateextdateformat -%Y-%m-%ddateyesterdaymissingoknotifemptycompressdelaycompresssu mongodb mongodbcreate 0640 mongodb mongodbsharedscriptspostrotateif /usr/bin/systemctl is-active --quiet mongodb.service; then/usr/bin/systemctl kill --kill-who=main --signal=SIGUSR1 mongodb.servicefiendscript}EOF -
Create the log rotation service
cat > /etc/systemd/system/mongodb-logrotate.service <<'EOF'[Unit]Description=Rotate MongoDB logsAfter=mongodb.service[Service]Type=oneshotExecStart=/usr/sbin/logrotate --state /var/lib/mongodb-logrotate.status /etc/mongodb-logrotate.confEOF -
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:00Persistent=true[Install]WantedBy=timers.targetEOF -
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.timerCheck the rotation configuration without modifying any logs:
/usr/sbin/logrotate --debug --state /var/lib/mongodb-logrotate.status /etc/mongodb-logrotate.confAfter 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.confList the log files and confirm that
mongodb.log-YYYY-MM-DDhas 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 tomongodb.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.logAfter new log entries appear, press
Ctrl+Cto stop following the log. -
Enable the timer
systemctl daemon-reloadsystemctl enable --now mongodb-logrotate.timerList the timer and confirm that the
NEXTcolumn shows the next scheduled run:systemctl list-timers mongodb-logrotate.timerAfter the timer has triggered the task, view its execution records:
journalctl -u mongodb-logrotate.service -n 20 --no-pager