MongoDB设置 Replication Sets

MongoDB 高可用可用分两种 :

Master-Slave 主从复制 :只需要在某一个服务启动时加上–master 参数, 而另一个服务加上–slave 与–source 参数, 即可实现同步。

MongoDB的最新版本已不再推荐此方案。

Replica Sets 复制集 :MongoDB 在 1.6 版本对开发了新功能 replica set,这比之前的 replication 功能要强大一 些,增加了故障自动切换

和自动修复成员节点,各个 DB 之间数据完全一致,大大降低了维 护成功。auto shard 已经明确说明不支持 replication paris,建议使用

replica set,replica set 故障切换完全自动。

Replica Sets 的结构非常类似一个集群 ,其中一个节点如果出现故障, 其它节点马上会将业务 接过来而无须停机操作。

192.168.110.131(node1)

192.168.110.132(node2)

192.168.110.133(node3)

官方文档:

http://docs.mongoing.com/manual-zh/

部署复制集:

http://docs.mongoing.com/manual-zh/tutorial/deploy-replica-set.html

一、MongoDB安装

[[email protected] ~]# vim /etc/yum.repos.d/Mongodb.repo

[mongodb-org-3.4]

name=MongoDB Repository

baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/

gpgcheck=1

enabled=1

gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc

[[email protected] ~]# yum install -y mongodb-org

[[email protected] ~]# service mongod start

Starting mongod:                                           [  OK  ]

[[email protected] ~]# ps aux|grep mong

mongod    1361  5.7 14.8 351180 35104 ?        Sl   01:26   0:01 /usr/bin/mongod -f /etc/mongod.conf

更改数据存放目录:

[[email protected] ~]# mkdir -p /mongodb/data

[[email protected] ~]# chown -R mongod:mongod /mongodb/

[[email protected] ~]# ll /mongodb/

total 4

drwxr-xr-x 2 mongod mongod 4096 May 18 02:04 data

[[email protected] ~]# grep -v "^#" /etc/mongod.conf |grep -v "^$"

systemLog:

destination: file

logAppend: true

path: /var/log/mongodb/mongod.log

storage:

dbPath: /mongodb/data

journal:

enabled: true

processManagement:

fork: true  # fork and run in background

pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile

net:

port: 27017

bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.

[[email protected] ~]# service mongod start

Starting mongod:                                           [  OK  ]

node2,node2与上面一样

二、配置 Replication Sets

介绍一下涉及到的参数

--oplogSize 日志操作文件的大小

--dbpath   数据文件路径

--logpath  日志文件路径

--port        端口号,默认是27017.我这里使用的也是这个端口号.

--replSet   复制集的名字,一个replica sets中的每个节点的这个参数都要用一个复制集名字,这里是test.

--replSet test/  这个后面跟的是其他standard节点的ip和端口

--maxConns   最大连接数

--fork       后台运行

--logappend   日志文件循环使用,如果日志文件已满,那么新日志覆盖最久日志。

--keyFile       标识同一集群的认证私钥

其中在启动节点时一定要加上oplogSize 的参数 为其设置大小,不然在64位操作系统上的mongodb,oplogs都相当大-可能是5%的磁盘空间。

要根据情况设置个合理的值。

v3.4.4上的参数:

[[email protected] ~]# vim /etc/mongod.conf

replication:

oplogSizeMB: 1024

replSetName: rs0

使用Keyfile存取控制部署复制集:

openssl rand -base64 756 > <path-to-keyfile>

chmod 400 <path-to-keyfile>

Configuration File

If using a configuration file, set the security.keyFile option to the keyfile’s path, and the replication.replSetName option to the replica set name:

security:

keyFile: <path-to-keyfile>

replication:

replSetName: <replicaSetName>

Command Line

If using the command line option, start the mongod with the --keyFile and --replSet parameters:

mongod --keyFile <path-to-keyfile> --replSet <replicaSetName>

配置带密钥文件的 Replication Sets:

[[email protected] ~]# openssl rand -base64 756 > /mongodb/mongokey

