replica set应该有奇数个成员,这样可以让选举正常进行。需要在每个机器上安装mongodb,replica set成员的网络必须是可通的,下面是测试的方式
EXAMPLE
Given a replica set with three members running on three separate hosts:
m1.example.net
m2.example.net
m3.example.net
Test the connection from m1.example.net to the other hosts with the following operation set m1.example.net:
mongo –host m2.example.net –port 27017
mongo –host m3.example.net –port 27017
Test the connection from m2.example.net to the other two hosts with the following operation set from m2.example.net, as in:
mongo –host m1.example.net –port 27017
mongo –host m3.example.net –port 27017
You have now tested the connection between m2.example.net and m1.example.net in both directions.
Test the connection from m3.example.net to the other two hosts with the following operation set from the m3.example.net host, as in:
mongo –host m1.example.net –port 27017
mongo –host m2.example.net –port 27017
在/etc/mongod.conf中配置指定存储数据的位置
1启动replica set中的每个成员
对每个成员,启动mongod,指定replica set名称
mongod –replSet “rs0”
也可以在控制文件中指定replica set的名字,然后用控制文件启动
mongod –config $HOME/.mongodb/config
2连接replica set 成员
3初始化replica set
只在一个成员上使用rs.initiate()初始化
4验证replica set配置
rs.conf()
5添加其他成员到replica set中
rs.add(“mongodb1.example.net”)
这个命令在一些情况下会触发一次选举,如果连接的mongod变成了secondary,你就需要使用mongoshell 连接到新的primary,在继续添加成员,可以使用rs.status() 来标识primary
6查看
rs.status()