`
litianyi520
  • 浏览: 41887 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

mongodb replaceSet

    博客分类:
  • java
 
阅读更多

 

./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

 

Member id Up cctimeLast heartbeat Votes Priority State Messages optimeskew
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 "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.

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 sf1sf2 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).

分享到:
评论

相关推荐

    Linux安装mongodb客户端

    sudo vim /etc/yum.repos.d/mongodb-org-4.2.repo 写入: [mongodb-org-4.2] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/ gpgcheck=1 enabled=1 gpg...

    mongodb-linux-x86_64-4.0.18.tgz

    mv mongodb-linux-x86_64-4.0.18 mongodb 3、进入 mongodb 目录创建目录 db 和 logs cd /usr/local/mongodb mkdir db mkdir logs 4、进入到 bin 目录下,编辑 mongodb.conf 文件,内容如下: dbpath=/usr/local/...

    MongoDB应用设计模式

    资源名称:MongoDB应用设计模式内容简介:无论是在构建社交媒体网站,还是在开发一个仅在内部使用的企业应用程序,《MongoDB应用设计模式》展示了MongoDB需要解决的商业问题之间的连接。你将学到如何把MongoDB设计...

    如何安装MongoDB 如何使用MongoDB

    本课程是一套关于MongoDB应用开发的实战性教程,名为《深入浅出MongoDB应用实战开发(基础、开发指南、系统管理、集群及系统架构)》,教程侧重于讲解MongoDB的常用特性及高级特性,从实际开发的角度出发对MongoDB...

    基于MongoDB的日志系统Mongodb-Log.zip

    mongodb-log 是一个基于MongoDB的Python日志系统。 MongoDB 的 Capped Collection是一个天生的日志系统,MongoDB自己的oplog就是用它来存储的,Capped Collection的特点是可以指定Collection的大小,当记录总大小...

    MongoDB(mongodb-org-server_5.0.4_amd64.deb)

    MongoDB Community Server(mongodb-org-server_5.0.4_amd64.deb)适用于适用于Debian10 MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。 MongoDB是...

    MongoDB笔记.docx

    一、MongoDB简介 3 二、MongoDB结构 3 二、MongoDB 数据库关系型(这里并不是值关系型数据库的关系) 3 1、MongoDB一对一关系型 3 2、MongoDB一对多关系型 4 3、MongoDB多对多关系型 4 三、创建数据库(mongodb_test...

    MongoDB(mongodb-src-r5.0.4.tar.gz)

    MongoDB Community Server(mongodb-src-r5.0.4.tar.gz)源代码 MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。 MongoDB是一个介于关系数据库和非...

    【BAT必备】MongoDB面试题

    【BAT必备】MongoDB面试题【BAT必备】MongoDB面试题【BAT必备】MongoDB面试题【BAT必备】MongoDB面试题【BAT必备】MongoDB面试题【BAT必备】MongoDB面试题【BAT必备】MongoDB面试题【BAT必备】MongoDB面试题【BAT...

    MongoDB Community(mongodb-linux-aarch64-ubuntu1804-5.0.8.tgz)

    MongoDB Community Server(mongodb-linux-aarch64-ubuntu1804-5.0.8.tgz)适用于Ubuntu 18.04 Arm芯片, MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决...

    mongodb c#驱动最新驱动mongodb.driver.dll 版本2.12.0-beta1

    mongodb c#驱动 最新驱动mongodb.driver.dll 版本2.12.0-beta1,包内有MongoDB.Bson.dll 、mongodb.driver.core.dll 等文件

    MongoDB4.2分片及副本集群搭建

    MongoDB4.2分片及副本集群搭建 MongoDB集群 MongoDB分片 MongoDB副本 MongoDB副本集群

    MongoDB图形化管理工具 MongoDB Compass

    MongoDB图形化管理工具 MongoDB Compass

    五、MongoDB 学习PPT

    MongoDB 学习PPT

    MongoDB教程基础入门

    教程名称:MongoDB教程基础入门 课程目录:【】MongoDB教程基础入门-代码【】MongoDB教程基础入门01第一讲上【】MongoDB教程基础入门02第一讲下【】MongoDB教程基础入门03第二讲上【】MongoDB教程基础入门04第二讲...

    MongoDB_3.8.2驱动jar包及其同版本依赖包bson和mongodb-driver-core

    java和mongodb连接,需要mongodb-driver,您还必须下载其依赖项: bson和 mongodb-driver-core》》3个包: mongodb-driver-3.8.2.jar; bson-3.8.2.jar; mongodb-driver-core-3.8.2.jar

    mongodb-v4.0

    mongodb-win32-x86_64-2008plus-ssl-v4.0-latest-signed.msi

    spring-data使用mongodbTemplate对MongoDB进行读写操作

    spring data mongodb的demo程序,参考App.java中的调用方法,mongodb配置文件在resources目录下,整个项目使用maven,适合初学者学习。

    基于MongoDb的图书管理系统.rar

    本系统是本人初学MongoDb时所写,代码不是很完美,基本实现图书管理系统的增删改查等基本功能,目前尚有一处缺陷未解决——在查询后只能在控制台看到结果,没有反馈到界面上,有兴趣的朋友可以加以修改,相信这是一...

    MONGOdb视频教程地址.txt

    真实有效的mongodb视频教程地址 深入浅出 MongoDB 高清IT教程视频下载 1.1、-nosql与MongoDB.mp4 1.2、-MongoDB安装配置.mp4 1.3、-MongoDB?shell详.mp4 1.4、-MongoDB文档、集合、数据库的概念.mp4 1.5、-...

Global site tag (gtag.js) - Google Analytics