Build a highly available MongoDB cluster replica set

Source: Internet
Author: User
Tags mongodb commands

What is a replica set? Play World of Warcraft always say to hit a copy, actually these two concepts almost one meaning. The copy of the game refers to the player concentrated in the peak time to go to a scene to play strange, there will be more than the situation of mob mobs, game developers in order to ensure the player's experience, for each batch of players to open a separate space for the same amount of monsters, this is a copy of the scene is a copy, No matter how many players play in their respective copies, they do not affect each other.


The copy of MongoDB is also this, the master-slave mode is actually a single copy of the application, not very good extensibility and fault tolerance. The replica set has multiple replicas to ensure fault tolerance, even if a copy hangs up there are many replicas exist, and resolves the first problem above "The primary node is dead, the entire cluster will automatically switch".

###### #原理和工作机制 #########

The primary server is responsible for reading and writing the entire replica set, the replica set periodically synchronizes data backup, one but the master node hangs, the replica node will elect a new primary server, the replica node in the copy set after the main node is hung off after the heartbeat mechanism detected, will be in the cluster initiated the main node election mechanism, automatically elect a new primary server.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/38/13/wKiom1OyWc6BDxW8AAFVmwZWFD0652.jpg "title=" Qq20140701100006.png "alt=" Wkiom1oywc6bdxw8aafvmwzwfd0652.jpg "/>



################################# #部署副本集 ####################################

Replica set machines are at least 3 units

10.2.16.250 Master Node

10.2.16.254 secondary Node

10.2.16.251 Arbiter Server

2. Set up MongoDB copy set Test folder on each machine respectively


#存放整个mongodb文件

Mkdir-p/data/mongodbtest/replset


#存放mongodb数据文件

Mkdir-p/data/mongodbtest/replset/data


#进入mongodb文件夹

Cd/data/mongodbtest

3. Download the MongoDB install package


wget <a href= "http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.8.tgz" >http://fastdl.mongodb.org/ Linux/mongodb-linux-x86_64-2.4.8.tgz</a>

#解压下载的压缩包

[[Email protected] ~] #tar xvzf mongodb-linux-x86_64-2.4.8.tgz


[[Email protected] ~] #mv mongodb-linux-x86_64-2.48/usr/local/mongodb/


Environment variables for adding MONGODB commands

[[Email protected] ~] #vim/etc/profile

[[Email protected] ~] #export path=/usr/local/mongodb/bin: $PATH


Start MongoDB, in both servers and from the same operation:

[[Email protected] ~] #mongod--dbpath/data/mongodbtest/replset/data--replset repset (replica set name)

You can see that the replica set is displayed on the console and that initialization information is not configured.


Sun Dec 20:12:02.953 [Rsstart] replset can ' t get local.system.replset config from/any seed (emptyconfig)

Sun Dec 20:12:02.953 [Rsstart] replset info need to run replsetinitiate--rs.initiate () in the shell--if th At isn't already done

5. Initialize the replica set


Any machine on three machines landed on MongoDB


[[Email protected] ~] #mongo


#使用admin数据库

Use admin

#定义副本集配置变量, here's _id: "Repset" and the above command parameter "–replset Repset" to remain the same.


Config = {_id: "Repset", members:[

... {_id:0,host: "10.2.16.250:27017"}, #id为0表示设置此IP为主服务器

... {_id:1,host: "10.2.16.254:27017"},]#id为1表示设置为备服务器, if you want to add a server again, set the ID to 2, and so on.

... }

#输出

{

"_id": "Repset",

"Members": [

{

"_id": 0,

"Host": "10.2.16.250:27017"

},

{

"_id": 1,

"Host": "10.2.16.254:27017"

}

]

}

#初始化副本集配置


Rs.initiate (config);

#输出成功


[Plain] View plaincopy

{

"Info": "Config now saved locally. Should come online in about a minute. ",

"OK": 1

}

#查看mongodb输出日志, after the replica set starts successfully, 250 primary,254 is the replica node for the primary node secondary



#查看集群节点的状态 Rs.status ();

Repset:primary> Rs.status ();

