Skip to main content

副本集

服务器IP主机角色
192.168.10.4MongoDB Replica Set Node01
192.168.10.5MongoDB Replica Set Node02
192.168.10.6MongoDB Replica Set Node03

开始部署

MongoDB Replica Set Node01

  1. 下载 mongodb 安装包并解压到安装目录

    wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.29.tgz
    tar -zxvf mongodb-linux-x86_64-rhel70-4.4.29.tgz
    mv mongodb-linux-x86_64-rhel70-4.4.29 /usr/local/mongodb
  2. 创建 mongodb 用户

    useradd -M -s /sbin/nologin mongodb
  3. 创建数据、日志目录并授予权限

    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
  4. 配置 systemd 管理文件

    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 --bind_ip 0.0.0.0
    ExecStop=/usr/bin/kill \$MAINPID
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target
    EOF
  5. 加入开机自启动

    # 安装后还没创建用户,所以先不用 systemctl start mongodb 启动服务
    systemctl daemon-reload
    systemctl enable mongodb

MongoDB Replica Set Node02

  1. 下载 mongodb 安装包并解压到安装目录

    wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.29.tgz
    tar -zxvf mongodb-linux-x86_64-rhel70-4.4.29.tgz
    mv mongodb-linux-x86_64-rhel70-4.4.29 /usr/local/mongodb
  2. 创建 mongodb 用户

    useradd -M -s /sbin/nologin mongodb
  3. 创建数据、日志目录并授予权限

    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
  4. 配置 systemd 管理文件

    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 --bind_ip 0.0.0.0
    ExecStop=/usr/bin/kill \$MAINPID
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target
    EOF
  5. 加入开机自启动

    # 安装后还没创建用户,所以先不用 systemctl start mongodb 启动服务
    systemctl daemon-reload
    systemctl enable mongodb

MongoDB Replica Set Node03

  1. 下载 mongodb 安装包并解压到安装目录

    wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.29.tgz
    tar -zxvf mongodb-linux-x86_64-rhel70-4.4.29.tgz
    mv mongodb-linux-x86_64-rhel70-4.4.29 /usr/local/mongodb
  2. 创建 mongodb 用户

    useradd -M -s /sbin/nologin mongodb
  3. 创建数据、日志目录并授予权限

    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
  4. 配置 systemd 管理文件

    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 --bind_ip 0.0.0.0
    ExecStop=/usr/bin/kill \$MAINPID
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target
    EOF
  5. 加入开机自启动

    # 安装后还没创建用户,所以先不用 systemctl start mongodb 启动服务
    systemctl daemon-reload
    systemctl enable mongodb

创建数据库用户

在 MongoDB Replica Set Node01 节点上操作

  1. 临时启动一个未开启连接认证的 mongodb 服务

    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. 创建用户

    /usr/local/mongodb/bin/mongo <<<'use admin
    db.createUser({user:"root",pwd:"12345678",roles:[{role:"root",db:"admin"}]})
    use MDLicense
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"MDLicense"}]})
    use ClientLicense
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"ClientLicense"}]})
    use commonbase
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"commonbase"}]})
    use MDAlert
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"MDAlert"}]})
    use mdapproles
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdapproles"}]})
    use mdapprove
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdapprove"}]})
    use mdapps
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdapps"}]})
    use mdattachment
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdattachment"}]})
    use mdcalendar
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdcalendar"}]})
    use mdcategory
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdcategory"}]})
    use MDChatTop
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"MDChatTop"}]})
    use mdcheck
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdcheck"}]})
    use mddossier
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mddossier"}]})
    use mdemail
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdemail"}]})
    use mdform
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdform"}]})
    use MDGroup
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"MDGroup"}]})
    use mdgroups
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdgroups"}]})
    use MDHistory
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"MDHistory"}]})
    use mdIdentification
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdIdentification"}]})
    use mdinbox
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdinbox"}]})
    use mdkc
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdkc"}]})
    use mdmap
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdmap"}]})
    use mdmobileaddress
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdmobileaddress"}]})
    use MDNotification
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"MDNotification"}]})
    use mdpost
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdpost"}]})
    use mdreportdata
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdreportdata"}]})
    use mdroles
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdroles"}]})
    use mdsearch
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdsearch"}]})
    use mdservicedata
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdservicedata"}]})
    use mdsms
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdsms"}]})
    use MDSso
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"MDSso"}]})
    use mdtag
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdtag"}]})
    use mdtransfer
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdtransfer"}]})
    use MDUser
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"MDUser"}]})
    use mdworkflow
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdworkflow"}]})
    use mdworksheet
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdworksheet"}]})
    use mdworkweixin
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdworkweixin"}]})
    use mdwsrows
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdwsrows"}]})
    use pushlog
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"pushlog"}]})
    use taskcenter
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"taskcenter"}]})
    use mdintegration
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdintegration"}]})
    use mdactionlog
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdactionlog"}]})
    use mdworksheetlog
    db.createUser({user:"mingdao",pwd:"123456",roles:[{role:"readWrite",db:"mdworksheetlog"}]})
    use mdworksheetsearch
    db.createUser({user: "mingdao",pwd: "123456",roles: [{role: "readWrite",db: "mdworksheetsearch"}]})
    use mddatapipeline
    db.createUser({user: "mingdao",pwd: "123456",roles: [{role: "readWrite",db: "mddatapipeline"}]})
    use mdwfplugin
    db.createUser({user: "mingdao",pwd: "123456",roles: [{role: "readWrite",db: "mdwfplugin"}]})'
    • 命令中指定的 mongodb admin 库权限 root 用户密码为 12345678 ,实际部署时注意替换
    • 命令中指定的 mongodb 业务库权限 mingdao 用户密码为 123456 ,实际部署时注意替换
  3. 关闭临时启动的 MongoDB

    kill $(pgrep -f 'mongod')

启动 MongoDB

三台部署 mongodb 的服务器上操作

systemctl start mongodb

配置副本集

在 MongoDB Replica Set Node01 节点上操作

  1. 登录 mongodb

    /usr/local/mongodb/bin/mongo -u root -p 12345678 --authenticationDatabase admin
  2. 配置副本集

    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"} ]})
    • rs.initiate 语句中的各节点IP,在实际部署时注意替换
  3. 确认副本集状态

    rs.status()

    # 输出结果中,正常为一个节点 PRIMARY,两个 SECONDARY 状态