[[email protected] ~]# cat /mongodb/mongokey

gxpcgjyFj2qE8b9TB/0XbdRVYH9VDb55NY03AHwxCFU58MUjJMeez844i1gaUo/t

.....

.....

[[email protected] ~]# chmod 400 /mongodb/mongokey

[[email protected] ~]# chown mongod:mongod /mongodb/mongokey

[[email protected] ~]# ll /mongodb/

total 8

drwxr-xr-x 4 mongod mongod 4096 May 19 18:39 data

-r-------- 1 mongod mongod 1024 May 19 18:29 mongokey

[[email protected] ~]# vim /etc/mongod.conf

#security:

security:

keyFile: /mongodb/mongokey

#operationProfiling:

#replication:

replication:

oplogSizeMB: 1024

replSetName: rs0

[[email protected] ~]# service mongod restart

Stopping mongod:                                           [  OK  ]

Starting mongod:                                           [  OK  ]

[[email protected] ~]# iptables -I INPUT 4 -m state --state NEW -p tcp --dport 27017  -j ACCEPT

复制hosts文件:

[[email protected] ~]# rsync -avH --progress ‘-e ssh -p 22‘ /etc/hosts [email protected]:/mongodb/

[[email protected] ~]# rsync -avH --progress ‘-e ssh -p 22‘ /etc/hosts [email protected]:/mongodb/

复制密钥文件:

[[email protected] ~]# rsync -avH --progress ‘-e ssh -p 22‘ /mongodb/mongokey [email protected]:/mongodb/

[[email protected] ~]# rsync -avH --progress ‘-e ssh -p 22‘ /mongodb/mongokey [email protected]:/mongodb/

复制配置文件:

[[email protected] ~]# rsync -avH --progress ‘-e ssh -p 22‘ /etc/mongod.conf [email protected]:/etc/

[[email protected] ~]# rsync -avH --progress ‘-e ssh -p 22‘ /etc/mongod.conf [email protected]:/etc/

注意:双方都要按照rsync和openssh-clients

[[email protected] ~]# mongo

> help

db.help()                    help on db methods

db.mycoll.help()             help on collection methods

sh.help()                    sharding helpers

rs.help()                    replica set helpers

.....

> rs.help()

rs.status()                                { replSetGetStatus : 1 } checks repl set status

rs.initiate()                              { replSetInitiate : null } initiates set with default settings

rs.initiate(cfg)                           { replSetInitiate : cfg } initiates set with configuration cfg

rs.conf()                                  get the current configuration object from local.system.replset

.....

> rs.status()

{

"info" : "run rs.initiate(...) if not yet done for the set",

"ok" : 0,

"errmsg" : "no replset config has been received",

"code" : 94,

"codeName" : "NotYetInitialized"

}

> rs.initiate()

{

"info2" : "no configuration specified. Using a default configuration for the set",

"me" : "node1.pancou.com:27017",

"ok" : 1

}

rs0:OTHER>

rs0:PRIMARY> rs.status()

{

"set" : "rs0",

"date" : ISODate("2017-05-18T17:00:49.868Z"),

"myState" : 1,

"term" : NumberLong(1),

"heartbeatIntervalMillis" : NumberLong(2000),

"optimes" : {

"lastCommittedOpTime" : {

"ts" : Timestamp(1495126845, 1),

"t" : NumberLong(1)

},

"appliedOpTime" : {

"ts" : Timestamp(1495126845, 1),

"t" : NumberLong(1)

},

"durableOpTime" : {

"ts" : Timestamp(1495126845, 1),

"t" : NumberLong(1)

}

},

"members" : [

{

"_id" : 0,

"name" : "node1.pancou.com:27017",

"health" : 1,

"state" : 1,

"stateStr" : "PRIMARY",

"uptime" : 1239,

"optime" : {

"ts" : Timestamp(1495126845, 1),

"t" : NumberLong(1)

},

"optimeDate" : ISODate("2017-05-18T17:00:45Z"),

"infoMessage" : "could not find member to sync from",

"electionTime" : Timestamp(1495126824, 2),

"electionDate" : ISODate("2017-05-18T17:00:24Z"),

"configVersion" : 1,

"self" : true

}

],

"ok" : 1

}

