Replica Set
| Server IP | Host Role |
|---|---|
| 192.168.10.4 | MongoDB Replica Set Node01 |
| 192.168.10.5 | MongoDB Replica Set Node02 |
| 192.168.10.6 | MongoDB Replica Set Node03 |
Begin Deployment
MongoDB Replica Set Node01
-
Download the MongoDB installation package and extract it to the installation directory
- RedHat / CentOS 7.0 x64
- RedHat / CentOS 8.0 x64
- Debian 10.0+ x64
- Other
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.30.tgztar -zxvf mongodb-linux-x86_64-rhel70-4.4.30.tgzmv mongodb-linux-x86_64-rhel70-4.4.30 /usr/local/mongodbwget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel80-4.4.30.tgztar -zxvf mongodb-linux-x86_64-rhel80-4.4.30.tgzmv mongodb-linux-x86_64-rhel80-4.4.30 /usr/local/mongodb- If the operating system is higher than Debian 10, first install the libssl1.1 dependency
wget https://pdpublic.mingdao.com/private-deployment/offline/common/libssl1.1_1.1.1w-0+deb11u1_amd64.debdpkg -i libssl1.1_1.1.1w-0+deb11u1_amd64.deb- Download the MongoDB installation package and extract it to the installation directory
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-debian10-4.4.30.tgztar -zxvf mongodb-linux-x86_64-debian10-4.4.30.tgzmv mongodb-linux-x86_64-debian10-4.4.30 /usr/local/mongodb-
Official Download: For packages targeting other operating systems, visit the MongoDB Community Edition Download Center.
-
Domestic System Compatibility: For systems like EulerOS, Kylin, and UOS, the corresponding CentOS 7/8 packages are generally compatible.
-
Example (Kylin V10): If using the Kylin V10 ARM version, you can download the CentOS 8 ARM 64 architecture package:
-
Create a MongoDB user
useradd -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 -
Disable Transparent Huge Pages (THP)
MongoDB may experience performance degradation and memory fragmentation when THP is enabled. It is recommended to disable it via a systemd service before starting MongoDB.
cat > /etc/systemd/system/disable-thp.service <<\EOF[Unit]Description=Disable Transparent Huge Pages (THP)DefaultDependencies=noAfter=sysinit.target local-fs.targetBefore=mongodb.service[Service]Type=oneshotExecStart=/bin/sh -c 'echo never > /sys/kernel/mm/transparent_hugepage/enabled'ExecStart=/bin/sh -c 'echo never > /sys/kernel/mm/transparent_hugepage/defrag'[Install]WantedBy=basic.targetEOFsystemctl daemon-reloadsystemctl enable disable-thpsystemctl start disable-thp -
Configure the systemd management file
cat > /etc/systemd/system/mongodb.service <<'EOF'[Unit]Description=MongoDBAfter=network-online.targetWants=network-online.target[Service]User=mongodbGroup=mongodbLimitNOFILE=1000000LimitNPROC=1000000LimitMEMLOCK=infinityExecStart=/usr/local/mongodb/bin/mongod --logpath /data/logs/mongodb/mongodb.log --dbpath /data/mongodb --auth --keyFile /data/mongodb/keyfile --port 27017 --replSet local-mongodb-one --bind_ip 0.0.0.0ExecStop=/usr/bin/kill $MAINPIDRestart=on-failureRestartSec=5TimeoutStopSec=300[Install]WantedBy=multi-user.targetEOF -
Add to startup
# Since the user is not created after installation, do not start the service with systemctl start mongodb yetsystemctl daemon-reloadsystemctl enable mongodb
MongoDB Replica Set Node02
-
Download the MongoDB installation package and extract it to the installation directory
- RedHat / CentOS 7.0 x64
- RedHat / CentOS 8.0 x64
- Debian 10.0+ x64
- Other
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.30.tgztar -zxvf mongodb-linux-x86_64-rhel70-4.4.30.tgzmv mongodb-linux-x86_64-rhel70-4.4.30 /usr/local/mongodbwget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel80-4.4.30.tgztar -zxvf mongodb-linux-x86_64-rhel80-4.4.30.tgzmv mongodb-linux-x86_64-rhel80-4.4.30 /usr/local/mongodb- If the operating system is higher than Debian 10, first install the libssl1.1 dependency
wget https://pdpublic.mingdao.com/private-deployment/offline/common/libssl1.1_1.1.1w-0+deb11u1_amd64.debdpkg -i libssl1.1_1.1.1w-0+deb11u1_amd64.deb- Download the MongoDB installation package and extract it to the installation directory
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-debian10-4.4.30.tgztar -zxvf mongodb-linux-x86_64-debian10-4.4.30.tgzmv mongodb-linux-x86_64-debian10-4.4.30 /usr/local/mongodb-
Official Download: For packages targeting other operating systems, visit the MongoDB Community Edition Download Center.
-
Domestic System Compatibility: For systems like EulerOS, Kylin, and UOS, the corresponding CentOS 7/8 packages are generally compatible.
-
Example (Kylin V10): If using the Kylin V10 ARM version, you can download the CentOS 8 ARM 64 architecture package:
-
Create a MongoDB user
useradd -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 -
Disable Transparent Huge Pages (THP)
MongoDB may experience performance degradation and memory fragmentation when THP is enabled. It is recommended to disable it via a systemd service before starting MongoDB.
cat > /etc/systemd/system/disable-thp.service <<\EOF[Unit]Description=Disable Transparent Huge Pages (THP)DefaultDependencies=noAfter=sysinit.target local-fs.targetBefore=mongodb.service[Service]Type=oneshotExecStart=/bin/sh -c 'echo never > /sys/kernel/mm/transparent_hugepage/enabled'ExecStart=/bin/sh -c 'echo never > /sys/kernel/mm/transparent_hugepage/defrag'[Install]WantedBy=basic.targetEOFsystemctl daemon-reloadsystemctl enable disable-thpsystemctl start disable-thp -
Configure the systemd management file
cat > /etc/systemd/system/mongodb.service <<'EOF'[Unit]Description=MongoDBAfter=network-online.targetWants=network-online.target[Service]User=mongodbGroup=mongodbLimitNOFILE=1000000LimitNPROC=1000000LimitMEMLOCK=infinityExecStart=/usr/local/mongodb/bin/mongod --logpath /data/logs/mongodb/mongodb.log --dbpath /data/mongodb --auth --keyFile /data/mongodb/keyfile --port 27017 --replSet local-mongodb-one --bind_ip 0.0.0.0ExecStop=/usr/bin/kill $MAINPIDRestart=on-failureRestartSec=5TimeoutStopSec=300[Install]WantedBy=multi-user.targetEOF -
Add to startup
# Since the user is not created after installation, do not start the service with systemctl start mongodb yetsystemctl daemon-reloadsystemctl enable mongodb
MongoDB Replica Set Node03
-
Download the MongoDB installation package and extract it to the installation directory
- RedHat / CentOS 7.0 x64
- RedHat / CentOS 8.0 x64
- Debian 10.0+ x64
- Other
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.30.tgztar -zxvf mongodb-linux-x86_64-rhel70-4.4.30.tgzmv mongodb-linux-x86_64-rhel70-4.4.30 /usr/local/mongodbwget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel80-4.4.30.tgztar -zxvf mongodb-linux-x86_64-rhel80-4.4.30.tgzmv mongodb-linux-x86_64-rhel80-4.4.30 /usr/local/mongodb- If the operating system is higher than Debian 10, first install the libssl1.1 dependency
wget https://pdpublic.mingdao.com/private-deployment/offline/common/libssl1.1_1.1.1w-0+deb11u1_amd64.debdpkg -i libssl1.1_1.1.1w-0+deb11u1_amd64.deb- Download the MongoDB installation package and extract it to the installation directory
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-debian10-4.4.30.tgztar -zxvf mongodb-linux-x86_64-debian10-4.4.30.tgzmv mongodb-linux-x86_64-debian10-4.4.30 /usr/local/mongodb-
Official Download: For packages targeting other operating systems, visit the MongoDB Community Edition Download Center.
-
Domestic System Compatibility: For systems like EulerOS, Kylin, and UOS, the corresponding CentOS 7/8 packages are generally compatible.
-
Example (Kylin V10): If using the Kylin V10 ARM version, you can download the CentOS 8 ARM 64 architecture package:
-
Create a MongoDB user
useradd -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 -
Disable Transparent Huge Pages (THP)
MongoDB may experience performance degradation and memory fragmentation when THP is enabled. It is recommended to disable it via a systemd service before starting MongoDB.
cat > /etc/systemd/system/disable-thp.service <<\EOF[Unit]Description=Disable Transparent Huge Pages (THP)DefaultDependencies=noAfter=sysinit.target local-fs.targetBefore=mongodb.service[Service]Type=oneshotExecStart=/bin/sh -c 'echo never > /sys/kernel/mm/transparent_hugepage/enabled'ExecStart=/bin/sh -c 'echo never > /sys/kernel/mm/transparent_hugepage/defrag'[Install]WantedBy=basic.targetEOFsystemctl daemon-reloadsystemctl enable disable-thpsystemctl start disable-thp -
Configure the systemd management file
cat > /etc/systemd/system/mongodb.service <<'EOF'[Unit]Description=MongoDBAfter=network-online.targetWants=network-online.target[Service]User=mongodbGroup=mongodbLimitNOFILE=1000000LimitNPROC=1000000LimitMEMLOCK=infinityExecStart=/usr/local/mongodb/bin/mongod --logpath /data/logs/mongodb/mongodb.log --dbpath /data/mongodb --auth --keyFile /data/mongodb/keyfile --port 27017 --replSet local-mongodb-one --bind_ip 0.0.0.0ExecStop=/usr/bin/kill $MAINPIDRestart=on-failureRestartSec=5TimeoutStopSec=300[Install]WantedBy=multi-user.targetEOF -
Add to startup
# Since the user is not created after installation, do not start the service with systemctl start mongodb yetsystemctl daemon-reloadsystemctl enable mongodb
Create Database Users
Operate on MongoDB Replica Set Node01
-
Temporarily start a MongoDB service without enabling connection authentication
su -c '/usr/local/mongodb/bin/mongod --fork --logpath /data/logs/mongodb/mongodb.log --dbpath /data/mongodb --noauth --port 27017' -s /bin/bash mongodb -
Create users
/usr/local/mongodb/bin/mongo <<<'use admindb.createUser({user:"root",pwd:"12345678",roles:[{role:"root",db:"admin"}]})use MDLicensedb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"MDLicense"}]})use ClientLicensedb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"ClientLicense"}]})use commonbasedb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"commonbase"}]})use MDAlertdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"MDAlert"}]})use mdapprolesdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdapproles"}]})use mdapprovedb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdapprove"}]})use mdappsdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdapps"}]})use mdattachmentdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdattachment"}]})use mdcalendardb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdcalendar"}]})use mdcategorydb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdcategory"}]})use MDChatTopdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"MDChatTop"}]})use mdcheckdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdcheck"}]})use mddossierdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mddossier"}]})use mdemaildb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdemail"}]})use mdformdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdform"}]})use MDGroupdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"MDGroup"}]})use mdgroupsdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdgroups"}]})use MDHistorydb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"MDHistory"}]})use mdIdentificationdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdIdentification"}]})use mdinboxdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdinbox"}]})use mdkcdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdkc"}]})use mdmapdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdmap"}]})use mdmobileaddressdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdmobileaddress"}]})use MDNotificationdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"MDNotification"}]})use mdpostdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdpost"}]})use mdreportdatadb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdreportdata"}]})use mdrolesdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdroles"}]})use mdsearchdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdsearch"}]})use mdservicedatadb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdservicedata"}]})use mdsmsdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdsms"}]})use MDSsodb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"MDSso"}]})use mdtagdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdtag"}]})use mdtransferdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdtransfer"}]})use MDUserdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"MDUser"}]})use mdworkflowdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdworkflow"}]})use mdworksheetdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdworksheet"}]})use mdworkweixindb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdworkweixin"}]})use mdwsrowsdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdwsrows"}]})use pushlogdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"pushlog"}]})use taskcenterdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"taskcenter"}]})use mdintegrationdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdintegration"}]})use mdactionlogdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdactionlog"}]})use mdworksheetlogdb.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdworksheetlog"}]})use mdworksheetsearchdb.createUser({user: "hap",pwd: "123456",roles: [{role: "readWrite",db: "mdworksheetsearch"}]})use mddatapipelinedb.createUser({user: "hap",pwd: "123456",roles: [{role: "readWrite",db: "mddatapipeline"}]})use mdwfplugindb.createUser({user: "hap",pwd: "123456",roles: [{role: "readWrite",db: "mdwfplugin"}]})use mdpaymentdb.createUser({user: "hap",pwd: "123456",roles: [{role: "readWrite",db: "mdpayment"}]})use mdwfaidb.createUser({user: "hap",pwd: "123456",roles: [{role: "readWrite",db: "mdwfai"}]})'- The root user password for the MongoDB admin database specified in the command is
12345678. It is mandatory to change it to a strong password during actual deployment. - The hap user password for the MongoDB business database specified in the command is
123456. It is mandatory to change it to a strong password during actual deployment. - If the password contains special characters, only
-or_are allowed. The use of@ ! # &and other characters is prohibited to avoid compatibility issues.
- The root user password for the MongoDB admin database specified in the command is
-
Shut down the temporarily started MongoDB
kill $(pgrep -f 'mongod')
Start MongoDB
Operate on all three servers where MongoDB is deployed
systemctl start mongodb
Configure Replica Set
Operate on MongoDB Replica Set Node01
-
Log in to MongoDB
/usr/local/mongodb/bin/mongo -u root -p 12345678 --authenticationDatabase admin -
Configure the replica set
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"} ]})- Before execution, make sure to change the IP address of the member nodes to the actual server address.
-
Verify the replica set status
rs.status()# The output should show one node as PRIMARY and two as SECONDARY -
After logging out, use the following command to test the connection of a single business database user
/usr/local/mongodb/bin/mongo -u hap -p 123456 --authenticationDatabase mdwsrows