Skip to main content

Replica Set

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

MongoDB Replica Set Node01

  1. Download the MongoDB installation package

    wget https://pdpublic.mingdao.com/private-deployment/offline/common/mongodb-linux-x86_64-3.4.24.tgz
  2. Extract to the installation directory

    tar -zxvf mongodb-linux-x86_64-3.4.24.tgz
    mv mongodb-linux-x86_64-3.4.24 /usr/local/mongodb
  3. Create a MongoDB user

    useradd -M -s /sbin/nologin mongodb
  4. 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
  5. Configure the systemd management file

    cat > /etc/systemd/system/mongodb.service <<EOF
    [Unit]
    Description=MongoDB
    [Service]
    User=mongodb
    Group=mongodb
    LimitNOFILE=1000000
    LimitNPROC=1000000
    ExecStart=/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
    ExecStop=/usr/bin/kill \$MAINPID
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target
    EOF
  6. Add to startup

    # Do not use systemctl start mongodb to start the service immediately after installation because the user is not yet created
    systemctl daemon-reload
    systemctl enable mongodb

MongoDB Replica Set Node02

  1. Download the MongoDB installation package

    wget https://pdpublic.mingdao.com/private-deployment/offline/common/mongodb-linux-x86_64-3.4.24.tgz
  2. Extract to the installation directory

    tar -zxvf mongodb-linux-x86_64-3.4.24.tgz
    mv mongodb-linux-x86_64-3.4.24 /usr/local/mongodb
  3. Create a MongoDB user

    useradd -M -s /sbin/nologin mongodb
  4. 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
  5. Configure the systemd management file

    cat > /etc/systemd/system/mongodb.service <<EOF
    [Unit]
    Description=MongoDB
    [Service]
    User=mongodb
    Group=mongodb
    LimitNOFILE=1000000
    LimitNPROC=1000000
    ExecStart=/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
    ExecStop=/usr/bin/kill \$MAINPID
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target
    EOF
  6. Add to startup

    # Do not use systemctl start mongodb to start the service immediately after installation because the user is not yet created
    systemctl daemon-reload
    systemctl enable mongodb

MongoDB Replica Set Node03

  1. Download the MongoDB installation package

    wget https://pdpublic.mingdao.com/private-deployment/offline/common/mongodb-linux-x86_64-3.4.24.tgz
  2. Extract to the installation directory

    tar -zxvf mongodb-linux-x86_64-3.4.24.tgz
    mv mongodb-linux-x86_64-3.4.24 /usr/local/mongodb
  3. Create a MongoDB user

    useradd -M -s /sbin/nologin mongodb
  4. 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
  5. Configure the systemd management file

    cat > /etc/systemd/system/mongodb.service <<EOF
    [Unit]
    Description=MongoDB
    [Service]
    User=mongodb
    Group=mongodb
    LimitNOFILE=1000000
    LimitNPROC=1000000
    ExecStart=/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
    ExecStop=/usr/bin/kill \$MAINPID
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target
    EOF
  6. Add to startup

    # Do not use systemctl start mongodb to start the service immediately after installation because the user is not yet created
    systemctl daemon-reload
    systemctl enable mongodb

Create Database Users

Operate on the MongoDB Replica Set Node01

  1. Temporarily start a MongoDB service with connection authentication disabled

    su -c '/usr/local/mongodb/bin/mongod --fork --logpath /data/logs/mongodb/mongodb.log --dbpath /data/mongodb --noauth  --port 27017' -s /bin/bash mongodb
  2. Create users

    /usr/local/mongodb/bin/mongo <<<'use admin
    db.createUser({user:"root",pwd:"123456",roles:[{role:"root",db:"admin"}]})
    use MDLicense
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"MDLicense"}]})
    use ClientLicense
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"ClientLicense"}]})
    use commonbase
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"commonbase"}]})
    use MDAlert
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"MDAlert"}]})
    use mdapproles
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdapproles"}]})
    use mdapprove
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdapprove"}]})
    use mdapps
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdapps"}]})
    use mdattachment
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdattachment"}]})
    use mdcalendar
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdcalendar"}]})
    use mdcategory
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdcategory"}]})
    use MDChatTop
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"MDChatTop"}]})
    use mdcheck
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdcheck"}]})
    use mddossier
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mddossier"}]})
    use mdemail
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdemail"}]})
    use mdform
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdform"}]})
    use MDGroup
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"MDGroup"}]})
    use mdgroups
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdgroups"}]})
    use MDHistory
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"MDHistory"}]})
    use mdIdentification
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdIdentification"}]})
    use mdinbox
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdinbox"}]})
    use mdkc
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdkc"}]})
    use mdmap
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdmap"}]})
    use mdmobileaddress
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdmobileaddress"}]})
    use MDNotification
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"MDNotification"}]})
    use mdpost
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdpost"}]})
    use mdreportdata
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdreportdata"}]})
    use mdroles
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdroles"}]})
    use mdsearch
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdsearch"}]})
    use mdservicedata
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdservicedata"}]})
    use mdsms
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdsms"}]})
    use MDSso
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"MDSso"}]})
    use mdtag
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdtag"}]})
    use mdtransfer
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdtransfer"}]})
    use MDUser
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"MDUser"}]})
    use mdworkflow
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdworkflow"}]})
    use mdworksheet
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdworksheet"}]})
    use mdworkweixin
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdworkweixin"}]})
    use mdwsrows
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdwsrows"}]})
    use pushlog
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"pushlog"}]})
    use taskcenter
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"taskcenter"}]})
    use mdintegration
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdintegration"}]})
    use mdactionlog
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdactionlog"}]})
    use mdworksheetlog
    db.createUser({user:"hap",pwd:"123456",roles:[{role:"readWrite",db:"mdworksheetlog"}]})
    use mdworksheetsearch
    db.createUser({user: "hap",pwd: "123456",roles: [{role: "readWrite",db: "mdworksheetsearch"}]})
    use mddatapipeline
    db.createUser({user: "hap",pwd: "123456",roles: [{role: "readWrite",db: "mddatapipeline"}]})
    use mdwfplugin
    db.createUser({user: "hap",pwd: "123456",roles: [{role: "readWrite",db: "mdwfplugin"}]})
    use mdpayment
    db.createUser({user: "hap",pwd: "123456",roles: [{role: "readWrite",db: "mdpayment"}]})'
    • The root user password for the MongoDB admin database specified in the command is 123456. It must be changed to a strong password for actual deployment.
    • The hap user password for MongoDB business databases specified in the command is 123456. It must be changed to a strong password for actual deployment.
    • If the password contains special characters, only - or _ are allowed. Characters like @ ! # & are prohibited to avoid compatibility issues.
  3. Shut down the temporarily started MongoDB

    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

  1. Login to MongoDB

    /usr/local/mongodb/bin/mongo -u root -p 123456 --authenticationDatabase admin
  2. 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.
  3. 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.