rs0:PRIMARY> rs.add("node2.pancou.com")

{ "ok" : 1 }

rs0:PRIMARY> rs.add("node3.pancou.com")

{ "ok" : 1 }

rs0:PRIMARY> rs.status()

{

"set" : "rs0",

"date" : ISODate("2017-05-18T17:08:47.724Z"),

"myState" : 1,

"term" : NumberLong(1),

"heartbeatIntervalMillis" : NumberLong(2000),

"optimes" : {

"lastCommittedOpTime" : {

"ts" : Timestamp(1495127325, 1),

"t" : NumberLong(1)

},

"appliedOpTime" : {

"ts" : Timestamp(1495127325, 1),

"t" : NumberLong(1)

},

"durableOpTime" : {

"ts" : Timestamp(1495127325, 1),

"t" : NumberLong(1)

}

},

"members" : [

{

"_id" : 0,

"name" : "node1.pancou.com:27017",

"health" : 1,              //表明状态正常

"state" : 1,               //1表示是PRIMARY,2表示是slave

"stateStr" : "PRIMARY",    //表示此机器是主库

"uptime" : 1717,

"optime" : {

"ts" : Timestamp(1495127325, 1),

"t" : NumberLong(1)

},

"optimeDate" : ISODate("2017-05-18T17:08:45Z"),

"electionTime" : Timestamp(1495126824, 2),

"electionDate" : ISODate("2017-05-18T17:00:24Z"),

"configVersion" : 3,

"self" : true

},

{

"_id" : 1,

"name" : "node2.pancou.com:27017",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 64,

"optime" : {

"ts" : Timestamp(1495127325, 1),

"t" : NumberLong(1)

},

"optimeDurable" : {

"ts" : Timestamp(1495127325, 1),

"t" : NumberLong(1)

},

"optimeDate" : ISODate("2017-05-18T17:08:45Z"),

"optimeDurableDate" : ISODate("2017-05-18T17:08:45Z"),

"lastHeartbeat" : ISODate("2017-05-18T17:08:46.106Z"),

"lastHeartbeatRecv" : ISODate("2017-05-18T17:08:47.141Z"),

"pingMs" : NumberLong(0),

"syncingTo" : "node1.pancou.com:27017",

"configVersion" : 3

},

{

"_id" : 2,

"name" : "node3.pancou.com:27017",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 55,

"optime" : {

"ts" : Timestamp(1495127325, 1),

"t" : NumberLong(1)

},

"optimeDurable" : {

"ts" : Timestamp(1495127325, 1),

"t" : NumberLong(1)

},

"optimeDate" : ISODate("2017-05-18T17:08:45Z"),

"optimeDurableDate" : ISODate("2017-05-18T17:08:45Z"),

"lastHeartbeat" : ISODate("2017-05-18T17:08:46.195Z"),

"lastHeartbeatRecv" : ISODate("2017-05-18T17:08:46.924Z"),

"pingMs" : NumberLong(0),

"syncingTo" : "node2.pancou.com:27017",

"configVersion" : 3

}

],

"ok" : 1

}

rs0:PRIMARY> db.isMaster()

{

"hosts" : [

"node1.pancou.com:27017",

"node2.pancou.com:27017",

"node3.pancou.com:27017"

],

"setName" : "rs0",

"setVersion" : 3,

"ismaster" : true,

"secondary" : false,

"primary" : "node1.pancou.com:27017",

"me" : "node1.pancou.com:27017",

"electionId" : ObjectId("7fffffff0000000000000001"),

"lastWrite" : {

"opTime" : {

"ts" : Timestamp(1495127705, 1),

"t" : NumberLong(1)

},

"lastWriteDate" : ISODate("2017-05-18T17:15:05Z")

},

"maxBsonObjectSize" : 16777216,

"maxMessageSizeBytes" : 48000000,

"maxWriteBatchSize" : 1000,

"localTime" : ISODate("2017-05-18T17:15:11.146Z"),

"maxWireVersion" : 5,

"minWireVersion" : 0,

"readOnly" : false,

"ok" : 1

}

