MongoDB High-availability deployment

Source: Internet
Author: User

Plan your deployment as follows:

650) this.width=650; "src=" Http://www.wangerbao.com/wp-content/uploads/2017/04/20170412184145_91477.png "style=" border:0px; "/>

1. Prepare the basic Environment

download mongodb-linux-x86_64-rhel62-3.4.2.gz on the Internet to each server, unzip and put in the corresponding directory, and then in the root directory of the new data folder for the unified storage of data and configuration information.

2, build configuration cluster 243-244-245

Create a new start script in the root directory of MONGO, as follows

Bin/./mongod--dbpath data/--logpath data/mongo.log--configsvr--fork--port 20243--replSet Config

Where CONFIGSVR represents the configuration service, fork means background boot, Replset represents the cluster, config represents the cluster name

Also on other configuration services to establish a corresponding start boot file, remember to modify the port Oh.

Start-up-3 units are started

./start

Enter a machine, enter MONGO, run the following command

>config = {_id: "config", Configsvr:true, members:[{_id:0,host: ' 172.16.13.243:20243 '}, {_id:1,host: ' 172.16.13.244:20244 '}, {_id:2,host: ' 172.16.13.245:20245 '}]} {"_id": "config", "configsvr": True, "members": [{"_id": 0 , "host": "172.16.13.243:20243"},{"_id": 1, "host": "172.16.13.244:20244"},{"_id": 2, "host": "172.16.13.245:20245"}]} > rs.initiate (config) {"OK": 1}

View cluster status

