mongodb replaceSet
--replSet myset 指定mongodb运行为replication模式,集合名为myset
--rest 用于启动adminstration ui,可以通过http://localhost:28017/_replSet查看服务器状态
--dbpath 指定数据库文件路径
--logpath 指定日志文件路径 /dev/null
./mongo
方式一 (有时候会报错,不建议)
---------------------------------
>rs.initiate()
{ "info2" : "no configuration explicitly specified -- making one", "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1 }
>rs.add("10.0.10.24:27018")
> rs.addArb("10.0.10.24:27019") //只做为选举
----------------------------
方式二
init.js
config={
----------------------------------------------
需要修改时
printjson(db.getLastErrorObj())
-------------------------------------------------
执行脚本 ./mongo init.js
>rs.conf()
{
}
27017状态
replSet member api:27017 PRIMARY
27018状态
replSet member 10.0.10.24:27018 SECONDARY
27019 状态
Thu Dec 15 11:10:42 [ReplSetHealthPollTask] replSet info 10.0.10.24:27018 is up
Thu Dec 15 11:11:22 [ReplSetHealthPollTask] replSet member 10.0.10.24:27018 SECONDARY
当17 挂掉 19选举 18 启动为 PRIMARY
Thu Dec 15 11:21:18 [conn4] end connection 10.0.10.24:11516
Thu Dec 15 11:22:07 [ReplSetHealthPollTask] replSet member api:27017 SECONDARY
Set name: | myset |
Majority up: | yes |
api:27017 (me) | 0 | 1 | 8.5 mins | 1 | 1 | SECONDARY | 4ee96793:1 | |||
10.0.10.24:27018 | 1 | 1 | 8.5 mins | 1 sec ago | 1 | 1 | PRIMARY | 4ee96793:1 | ||
10.0.10.24:27019 | 2 | 1 | 8.5 mins | 1 sec ago | 1 | 1 | ARBITER | 0:0 |
备份脚本
#export mongodb
echo "end"
恢复脚本
d=`ls -l -t /data/mongobak/ |awk 'NR==2 {print $9;exit}'`
echo "db restored!"
java客户端
---------------------------官方文档
Minimum Configuration
For production use you will want a minimum of three nodes in the replica set.
Either:
- 2 full nodes and 1 arbiter
- 3 full nodes
To avoid a single point of failure, these nodes must be on different computers.
It is standard to have at least 2 nodes equipped to handle primary duties. |
Basic Configuration
- Replica sets typically operate with 2 to 7 full nodes and possibly an arbiter.
- Within a given set, there should be an odd number of total nodes.
- If full nodes are only available in even numbers, then an arbiter should be added to provide an odd number.
- The arbiter is a lightweight mongod process whose purpose is to break ties when electing a primary.
- The arbiter does not typically require a dedicated machine.
Example: 3 full servers
In this example, three servers are connected together in a single cluster. If the primary fails any of the secondary nodes can take over.
Getting Started – A sample session
The following is a simple configuration session for replica sets.
For this example assume a 3-node Replica set with 2 full nodes and one arbiter node. These servers will be named sf1, sf2 and sf3.
Step 1: Start mongod with --replSet
On each of the servers start an instance of the mongo daemon:
sf1$ mongod --rest --replSet myset sf2$ mongod --rest --replSet myset sf3$ mongod --rest --replSet myset
the --replSet parameter has the same value myset on all three instances. |
Step 1a: Check the Replication UI (optional)
Visit http://sf1:28017/_replSet, this will give you an idea on the current status of the replica set. As you proceed through the remaining steps, refreshing this dashboard to see changes. See the docs here for more details.
Step 2: Initiate the replica set
Connect to mongo on sf1.
$ mongo --host sf1 > rs.initiate() { "info2" : "no configuration explicitly specified -- making one", "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1 } >
Initializing the replica set this way will cause the replica set to use the hostname of the current server in the replica set configuration. If your hostnames are not known to all mongo and application servers, you may need to initialize the hosts explicitly - see Replica Set Configuration for more details. |
Step 3: Add nodes to the replica set
$ mongo --host sf1 > rs.add(“sf2”) { “ok” : 1 } > rs.addArb(“sf3”) { “ok” : 1 }
Any operations that change data will now be replicated from sf1 to sf2.
If sf1 is shut down, you will see sf2 take over as primary
Changing Client Code
- To leverage replica sets from your client code, you will need to modify your client connection code.
- The details for doing this will vary with each driver (language).