rs0:PRIMARY> use testdb

rs0:PRIMARY> show collections

testcoll

rs0:PRIMARY> db.testcoll.find()

{ "_id" : ObjectId("591dd9f965cc255a5373aefa"), "name" : "tom", "age" : 25 }

到从库上查看:

node2:

rs0:SECONDARY> rs.slaveOk()

rs0:SECONDARY> show dbs

admin   0.000GB

local   0.000GB

testdb  0.000GB

rs0:SECONDARY> use testdb

switched to db testdb

rs0:SECONDARY> show collections

testcoll

rs0:SECONDARY> db.testcoll.find()

{ "_id" : ObjectId("591dd9f965cc255a5373aefa"), "name" : "tom", "age" : 25 }

rs0:SECONDARY>

node3:

rs0:SECONDARY> rs.slaveOk()

rs0:SECONDARY> show dbs

admin   0.000GB

local   0.000GB

testdb  0.000GB

rs0:SECONDARY> use testdb

switched to db testdb

rs0:SECONDARY> show collections

testcoll

rs0:SECONDARY> db.testcoll.find()

{ "_id" : ObjectId("591dd9f965cc255a5373aefa"), "name" : "tom", "age" : 25 }

rs0:SECONDARY>

主从操作日志

rs0:PRIMARY> use local

switched to db local

rs0:PRIMARY> show collections

me

oplog.rs

replset.election

replset.minvalid

startup_log

system.replset

rs0:PRIMARY> db.oplog.rs.find()

{ "ts" : Timestamp(1495126824, 1), "h" : NumberLong("3056083863196084673"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "initiating set" } }

{ "ts" : Timestamp(1495126825, 1), "t" : NumberLong(1), "h" : NumberLong("7195178065440751511"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "new primary" } }

{ "ts" : Timestamp(1495126835, 1), "t" : NumberLong(1), "h" : NumberLong("5723995478292318850"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "periodic noop" } }

