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 |
Start Deployment
MongoDB Replica Set Node01
-
Download the MongoDB installation package
- Internet Access Available
- Internet Access Unavailable
wget https://pdpublic.mingdao.com/private-deployment/offline/common/mongodb-linux-x86_64-3.4.24.tgz# MongoDB installation package download link; upload to the deployment server after downloadhttps://pdpublic.mingdao.com/private-deployment/offline/common/mongodb-linux-x86_64-3.4.24.tgz -
Extract to the installation directory
tar -zxvf mongodb-linux-x86_64-3.4.24.tgzmv mongodb-linux-x86_64-3.4.24 /usr/local/mongodb -
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-oneExecStop=/usr/bin/kill \$MAINPIDRestart=on-failureRestartSec=5TimeoutStopSec=300[Install]WantedBy=multi-user.targetEOF -
Add to startup
# Do not use systemctl start mongodb to start the service immediately after installation because the user is not yet createdsystemctl daemon-reloadsystemctl enable mongodb
MongoDB Replica Set Node02
-
Download the MongoDB installation package
- Internet Access Available
- Internet Access Unavailable
wget https://pdpublic.mingdao.com/private-deployment/offline/common/mongodb-linux-x86_64-3.4.24.tgz# MongoDB installation package download link; upload to the deployment server after downloadhttps://pdpublic.mingdao.com/private-deployment/offline/common/mongodb-linux-x86_64-3.4.24.tgz -
Extract to the installation directory
tar -zxvf mongodb-linux-x86_64-3.4.24.tgzmv mongodb-linux-x86_64-3.4.24 /usr/local/mongodb -
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-oneExecStop=/usr/bin/kill \$MAINPIDRestart=on-failureRestartSec=5TimeoutStopSec=300[Install]WantedBy=multi-user.targetEOF -
Add to startup
# Do not use systemctl start mongodb to start the service immediately after installation because the user is not yet createdsystemctl daemon-reloadsystemctl enable mongodb
MongoDB Replica Set Node03
-
Download the MongoDB installation package
- Internet Access Available
- Internet Access Unavailable
wget https://pdpublic.mingdao.com/private-deployment/offline/common/mongodb-linux-x86_64-3.4.24.tgz# MongoDB installation package download link; upload to the deployment server after downloadhttps://pdpublic.mingdao.com/private-deployment/offline/common/mongodb-linux-x86_64-3.4.24.tgz -
Extract to the installation directory
tar -zxvf mongodb-linux-x86_64-3.4.24.tgzmv mongodb-linux-x86_64-3.4.24 /usr/local/mongodb -
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-oneExecStop=/usr/bin/kill \$MAINPIDRestart=on-failureRestartSec=5TimeoutStopSec=300[Install]WantedBy=multi-user.targetEOF -
Add to startup
# Do not use systemctl start mongodb to start the service immediately after installation because the user is not yet createdsystemctl daemon-reloadsystemctl enable mongodb
Create Database Users
Run on MongoDB Replica Set Node01.
-
Temporarily start MongoDB without 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 -
Log in to MongoDB
Run in the system terminal:
/usr/local/mongodb/bin/mongo --host 127.0.0.1 --port 27017Run the following user creation statements in mongo.
-
Create the administrator user
db.getSiblingDB("admin").createUser({user: "root",pwd: "123456",roles: [{role: "root", db: "admin"}]});- The example password for the root user in the MongoDB admin database is
123456. 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 users in the MongoDB business databases is
123456. Replace it with a strong password 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 users in the MongoDB business databases is
-
Exit mongo
exit(); -
Stop the temporary MongoDB instance
Return to the system terminal and run:
kill $(pgrep -f 'mongod')
Start MongoDB
Operate on the three servers deployed with MongoDB
systemctl start mongodb
Configure Replica Set
Operate on the MongoDB Replica Set Node01
-
Login to MongoDB
/usr/local/mongodb/bin/mongo -u root -p 123456 --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, be sure to modify the IP addresses of the member nodes to the actual server addresses.
-
Check the replica set status
rs.status()# In the output, typically one node will be PRIMARY, and the other two will be in SECONDARY state.