Config:primary> rs.status (); {"Set"  :  "config", "date"  : isodate ("2017-04-12t09:29:51.889z"), "MyState"  : 1, " term " : numberlong (1)," Configsvr " : true," Heartbeatintervalmillis " : numberlong ( "Optimes"  : {"Lastcommittedoptime"  : {"ts"  : timestamp (1491989382, 1) , "T"  : numberlong (1)}, "Readconcernmajorityoptime"  : {"ts"  : timestamp (1491989382 ,  1), "T"  : numberlong (1)}, "Appliedoptime"  : {"ts"  : timestamp (1491989382,  1), "T"  : numberlong (1)}, "Durableoptime"  : {"ts"  : timestamp (1491989382,  1), "T"  : numberlong (1)}, "members"  : [{"_id"  : 0, "name"  :  " 172.16.13.243:20243 "," Health " : 1," state " : 1," Statestr " : " PRIMARY "," uptime "  : 621, "Optime"  : {"ts"  : timestamp (1491989382, 1), "T"  :&nbSp Numberlong (1)}, "Optimedate"  : isodate ("2017-04-12t09:29:42z"), "InfoMessage"  :  "could  Not find member to sync from "," Electiontime " : timestamp (1491989360, 1 ), "Electiondate"  : isodate ("2017-04-12t09:29:20z"), "ConfigVersion"  : 1, "self"  :  true},{"_id"  : 1, "name"  :  "172.16.13.244:20244", "Health"  : 1, "state"  :  2, "Statestr"  :  "secondary", "uptime"  : 42, "Optime"  : {"ts"  :  Timestamp (1491989382, 1), "T"  : numberlong (1)}, "Optimedurable"  : {"ts"  :  Timestamp (1491989382, 1), "T"  : numberlong (1)}, "Optimedate"  : isodate ("2017-04-12t09 : 29:42z ")," Optimedurabledate " : isodate (" 2017-04-12t09:29:42z ")," Lastheartbeat " : ISODate ( "2017-04-12t09:29:50.238z"), "Lastheartbeatrecv"  : isodate ("2017-04-12t09:29:51.467z"), "PingMs"   :  numberlong (0),"Syncingto"  :  "172.16.13.243:20243", "ConfigVersion"  : 1},{"_id"  : 2, "name"   :  "172.16.13.245:20245", "Health"  : 1, "state"  : 2, "Statestr"  :  "secondary", " Uptime " : 42," Optime " : {" ts " : timestamp (1491989382, 1)," T " :  Numberlong (1)}, "Optimedurable"  : {"ts"  : timestamp (1491989382, 1), "T"  :  Numberlong (1)}, "Optimedate"  : isodate ("2017-04-12t09:29:42z"), "Optimedurabledate"  :  Isodate ("2017-04-12t09:29:42z"), "Lastheartbeat"  : isodate ("2017-04-12t09:29:50.237z"), " Lastheartbeatrecv " : isodate (" 2017-04-12t09:29:51.537z ")," Pingms " : numberlong (0)," Syncingto " : " 172.16.13.243:20243 "," ConfigVersion " : 1}]," OK " : 1}

3. Build a shard cluster 246-247

Create a new start script in the root directory of the MONGO 246 machine, as follows

[[email protected] mongodb-32246]# cat start Bin/./mongod--dbpath data/--logpath data/mongo.log--fork--port 32246--re Plset 246--shardsvr

Replset represents the cluster, SHARDSVR represents the Shard, 246 represents the name of the cluster, and in the other MONGO directory of 246, the corresponding start script is established, remember to modify the port and start the script.

Run the following command

Config = {... _id: ' 246 ',... members:[... {_id:0,host: ' 172.16.13.246:30246 '},... {_id:1,host: ' 172.16.13.246:31246 '},... {_id:2,host: ' 172.16.13.246:32246 '} ...] ... }  {"_id": "246", "members": [{"_id": 0, "host": "172.16.13.246:30246"},{"_id": 1, "host": "172.16.13.246:31246"},{"_id": 2, "host": "172.16.13.246:32246"}]}> rs.initiate (config) {"OK": 1}

View cluster status

This is not listed here, the same operation is performed on 247, remember to modify the cluster name


4, Build MONGOs

Create a start script in the MONGOs root directory of 241

[email protected] mongodb-3.4.2]# cat start Bin/./mongos--logpath data/mongo.log--fork--configdb config/172.16.13.24 3:20243,172.16.13.244:20244,172.16.13.245:20245

Enter MONGOs's command interface,

Adding shards

Mongos> Sh.addshard ("246/172.16.13.246:30246,172.16.13.246:31246,172.16.13.246:32246") {"shardAdded": "246", " OK ": 1}mongos> sh.addshard (" 247/172.16.13.247:30247,172.16.13.247:31247,172.16.13.247:32247 ") {" shardAdded ":" 247 "," OK ": 1}

The above command is also run on 242.

Set the Test.aj table to allocate data according to the _ID field in hash format

Mongos> sh.enablesharding ("test") {"OK": 1}mongos> sh.shardcollection ("Test.aj", {_id: ' hashed '}) {" Collectionsharded ":" Test.aj "," OK ": 1}

5. Inserting data

for (var id = 1; ID <= 10000; id++) Db.aj.save ({"_class": "Java.util.HashMap", "CAh": "123", "CCBRXM": "Wangchy" , "valid": 0, "FYDM": "2400", "CAJMC": "Ptest", "CCBR": "Ptest", "Wcreatetime": Isodate ("2015-10-13t18:29:06.649 Z ")});

6. Verification Results

246 Master Node Validation data volume

246:primary> Db.aj.count (); 5082

246 child nodes Verify the amount of data

246:secondary> use testswitched to DB test246:secondary> Db.getmongo (). Setslaveok ();246:secondary> Db.aj.count (); 5082

247 Repeat the above operation

247:primary> db.aj.count ();4918247:secondary> use test;switched to DB test247:secondary> Db.getmongo (). Setslaveok ();247:secondary> db.aj.count (); 4918



This article is from the "www.wangerbao.com" blog, make sure to keep this source http://wangerbao.blog.51cto.com/4363165/1915341

MongoDB High-availability deployment

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.