mongodb replaceSet

./mongod --dbpath /data/mongodb --fork --port 27017 --logpath /data/log/mongodb.log --pidfilepath /usr/local/mongodb/mongo.pid  --rest --replSet myset
./mongod --dbpath /data/mongodb1 --fork --port 27018 --logpath /data/log/mongodb1.log --pidfilepath /usr/local/mongodb1/mongo.pid --rest --replSet myset
./mongod --dbpath /data/mongodb2 --fork --port 27019 --logpath /data/log/mongodb2.log --pidfilepath /usr/local/mongodb2/mongo.pid --rest --replSet myset
 
 

--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={

    _id : "myset",
    members : [
        {_id : 0, host : "10.0.10.24:27017"},
        {_id : 1, host : "10.0.10.24:27018"},
        {_id : 2, host : "10.0.10.24:27019",arbiterOnly:true}
    ]
};
 
rs.initiate(config)
 

----------------------------------------------

需要修改时

 config={
    _id : "myset",
    members : [
        {_id : 0, host : "10.0.10.24:27017"},
        {_id : 1, host : "10.0.10.24:27018"},
        {_id : 2, host : "10.0.10.24:27019",arbiterOnly:true}
    ]
};
 
db = db.getSisterDB("local")
 
old_config = db.system.replset.findOne();
#不+1  会报版本错误
config.version = old_config.version + 1;  
 
 
 
 
db = db.getSisterDB("admin")
db.runCommand({ replSetReconfig : config });
 
 

printjson(db.getLastErrorObj())

-------------------------------------------------

执行脚本 ./mongo init.js

>rs.conf()

{

        "_id" : "myset",
        "version" : 3,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "api:27017"
                },
                {
                        "_id" : 1,
                        "host" : "10.0.10.24:27018"
                },
                {
                        "_id" : 2,
                        "host" : "10.0.10.24:27019",
                        "arbiterOnly" : true
                }
        ]

}

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:10:42 [ReplSetHealthPollTask] replSet member 10.0.10.24:27018 RECOVERING
Thu Dec 15 11:10:42 [ReplSetHealthPollTask] replSet info api:27017 is up
Thu Dec 15 11:10:42 [ReplSetHealthPollTask] replSet member api:27017 PRIMARY

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:21:19 [ReplSetHealthPollTask] DBClientCursor::init call() failed
Thu Dec 15 11:21:19 [ReplSetHealthPollTask] replSet info api:27017 is down (or slow to respond): DBClientBase::findOne: transport error: api:27017 query: { replSetHeartbeat: "myset", v: 3, pv: 1, checkEmpty: false, from: "10.0.10.24:27019" }
Thu Dec 15 11:21:20 [conn3] replSet info voting yea for 1
Thu Dec 15 11:21:21 [ReplSetHealthPollTask] replSet member 10.0.10.24:27018 PRIMARY
Thu Dec 15 11:22:07 [initandlisten] connection accepted from 10.0.10.24:63165 #6
Thu Dec 15 11:22:07 [ReplSetHealthPollTask] replSet info api:27017 is up

Thu Dec 15 11:22:07 [ReplSetHealthPollTask] replSet member api:27017 SECONDARY

Set name:myset
Majority up:yes
api:27017 (me)018.5 mins11SECONDARY4ee96793:1
10.0.10.24:27018118.5 mins1 sec ago11PRIMARY4ee96793:1
10.0.10.24:27019218.5 mins1 sec ago11ARBITER0:0

备份脚本

#export mongodb 

echo "start"
pid=`cat /usr/local/mongodb/mongo.pid`
if [ "$pid" = "" ];then
   echo "server is stoped"
exit 0
fi
prefix=`date +%Y%m%d`
file="/data/mongobak/test.$prefix"
#exportcmd="/usr/local/mongodb/bin/mongodump -d test -o $file"
#$exportcmd

echo "end"

恢复脚本

d=`ls -l -t /data/mongobak/ |awk 'NR==2 {print $9;exit}'`

file="/data/mongobak/$d/test"
 
if [ ! -d $file ];then
 echo "file not exist!"
fi
 
for f in $file/* ;do
echo $f
done
cmd="/usr/local/mongodb/bin/mongorestore -d test $file"
$cmd
 

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.

mongodb replaceSetIt 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.mongodb replaceSet

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
mongodb replaceSetthe --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
}
>
mongodb replaceSetInitializing 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).

相关推荐