{

"Set": "Repset",

"Date": Isodate ("2014-06-30t10:03:22z"),

"MyState": 1,

"Members": [

{

"_id": 0,

"Name": "10.2.16.250:27017",

"Health": 1,

"State": 1,

"Statestr": "PRIMARY",

"Uptime": 961,

"Optime": Timestamp (1404122493, 1),

"Optimedate": Isodate ("2014-06-30t10:01:33z"),

"Self": true

},

{

"_id": 1,

"Name": "10.2.16.254:27017",

"Health": 1,

"State": 2,

"Statestr": "Secondary",

"Uptime": 106,

"Optime": Timestamp (1404122493, 1),

"Optimedate": Isodate ("2014-06-30t10:01:33z"),

"Lastheartbeat": Isodate ("2014-06-30t10:03:20z"),

"Lastheartbeatrecv": Isodate ("2014-06-30t10:03:21z"),

"Pingms": 2,

"Syncingto": "10.2.16.250:27017"

}

],

"OK": 1

}

6. Test copy Set data copy function


#在主节点10.2.16.250 connected to the terminal:

[[Email protected] ~] #mongo


#建立test database.

Repset:primary>use test;


Insert data toward the TestDB table.

Repset:primary>> Db.testdb.insert ({"Test1": "Testval1"})


#在副本节点10.2.16.254 connect to MongoDB to see if the data is copied over.


[[Email protected] ~] #mongo


#使用test database.

repset:secondary> use test;


Repset:secondary> Show tables;


#输出

[Plain] View plaincopy

Sun Dec 21:50:48.590 Error: {"$err": "Not Master and Slaveok=false", "Code": 13435} at src/mongo/shell/query.js:128

#设置副本节点可以读: MongoDB reads and writes data from the master node by default, is not allowed on the replica node, and needs to be set to read

Repset:secondary> Db.getmongo (). Setslaveok ();


#可以看到数据已经复制到了副本集.

Repset:secondary> Db.testdb.find ();

[Plain] View plaincopy

#输出

{"_id": ObjectId ("52c028460c7505626a93944f"), "test1": "Testval1"}

To add an arbiter server:

Now the arbiter server 10.2.16.251 the MongoDB, and then:


#建立仲裁文件夹

Mkdir/data/arb


#启动仲裁

Mongod--port 30000--dbpath/data/arb--replset repset (replica set name)


To add a quorum on the primary server

Repset:primary> Rs.addarb ("10.2.16.251:30000")

{"OK": 1}

7. Test the replica set failover function


First stop the master node MongoDB 250, view the quorum server and 254 logs from the server can be seen through a series of selection operations, 254 elected master node.




To view the status of the entire cluster:


Mongo


Repset:primary> Rs.status ();


#输出


{

"Set": "Repset",

"Date": Isodate ("2014-06-30t13:58:06z"),

"MyState": 1,

"Members": [

{

"_id": 0,

"Name": "10.2.16.250:27017",

"Health": 1,

"State": 1,

"Statestr": "PRIMARY",

"Uptime": 6095,

"Optime": Timestamp (1404131596, 1),

"Optimedate": Isodate ("2014-06-30t12:33:16z"),

"Self": true

},

{

"_id": 1,

"Name": "10.2.16.254:27017",

"Health": 1,

"State": 2,

"Statestr": "Secondary",

"Uptime": 5542,

"Optime": Timestamp (1404131596, 1),

"Optimedate": Isodate ("2014-06-30t12:33:16z"),

"Lastheartbeat": Isodate ("2014-06-30t13:58:06z"),

"Lastheartbeatrecv": Isodate ("2014-06-30t13:58:05z"),

"Pingms": 0,

"Syncingto": "10.2.16.250:27017"

},

{

"_id": 2,

"Name": "10.2.16.251:30000",

"Health": 1,

"State": 7,

"Statestr": "Arbiter",

"Uptime": 11,

"Lastheartbeat": Isodate ("2014-06-30t13:58:05z"),

"Lastheartbeatrecv": Isodate ("2014-06-30t13:58:06z"),

"Pingms": 8

}

],

"OK": 1

}

When the 250 node is started, the replica set is automatically added and changed back to the primary node.


################### #至此即为配置成功!! #########################

A total of five status parameters: Primary, primarypreferred, secondary, secondarypreferred, nearest.

Primary: The default parameter, only read from the primary node;

Primarypreferred: Most of the data is read from the primary node, and the data is read from the secondary node only when the primary node is unavailable.

Secondary: Only read from the secondary node, the problem is that the data of the secondary node is "old" than the primary node data.

Secondarypreferred: read from the secondary node, and read data from the master node when the secondary node is unavailable;

Nearest: Reads data from the node with the lowest network latency, whether it is the primary node or the secondary node.


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.