Big Data MongoDB replication set Management

Source: Internet
Author: User
Tags mongodb

MongoDB Replica Set
    • MongoDB replication is the process of synchronizing data across multiple servers.

    • Replication provides redundant backups of data, stores copies of data on multiple servers, improves data availability, and guarantees data security.

    • Replication also allows you to recover data from hardware failures and service outages.
Benefits of replication Sets
    • Secure your data
    • Data high Availability (24*7)
    • Disaster recovery
    • No downtime maintenance (e.g. backup, rebuild index, compression)
    • The principle of distributed read Data MongoDB replication

      MongoDB requires a minimum of two nodes for replication. One is the master node, which handles client requests, and the rest is the slave node, responsible for replicating the data on the master node.

MongoDB each node common collocation method is: A master one from, a master many from.

The master node records all operations on it, oplog the primary node periodically from the node, and then performs these operations on its own copy of the data, ensuring that the data from the node is consistent with the primary node.

The MONGODB replication structure diagram looks like this:

In the above structure diagram, the client reads data from the primary node, and when the client writes data to the master node, the master node interacts with the data from the node to ensure data consistency.

To copy a CHI feature:
    • N-node clusters
    • Any node can be used as the master node
    • All write operations are on the primary node
    • Auto Fail-Over
    • Automatic recovery
Installing MongoDB (TAR installation):
    • 1. Setup to create multiple instances:
tar -zxvf mongodb-linux-x86_64-3.2.1.tgz -C /usr/local/cd /usr/local/mv mongodb-linux-x86_64-3.2.1/ mongodb      #重命名mkdir -p /data/mongodb/mongodb{1,2,3,4}     #创建数据目录mkdir -p /data/logstouch /data/logs/mongodb{1,2,3,4}.log       #创建日志文件cd /data/logs/chmod 777 *.log      #赋予权限cd /usr/local/mongodb/binvim mongodb1.confport=27017dbpath=/data/mongodb/mongodb1logpath=/data/logs/mongodb1.loglogappend=truefork=truemaxConns=5000storageEngine=mmapv1replSet=test     #复制集名称------------------------------------------------------------------------------------#下面是yum安装时修改、添加的replication:           #去注释  replSetName: test   #添加复制集名称
ln -s /usr/local/mongodb/bin/mongod /usr/bin/  #把mongodb常用的命令添加到系统命令中ln -s /usr/local/mongodb/bin/mongo /usr/bin/
    • 2. Turn on multi-instance, initialize configuration replica set:
[[email protected] bin]# mongo> cfg={"_id": "Test", "members": [{"_id": 0, "host": "192.168.217.129:27017"},{"_ ID ": 1," host ":" 192.168.217.129:27018 "},{" _id ": 2," host ":" 192.168.217.129:27019 "}]} #配置复制集, note the name of the replica set to be consistent {" _id ":" Test            "," members ": [{" _id ": 0," host ":" 192.168.217.129:27017 "}, { "_id": 1, "host": "192.168.217.129:27018"}, {"_id": 2, "host": "19 2.168.217.129:27019 "}]}> rs.initiate (cfg) #初始化配置时保证从节点没有数据 {" OK ": 1}test:primary> rs.status () #查看        The full state of the copy set {"Set": "Test", .....            {"_id": 0, "name": "192.168.217.129:27017", "Health": 1, "state": 1, "Statestr": "PRIMARY", #27017 port as the primary node "uptime": 1234, "Optime": {"TS  ": Timestamp (1531961046, 1)," T ": Numberlong (1)}, ...},      {"_id": 1, "name": "192.168.217.129:27018", "Health": 1, "state": 2 , "Statestr": "Secondary", #从节点 "uptime": $, "optime": {"ts": Tim Estamp (1531961046, 1), "T": Numberlong (1)}, ...}, {"_ ID ": 2," "Name": "192.168.217.129:27019", "Health": 1, "state": 2, "statest                 R ":" Secondary ", #从节点" uptime ": $," optime ": {" ts ": Timestamp (1531961046, 1),    "T": Numberlong (1)}, ...} ], "OK": 1}
    • 3. Add and Remove nodes:
test:PRIMARY> rs.add("192.168.217.129:27020")     #添加节点{ "ok" : 1 }test:PRIMARY> rs.status()    ........            "_id" : 3,            "name" : "192.168.217.129:27020",            "health" : 1,            "state" : 2,            "stateStr" : "SECONDARY",    ........test:PRIMARY> rs.remove("192.168.217.129:27020")  #删除节点{ "ok" : 1 }test:PRIMARY> rs.status().........
    • 4. Simulate the failure to see if the primary node is automatically switched:
[[email protected] bin]# mongod -f mongodb1.conf --shutdown   #关闭主节点端口killing process with pid: 3552[[email protected] bin]# mongo --port 27018  test:SECONDARY> rs.status(){    "set" : "test",    .......        {            "_id" : 0,            "name" : "192.168.217.129:27017",               "health" : 0,            "state" : 8,            "stateStr" : "(not reachable/healthy)",              "uptime" : 0,            "optime" : {            .......        {            "_id" : 1,            "name" : "192.168.217.129:27018",            "health" : 1,            "state" : 2,            "stateStr" : "SECONDARY",            "uptime" : 1811,            .......        {            "_id" : 2,            "name" : "192.168.217.129:27019",   #自动切换主节点            "health" : 1,            "state" : 1,            "stateStr" : "PRIMARY",            "uptime" : 712,            ......        }    ],    "ok" : 1}
    • 5. Manually switch the master node:
[[email protected] bin]# mongo --port 27019  test:PRIMARY> rs.freeze(30)        #暂停30s不参与选举{ "ok" : 1 }test:PRIMARY> rs.stepDown(60,30)  #交出主节点位置,维持从节点状态不少于60秒,等待30秒使主节点和从节点日志同步test:SECONDARY> rs.status()......
    • 6. Allow data to be read from the node:
test:SECONDARY> show dbs   #2018-07-19T09:04:34.898+0800 E QUERY    [thread1] Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 } :[email protected]/mongo/shell/utils.js:23:13[email protected]/mongo/shell/mongo.js:53:1[email protected]/mongo/shell/utils.js:700:19[email protected]/mongo/shell/utils.js:594:15@(shellhelp2):1:1test:SECONDARY> rs.slaveOk()    #允许默认从节点读取数据test:SECONDARY> show dbslocal  1.078GB
    • 7. Change the Oplog size:
test:PRIMARY> use localswitched to db localtest:PRIMARY> rs.printReplicationInfo()  #查看日志文件能够使用的大小 默认oplog大小会占用64位实例5%的可用磁盘空间configured oplog size:   95.37109375MBlog length start to end: 1103secs (0.31hrs)oplog first event time:  Thu Jul 19 2018 08:43:55 GMT+0800 (CST)oplog last event time:   Thu Jul 19 2018 09:02:18 GMT+0800 (CST)now:                     Thu Jul 19 2018 09:20:08 GMT+0800 (CST)test:PRIMARY> db.runCommand({"convertToCapped":"oplog.rs","size":10000000000}) #修改  单位:B{ "ok" : 1 }test:PRIMARY> rs.printReplicationInfo()configured oplog size:   9536.746032714844MBlog length start to end: 1103secs (0.31hrs)oplog first event time:  Thu Jul 19 2018 08:43:55 GMT+0800 (CST)oplog last event time:   Thu Jul 19 2018 09:02:18 GMT+0800 (CST)now:                     

Big Data MongoDB replication set Management

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.