{ "ts" : Timestamp(1495126845, 1), "t" : NumberLong(1), "h" : NumberLong("-3772304067699003381"), "v" : 2, "op" : "n", "ns" : "", "o"

三、查看配置信息

rs0:PRIMARY> db.printReplicationInfo()

configured oplog size:   1024MB

log length start to end: 2541secs (0.71hrs)

oplog first event time:  Fri May 19 2017 01:00:24 GMT+0800 (CST)

oplog last event time:   Fri May 19 2017 01:42:45 GMT+0800 (CST)

now:                     Fri May 19 2017 01:42:48 GMT+0800 (CST)

rs0:PRIMARY>

db.oplog.rs.find():查看复制集产生的日志

db.printReplicationInfo():查看操作日志的一些基本信息,如日志大小、日志启用时间。

db.printSlaveReplicationInfo():查看所有slave延迟情况。

rs0:PRIMARY> db.printSlaveReplicationInfo()

source: node2.pancou.com:27017

syncedTo: Fri May 19 2017 01:47:15 GMT+0800 (CST)

0 secs (0 hrs) behind the primary

source: node3.pancou.com:27017

syncedTo: Fri May 19 2017 01:47:15 GMT+0800 (CST)

0 secs (0 hrs) behind the primary

db.system.replset.find():查看复制集

配置信息:

rs0:PRIMARY> db.system.replset.find()

{ "_id" : "rs0", "version" : 3, "protocolVersion" : NumberLong(1), "members" : [ { "_id" : 0, "host" : "node1.pancou.com:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : {  }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 1, "host" : "node2.pancou.com:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : {  }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 2, "host" : "node3.pancou.com:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : {  }, "slaveDelay" : NumberLong(0), "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatIntervalMillis" : 2000, "heartbeatTimeoutSecs" : 10, "electionTimeoutMillis" : 10000, "catchUpTimeoutMillis" : 2000, "getLastErrorModes" : {  }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 }, "replicaSetId" : ObjectId("591dd3284fc6957e660dc933") } }

rs0:PRIMARY> db.system.replset.find().forEach(printjson)  这种方式更直观

四、主从切换:

1、把node3冰冻30秒

rs0:SECONDARY> rs.freeze(30)

{ "ok" : 1 }

2、把node1 PRIMARY降级、

rs0:PRIMARY> rs.stepDown(30)

2017-05-19T02:09:27.945+0800 E QUERY    [thread1] Error: error doing query: failed: network error while attempting to run command ‘replSetStepDown‘ on host ‘127.0.0.1:27017‘  :

[email protected]/mongo/shell/db.js:132:1

[email protected]/mongo/shell/db.js:150:16

[email protected]/mongo/shell/utils.js:1261:12

@(shell):1:1

2017-05-19T02:09:27.947+0800 I NETWORK  [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed

2017-05-19T02:09:27.949+0800 I NETWORK  [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) ok

30秒后就变成从了

rs0:SECONDARY> rs.status()

{

"set" : "rs0",

"date" : ISODate("2017-05-18T18:12:09.732Z"),

"myState" : 2,

"term" : NumberLong(2),

"syncingTo" : "node2.pancou.com:27017",

"heartbeatIntervalMillis" : NumberLong(2000),

"optimes" : {

"lastCommittedOpTime" : {

"ts" : Timestamp(1495131128, 1),

"t" : NumberLong(2)

},

"appliedOpTime" : {

"ts" : Timestamp(1495131128, 1),

"t" : NumberLong(2)

},

"durableOpTime" : {

"ts" : Timestamp(1495131128, 1),

"t" : NumberLong(2)

}

},

"members" : [

{

"_id" : 0,

"name" : "node1.pancou.com:27017",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 5519,

"optime" : {

"ts" : Timestamp(1495131128, 1),

"t" : NumberLong(2)

},

"optimeDate" : ISODate("2017-05-18T18:12:08Z"),

"syncingTo" : "node2.pancou.com:27017",

"configVersion" : 3,

"self" : true

},

{

"_id" : 1,

"name" : "node2.pancou.com:27017",

"health" : 1,

"state" : 1,

"stateStr" : "PRIMARY",

"uptime" : 3866,

"optime" : {

"ts" : Timestamp(1495131118, 1),

"t" : NumberLong(2)

},

"optimeDurable" : {

"ts" : Timestamp(1495131118, 1),

"t" : NumberLong(2)

},

"optimeDate" : ISODate("2017-05-18T18:11:58Z"),

"optimeDurableDate" : ISODate("2017-05-18T18:11:58Z"),

"lastHeartbeat" : ISODate("2017-05-18T18:12:08.333Z"),

"lastHeartbeatRecv" : ISODate("2017-05-18T18:12:08.196Z"),

"pingMs" : NumberLong(0),

"electionTime" : Timestamp(1495130977, 1),

"electionDate" : ISODate("2017-05-18T18:09:37Z"),

"configVersion" : 3

},

{

"_id" : 2,

"name" : "node3.pancou.com:27017",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 3857,

"optime" : {

"ts" : Timestamp(1495131118, 1),

"t" : NumberLong(2)

},

"optimeDurable" : {

"ts" : Timestamp(1495131118, 1),

"t" : NumberLong(2)

},

"optimeDate" : ISODate("2017-05-18T18:11:58Z"),

"optimeDurableDate" : ISODate("2017-05-18T18:11:58Z"),

"lastHeartbeat" : ISODate("2017-05-18T18:12:08.486Z"),

"lastHeartbeatRecv" : ISODate("2017-05-18T18:12:08.116Z"),

"pingMs" : NumberLong(0),

"syncingTo" : "node2.pancou.com:27017",

"configVersion" : 3

}

],

"ok" : 1

}

rs0:SECONDARY>

五、增减节点

1、增加节点

通过oplog增加节点,这种方式使数据的同步完全依赖于oplog,即oplog中有多少操作日志,这些操作日志就完全在新添加的节点中执行一遍,以完成同步。

在上面有一个3节点的复制集基础上,现在想配置并启动一个新节点,将其加入现在复制集环境中。

[[email protected] ~]# rsync -avH --progress ‘-e ssh -p 22‘ /etc/hosts [email protected]:/etc/

[[email protected] ~]# rsync -avH --progress ‘-e ssh -p 22‘ /mongodb/mongokey [email protected]:/mongodb/

[[email protected] ~]# rsync -avH --progress ‘-e ssh -p 22‘ /etc/mongod.conf [email protected]:/etc/

[[email protected] ~]# iptables -I INPUT 4 -m state --state NEW -p tcp --dport 27017  -j ACCEPT

在主上添加新节点:

rs0:PRIMARY> rs.add("node4.pancou.com")

{ "ok" : 1 }

rs0:PRIMARY> rs.status()

{

"set" : "rs0",

"date" : ISODate("2017-05-19T12:12:57.697Z"),

"myState" : 1,

"term" : NumberLong(8),

"heartbeatIntervalMillis" : NumberLong(2000),

"optimes" : {

"lastCommittedOpTime" : {

"ts" : Timestamp(1495195971, 1),

"t" : NumberLong(8)

},

"appliedOpTime" : {

"ts" : Timestamp(1495195971, 1),

"t" : NumberLong(8)

},

"durableOpTime" : {

"ts" : Timestamp(1495195971, 1),

"t" : NumberLong(8)

}

},

"members" : [

{

"_id" : 0,

"name" : "node1.pancou.com:27017",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 159,

"optime" : {

"ts" : Timestamp(1495195971, 1),

"t" : NumberLong(8)

},

"optimeDurable" : {

"ts" : Timestamp(1495195971, 1),

"t" : NumberLong(8)

},

"optimeDate" : ISODate("2017-05-19T12:12:51Z"),

"optimeDurableDate" : ISODate("2017-05-19T12:12:51Z"),

"lastHeartbeat" : ISODate("2017-05-19T12:12:56.111Z"),

"lastHeartbeatRecv" : ISODate("2017-05-19T12:12:57.101Z"),

"pingMs" : NumberLong(0),

"syncingTo" : "node3.pancou.com:27017",

"configVersion" : 4

},

{

"_id" : 1,

"name" : "node2.pancou.com:27017",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 189,

"optime" : {

"ts" : Timestamp(1495195971, 1),

"t" : NumberLong(8)

},

"optimeDurable" : {

"ts" : Timestamp(1495195971, 1),

"t" : NumberLong(8)

},

"optimeDate" : ISODate("2017-05-19T12:12:51Z"),

"optimeDurableDate" : ISODate("2017-05-19T12:12:51Z"),

"lastHeartbeat" : ISODate("2017-05-19T12:12:56.111Z"),

"lastHeartbeatRecv" : ISODate("2017-05-19T12:12:57.103Z"),

"pingMs" : NumberLong(0),

"syncingTo" : "node3.pancou.com:27017",

"configVersion" : 4

},

{

"_id" : 2,

"name" : "node3.pancou.com:27017",

"health" : 1,

"state" : 1,

"stateStr" : "PRIMARY",

"uptime" : 191,

"optime" : {

"ts" : Timestamp(1495195971, 1),

"t" : NumberLong(8)

},

"optimeDate" : ISODate("2017-05-19T12:12:51Z"),

"electionTime" : Timestamp(1495195800, 1),

"electionDate" : ISODate("2017-05-19T12:10:00Z"),

"configVersion" : 4,

"self" : true

},

{

"_id" : 3,

"name" : "node4.pancou.com:27017",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 71,

"optime" : {

"ts" : Timestamp(1495195971, 1),

"t" : NumberLong(8)

},

"optimeDurable" : {

"ts" : Timestamp(1495195971, 1),

"t" : NumberLong(8)

},

"optimeDate" : ISODate("2017-05-19T12:12:51Z"),

"optimeDurableDate" : ISODate("2017-05-19T12:12:51Z"),

"lastHeartbeat" : ISODate("2017-05-19T12:12:56.122Z"),

"lastHeartbeatRecv" : ISODate("2017-05-19T12:12:56.821Z"),

"pingMs" : NumberLong(1),

"syncingTo" : "node3.pancou.com:27017",

"configVersion" : 4

}

],

"ok" : 1

}

查看状态:

rs0:SECONDARY> rs.slaveOk()

rs0:SECONDARY> show dbs

admin   0.000GB

local   0.000GB

testdb  0.000GB

rs0:SECONDARY> use testdb

switched to db testdb

rs0:SECONDARY> show collections

testcoll

rs0:SECONDARY> db.testcoll.find()

{ "_id" : ObjectId("591dd9f965cc255a5373aefa"), "name" : "tom", "age" : 25 }

rs0:SECONDARY>

rs0:SECONDARY> db.isMaster()

{

"hosts" : [

"node1.pancou.com:27017",

"node2.pancou.com:27017",

"node3.pancou.com:27017",

"node4.pancou.com:27017"

],

"setName" : "rs0",

"setVersion" : 4,

"ismaster" : false,

"secondary" : true,

"primary" : "node3.pancou.com:27017",

"me" : "node4.pancou.com:27017",

"lastWrite" : {

"opTime" : {

"ts" : Timestamp(1495196261, 1),

"t" : NumberLong(8)

},

"lastWriteDate" : ISODate("2017-05-19T12:17:41Z")

},

"maxBsonObjectSize" : 16777216,

"maxMessageSizeBytes" : 48000000,

"maxWriteBatchSize" : 1000,

"localTime" : ISODate("2017-05-19T12:17:44.104Z"),

"maxWireVersion" : 5,

"minWireVersion" : 0,

"readOnly" : false,

"ok" : 1

}

rs0:SECONDARY>

2、减少节点

rs0:PRIMARY> rs.remove("node4.pancou.com:27017")

{ "ok" : 1 }

rs0:PRIMARY> db.isMaster()

{

"hosts" : [

"node1.pancou.com:27017",

"node2.pancou.com:27017",

"node3.pancou.com:27017"

],

"setName" : "rs0",

"setVersion" : 5,

"ismaster" : true,

"secondary" : false,

"primary" : "node3.pancou.com:27017",

"me" : "node3.pancou.com:27017",

"electionId" : ObjectId("7fffffff0000000000000008"),

"lastWrite" : {

"opTime" : {

"ts" : Timestamp(1495196531, 1),

"t" : NumberLong(8)

},

"lastWriteDate" : ISODate("2017-05-19T12:22:11Z")

},

"maxBsonObjectSize" : 16777216,

"maxMessageSizeBytes" : 48000000,

"maxWriteBatchSize" : 1000,

"localTime" : ISODate("2017-05-19T12:22:19.874Z"),

"maxWireVersion" : 5,

"minWireVersion" : 0,

"readOnly" : false,

"ok" : 1

}

rs0:PRIMARY>

时间: 2024-12-27 21:52:17

MongoDB设置 Replication Sets的相关文章

Docker给MongoDB设置用户密码

Docker给MongoDB设置用户密码 没有用户和密码,MongoDB的客户端NoSQL Manager for MongoDB是无法连接的 设置用户和密码如下 #admin进入 docker exec -it 10b15378e5ae mongo admin #创建用户和密码 db.createUser({ user: 'admin', pwd: '123456', roles: [ { role: "userAdminAnyDatabase", db: "admin&q

MongoDB -(2) replica sets

副本集搭建 ---何旭东(某上市公司运维工程师) 目录 副本集搭建... 1 参数介绍:... 1 开战... 3 基本操作... 3 逻辑图... 4 配置文件... 4 节点说明... 4 状态说明... 5 插入配置数据... 5 复制集初始化... 6 查看状态... 6 查看当前主库... 7 优化... 8 调整从库可读... 8 2015年7月16日 到此就配置好了二个replica sets! 发现问题思路:把基础环境一个一个总计一个一个梳理,确认每个节点的对应关系.即可! 在这

MongoDB 设置账号和密码

一.安装MongoDB 1.环境配置: i.操作系统:CentOS release 6.8 (Final) [[email protected] bin]# cat /etc/redhat-release ii.计算机类型:x86_64 [[email protected] bin]# uname -m 2.下载对应的MongoDB 版本 [[email protected] bin]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x8

MongoDB(四)mongodb设置用户访问权限

我们知道MySQL在安装的时候需要我们设置一个数据库默认的用户名和密码,mongodb也不例外,不过mongodb是默认的没有设置访问限制的,不需要输入用户名和密码都可以访问的,但是这样会十分的不安全,我们需要手动的为mongodb加上权限限制命令. 首先建立一个默认的账户 Use admin Db.addUser('sa','sa') 这样我们建立了一个用户名为sa,密码为sa的默认账户. 在此时我们还是可以不输入用户名和密码进入数据库的,接下来我们做如下的修改. 打开mongodb所在的注册

MongoDB设置访问权限、设置用户

mongo                          # 运行客户端(也可以去mongodb安装目录下运行这个)use admin                      # 切换到系统帐户表db.system.users.find()         # 查看当前帐户(密码有加密过)db.system.users.remove({})     # 删除所有帐户db.addUser('admin','password') # 添加新帐户 MongoDB已经使用很长一段时间了,基于Mon

mongodb设置用户名和密码

需求:我们需要在一个mongodb上面新建两个数据库,每个数据库的用户名和密码不一样,讲道理来说我们直接设置admin,就可以控制所有的数据库,不过用起来总是感觉有各种问题,目前还不太熟悉mongodb,所以直接对不同的数据库设置用户名和密码,可以达到预期的效果.用mongobooster测试的时候没有输入用户名和密码会看不到数据库. mongodb的用户名和密码是对应到每个具体的数据库的. 这里记录一下一些命令: 1.首先我们启动mongodb的时候,如果需要修改就以普通方式启动 mongod

MongoDB设置访问权限、设置用户(转)

MongoDB已经使用很长一段时间了,基于MongoDB的数据存储也一直没有使用到权限访问(MongoDB默认设置为无权限访问限制),今天特地花了一点时间研究了一下,研究成果如下: 注:研究成果基于Windows平台 MongoDB在本机安装部署好后 1. 输入命令:show dbs,你会发现它内置有两个数据库,一个名为admin,一个名为local.local好像没啥用,如果哪位在使用过程中发现了这个local表的用途,希望能够留言提醒,那我们就专心来说说admin表 2. 输入命令:use

MongoDB设置访问权限、设置用户、在Java中用户名密码访问

以下操作基于Windows平台 1.启动MongoDB服务 mongod.exe --dbpath d:\mongodb-win32-x86_64-2.2.2\data --logpath d:\mongodb-win32-x86_64-2.2.2\log\mongo.log --auth [--auth]:代表需要根据权限访问,必须加此参数启动服务,客户端才能以用户名密码方式建立连接 2.启动Mongo控制台 mongo D:\mongodb-win32-x86_64-2.2.2\bin>mo

深入浅出MongoDB(四)mongodb设置用户访问权限

我们知道mysql在安装的时候需要我们设置一个数据库默认的用户名和密码,mongodb也不例外,不过mongodb是默认的没有设置访问限制的,不需要输入用户名和密码都可以访问的,但是这样会十分的不安全,我们需要手动的为mongodb加上权限限制命令. 首先建立一个默认的账户 Use admin Db.addUser('sa','sa') 这样我们建立了一个用户名为sa,密码为sa的默认账户. 在此时我们还是可以不输入用户名和密码进入数据库的,接下来我们做如下的修改. 打开mongodb所在的注册