Mongodb副本集部署+用户认证

工作原理

MongoDB的复制是基于操作日志oplog,相当于MySQL中的二进制日志,只记录发生改变的记录。复制是将主节点的oplog日志同步并应用到其他从节点的过程。

   1.副本集之间的复制是通过oplog日志现实的.备份节点通过查询这个集合就可以知道需要进行复制的操作

   2.oplog是节点中local库中的一个固定的集合,在默认情况下oplog初始化大小为空闲磁盘的5%.oplog是capped collection,所以当oplog的空间被占满时,会覆盖最初写入的日志

   3.通过改变oplog文档的大小直接改变local所占磁盘空间的大小.可以在配置文件中设置oplogSize参数来指定oplog文档的大小。

   4.通过oplog中的操作记录,把数据复制在备份节点.

   5.主节点是集中接收写入操作的,MongoDB中的应用连接到主节点进行写入操作,然后记录在主节点操作oplog,从节点成员定期轮询复制此日志并将操作应用于其数据集。

常用命令

   rs.conf()   查看副本集配置

   rs.status()  查看副本集状态

   rs.initiate(config)   初始化副本集

   rs.isMaster()   查看是否为主节点

   db.getMongo().setSlaveOk();   赋予副本集副本节点查询数据的权限,副本节点执行

   rs.add("ip:port")   新增副本集节点

   rs.remove("ip:port")  删除副本集节点

   rs.addArb("ip:port")    添加仲裁节点

副本集参数说明

"_id":            #集群中节点编号 
"name":           #成员服务器名称及端口 
"health" :          #表示成员中的健康状态(0:down;1:up) 
"state" :          #为0~10,表示成员的当前状态 
"stateStr" :        #描述该成员是主库(PRIMARY)还是备库(SECONDARY) 
"uptime" :          #该成员在线时间(秒) 
"optime" :          #成员最后一次应用日志(oplog)的信息 
"optimeDate" :       #成员最后一次应用日志(oplog)的时间 
"electionTime" :      #当前primary从操作日志中选举信息 
"electionDate" :      #当前primary被选定为primary的日期 
"configVersion" :     #mongodb版本 
"self" :           #为true 表示当前节点 

开始部署mongod副本集

一、环境准备

操作系统centos6.5 mongodb版本3.6.6

ip地址                  主机名                    角色

192.168.1.230       mongodb01          副本集主节点

192.168.1.18        mongodb02           副本集副本节点

192.168.1.247      mongodb03           副本集副本节点

关闭3个服务器的防火墙及selinux

二、安装部署mongodb及副本集配置

1、安装部署mongodb

 可参考:https://www.cnblogs.com/wusy/p/10405991.html

2、副本集配置

1)yum安装mongodb在做副本集时需在所有服务器配置文件加入以下配置:

replication:
   replSetName: repset(副本集名,可自定义)   #副本集配置

security:                  #开启副本集用户认证
   authorization: enabled
   keyFile: /data/mongodb/keyfile
   clusterAuthMode: keyFile

2)源码包构建副本集时,则需加以下语法来配置:

replSet=repset(副本集名)  #副本集配置

auth=true   #开启副本集用户认证,若为单击模式,则不创建和指定秘钥文件
keyFile=/data2/mongodb-3.6.6/keyfile  #指定秘钥文件路径

journal=true  #开启journal日志,默认为开启

3、初始化副本集

 在任意一台节点初始化都可,但在哪一台做初始化,那么这一台就作为副本集的主节点,这里选择mongodb01(也可使用权重值来控制主副节点)

1)进入mongodb数据库

[root@mongodb01 ~]# mongo --host 192.168.1.230

2)定义副本集配置,这里的_id:repset要与配置文件中所指的副本集名称一致

 1 >config={
 2 ... _id:"repset",   #务必与配置文件中指定的副本集名称一致
 3 ... members:[
 4 ... {_id:0,host:"192.168.1.230:27017"},
 5 ... {_id:1,host:"192.168.1.18:27017"},
 6 ... {_id:2,host:"192.168.1.247:27017"}
 7 ... ]
 8 ... }
 9 
10 {
11 "_id" : "repset",
12 "members" : [
13 {
14 "_id" : 0,
15 "host" : "192.168.1.230:27017"
16 },
17 {
18 "_id" : 1,
19 "host" : "192.168.1.18:27017"
20 },
21 {
22 "_id" : 2,
23 "host" : "192.18.1.247:27017"
24 }
25 ]
26 }

3)初始化副本集配置

 1 > rs.initiate(config)
 2 {
 3 "ok" : 1,
 4 "operationTime" : Timestamp(1551502195, 1),
 5 "$clusterTime" : {
 6 "clusterTime" : Timestamp(1551502195, 1),
 7 "signature" : {
 8 "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
 9 "keyId" : NumberLong(0)
10 }
11 }
12 }

4)查看集群节点状态(health为1表明正常,为0表明异常)

  1 repset:PRIMARY> rs.status()
  2 {
  3 "set" : "repset",
  4 "date" : ISODate("2019-03-02T04:50:47.554Z"),
  5 "myState" : 1,
  6 "term" : NumberLong(1),
  7 "syncingTo" : "",
  8 "syncSourceHost" : "",
  9 "syncSourceId" : -1,
 10 "heartbeatIntervalMillis" : NumberLong(2000),
 11 "optimes" : {
 12 "lastCommittedOpTime" : {
 13 "ts" : Timestamp(1551502238, 1),
 14 "t" : NumberLong(1)
 15 },
 16 "readConcernMajorityOpTime" : {
 17 "ts" : Timestamp(1551502238, 1),
 18 "t" : NumberLong(1)
 19 },
 20 "appliedOpTime" : {
 21 "ts" : Timestamp(1551502238, 1),
 22 "t" : NumberLong(1)
 23 },
 24 "durableOpTime" : {
 25 "ts" : Timestamp(1551502238, 1),
 26 "t" : NumberLong(1)
 27 }
 28 },
 29 "lastStableCheckpointTimestamp" : Timestamp(1551502208, 1),
 30 "members" : [
 31 {
 32 "_id" : 0,
 33 "name" : "192.168.1.230:27017",
 34 "health" : 1,
 35 "state" : 1,
 36 "stateStr" : "PRIMARY",
 37 "uptime" : 153,
 38 "optime" : {
 39 "ts" : Timestamp(1551502238, 1),
 40 "t" : NumberLong(1)
 41 },
 42 "optimeDate" : ISODate("2019-03-02T04:50:38Z"),
 43 "syncingTo" : "",
 44 "syncSourceHost" : "",
 45 "syncSourceId" : -1,
 46 "infoMessage" : "could not find member to sync from",
 47 "electionTime" : Timestamp(1551502207, 1),
 48 "electionDate" : ISODate("2019-03-02T04:50:07Z"),
 49 "configVersion" : 1,
 50 "self" : true,
 51 "lastHeartbeatMessage" : ""
 52 },
 53 {
 54 "_id" : 1,
 55 "name" : "192.168.1.18:27017",
 56 "health" : 1,
 57 "state" : 2,
 58 "stateStr" : "SECONDARY",
 59 "uptime" : 51,
 60 "optime" : {
 61 "ts" : Timestamp(1551502238, 1),
 62 "t" : NumberLong(1)
 63 },
 64 "optimeDurable" : {
 65 "ts" : Timestamp(1551502238, 1),
 66 "t" : NumberLong(1)
 67 },
 68 "optimeDate" : ISODate("2019-03-02T04:50:38Z"),
 69 "optimeDurableDate" : ISODate("2019-03-02T04:50:38Z"),
 70 "lastHeartbeat" : ISODate("2019-03-02T04:50:47.382Z"),
 71 "lastHeartbeatRecv" : ISODate("2019-03-02T04:50:46.242Z"),
 72 "pingMs" : NumberLong(0),
 73 "lastHeartbeatMessage" : "",
 74 "syncingTo" : "192.168.1.247:27017",
 75 "syncSourceHost" : "192.168.1.247:27017",
 76 "syncSourceId" : 2,
 77 "infoMessage" : "",
 78 "configVersion" : 1
 79 },
 80 {
 81 "_id" : 2,
 82 "name" : "192.168.1.247:27017",
 83 "health" : 1,
 84 "state" : 2,
 85 "stateStr" : "SECONDARY",
 86 "uptime" : 51,
 87 "optime" : {
 88 "ts" : Timestamp(1551502238, 1),
 89 "t" : NumberLong(1)
 90 },
 91 "optimeDurable" : {
 92 "ts" : Timestamp(1551502238, 1),
 93 "t" : NumberLong(1)
 94 },
 95 "optimeDate" : ISODate("2019-03-02T04:50:38Z"),
 96 "optimeDurableDate" : ISODate("2019-03-02T04:50:38Z"),
 97 "lastHeartbeat" : ISODate("2019-03-02T04:50:47.382Z"),
 98 "lastHeartbeatRecv" : ISODate("2019-03-02T04:50:46.183Z"),
 99 "pingMs" : NumberLong(0),
100 "lastHeartbeatMessage" : "",
101 "syncingTo" : "192.168.1.230:27017",
102 "syncSourceHost" : "192.168.1.230:27017",
103 "syncSourceId" : 0,
104 "infoMessage" : "",
105 "configVersion" : 1
106 }
107 ],
108 "ok" : 1,
109 "operationTime" : Timestamp(1551502238, 1),
110 "$clusterTime" : {
111 "clusterTime" : Timestamp(1551502238, 1),
112 "signature" : {
113 "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
114 "keyId" : NumberLong(0)
115 }
116 }
117 }
118 repset:PRIMARY>

 5)查看是否为主节点(可在其他节点执行查看)

 1 repset:PRIMARY> rs.isMaster()
 2 {
 3 "hosts" : [
 4 "192.168.1.230:27017",
 5 "192.168.1.18:27017",
 6 "192.168.1.247:27017"
 7 ],
 8 "setName" : "repset",
 9 "setVersion" : 1,
10 "ismaster" : true,
11 "secondary" : false,
12 "primary" : "192.168.1.230:27017",
13 "me" : "192.168.1.230:27017",
14 "electionId" : ObjectId("7fffffff0000000000000001"),
15 "lastWrite" : {
16 "opTime" : {
17 "ts" : Timestamp(1551502348, 1),
18 "t" : NumberLong(1)
19 },
20 "lastWriteDate" : ISODate("2019-03-02T04:52:28Z"),
21 "majorityOpTime" : {
22 "ts" : Timestamp(1551502348, 1),
23 "t" : NumberLong(1)
24 },
25 "majorityWriteDate" : ISODate("2019-03-02T04:52:28Z")
26 },
27 "maxBsonObjectSize" : 16777216,
28 "maxMessageSizeBytes" : 48000000,
29 "maxWriteBatchSize" : 100000,
30 "localTime" : ISODate("2019-03-02T04:52:32.106Z"),
31 "logicalSessionTimeoutMinutes" : 30,
32 "minWireVersion" : 0,
33 "maxWireVersion" : 7,
34 "readOnly" : false,
35 "ok" : 1,
36 "operationTime" : Timestamp(1551502348, 1),
37 "$clusterTime" : {
38 "clusterTime" : Timestamp(1551502348, 1),
39 "signature" : {
40 "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
41 "keyId" : NumberLong(0)
42 }
43 }
44 }

6)可登入副本集副本节点,并查看是否为主节点或副本节点

 1 [root@mongodb02 ~]# mongo --host 192.168.1.18
 2 
 3 repset:SECONDARY>
 4 
 5 repset:SECONDARY> rs.isMaster()
 6 {
 7 "hosts" : [
 8 "192.168.1.230:27017",
 9 "192.168.1.18:27017",
10 "192.168.1.247:27017"
11 ],
12 "setName" : "repset",
13 "setVersion" : 1,
14 "ismaster" : false,
15 "secondary" : true,
16 "primary" : "192.168.1.230:27017",
17 "me" : "192.168.1.18:27017",
18 "lastWrite" : {
19 "opTime" : {
20 "ts" : Timestamp(1551502458, 1),
21 "t" : NumberLong(1)
22 },
23 "lastWriteDate" : ISODate("2019-03-02T04:54:18Z"),
24 "majorityOpTime" : {
25 "ts" : Timestamp(1551502458, 1),
26 "t" : NumberLong(1)
27 },
28 "majorityWriteDate" : ISODate("2019-03-02T04:54:18Z")
29 },
30 "maxBsonObjectSize" : 16777216,
31 "maxMessageSizeBytes" : 48000000,
32 "maxWriteBatchSize" : 100000,
33 "localTime" : ISODate("2019-03-02T04:54:27.819Z"),
34 "logicalSessionTimeoutMinutes" : 30,
35 "minWireVersion" : 0,
36 "maxWireVersion" : 7,
37 "readOnly" : false,
38 "ok" : 1,
39 "operationTime" : Timestamp(1551502458, 1),
40 "$clusterTime" : {
41 "clusterTime" : Timestamp(1551502458, 1),
42 "signature" : {
43 "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
44 "keyId" : NumberLong(0)
45 }
46 }
47 }

三、登入副本集副本集节点验证数据复制功能(默认副本集的副本节点不能查看数据

1、在主节点mongodb01上插入数据,在副本节点查看数据

1 repset:PRIMARY> use test
2 switched to db test
3 
4 repset:PRIMARY> db.test.save({id:"one",name:"wushaoyu"})
5 WriteResult({ "nInserted" : 1 })

2、进入副本节点mongodb02查看主库插入的数据

 1 repset:SECONDARY> use test
 2 switched to db test
 3 repset:SECONDARY> show dbs
 4 2019-03-02T13:07:06.503+0800 E QUERY [js] Error: listDatabases failed:{
 5 "operationTime" : Timestamp(1551503218, 1),
 6 "ok" : 0,
 7 "errmsg" : "not master and slaveOk=false",
 8 "code" : 13435,
 9 "codeName" : "NotMasterNoSlaveOk",
10 "$clusterTime" : {
11 "clusterTime" : Timestamp(1551503218, 1),
12 "signature" : {
13 "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
14 "keyId" : NumberLong(0)
15 }
16 }
17 } :
18 _getErrorWithCode@src/mongo/shell/utils.js:25:13
19 Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1
20 shellHelper.show@src/mongo/shell/utils.js:865:19
21 shellHelper@src/mongo/shell/utils.js:755:15
22 @(shellhelp2):1:1

注:上面出现了报错

因为主库插入的数据,从库是不具备读的权限的,所以要设置副本节点可以读

1 repset:SECONDARY> db.getMongo().setSlaveOk();
2 
3 repset:SECONDARY> db.test.find()
4 { "_id" : ObjectId("5c7a0f3a6698c70ef075f0a8"), "id" : "one", "name" : "wushaoyu" }
5 
6 repset:SECONDARY> show tables
7 test

注:由此看到,副本节点已经把主节点的数据复制过来了(在mongodb03上做同样操作即可)

、验证故障转移功能,停止当前副本主节点,验证高可用

先停止副本主节点,在查看副本节点的状态,

1)停止副本主节点的mongodb

1 [root@mongodb01 ~]# ps -ef |grep mongod
2 mongod 27170 1 0 12:48 ? 00:00:06 /usr/bin/mongod -f /etc/mongod.conf
3 root 27335 5388 0 13:15 pts/3 00:00:00 grep mongod
4 
5 [root@mongodb01 ~]# kill -9 27170

2)登录其他两个副本集节点,查看是否推选出新的主节点

 1 [root@mongodb02 ~]# mongo --host 192.168.1.18
 2 
 3 repset:SECONDARY> rs.isMaster()
 4 {
 5 "hosts" : [
 6 "192.168.1.230:27017",
 7 "192.168.1.18:27017",
 8 "192.168.1.247:27017"
 9 ],
10 "setName" : "repset",
11 "setVersion" : 1,
12 "ismaster" : false,
13 "secondary" : true,
14 "primary" : "192.168.1.247:27017",
15 "me" : "192.168.1.18:27017",
16 "lastWrite" : {
17 "opTime" : {
18 "ts" : Timestamp(1551503801, 1),
19 "t" : NumberLong(2)
20 },
21 "lastWriteDate" : ISODate("2019-03-02T05:16:41Z"),
22 "majorityOpTime" : {
23 "ts" : Timestamp(1551503801, 1),
24 "t" : NumberLong(2)
25 },
26 "majorityWriteDate" : ISODate("2019-03-02T05:16:41Z")
27 },
28 "maxBsonObjectSize" : 16777216,
29 "maxMessageSizeBytes" : 48000000,
30 "maxWriteBatchSize" : 100000,
31 "localTime" : ISODate("2019-03-02T05:16:48.433Z"),
32 "logicalSessionTimeoutMinutes" : 30,
33 "minWireVersion" : 0,
34 "maxWireVersion" : 7,
35 "readOnly" : false,
36 "ok" : 1,
37 "operationTime" : Timestamp(1551503801, 1),
38 "$clusterTime" : {
39 "clusterTime" : Timestamp(1551503801, 1),
40 "signature" : {
41 "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
42 "keyId" : NumberLong(0)
43 }
44 }
45 }
46 
47 [root@mongodb03 ~]# mongo --host 192.168.1.247
48 
49 repset:PRIMARY> rs.isMaster()
50 {
51 "hosts" : [
52 "192.168.1.230:27017",
53 "192.168.1.18:27017",
54 "192.168.1.247:27017"
55 ],
56 "setName" : "repset",
57 "setVersion" : 1,
58 "ismaster" : true,
59 "secondary" : false,
60 "primary" : "192.168.1.247:27017",
61 "me" : "192.168.1.247:27017",
62 "electionId" : ObjectId("7fffffff0000000000000002"),
63 "lastWrite" : {
64 "opTime" : {
65 "ts" : Timestamp(1551503971, 1),
66 "t" : NumberLong(2)
67 },
68 "lastWriteDate" : ISODate("2019-03-02T05:19:31Z"),
69 "majorityOpTime" : {
70 "ts" : Timestamp(1551503971, 1),
71 "t" : NumberLong(2)
72 },
73 "majorityWriteDate" : ISODate("2019-03-02T05:19:31Z")
74 },
75 "maxBsonObjectSize" : 16777216,
76 "maxMessageSizeBytes" : 48000000,
77 "maxWriteBatchSize" : 100000,
78 "localTime" : ISODate("2019-03-02T05:19:35.282Z"),
79 "logicalSessionTimeoutMinutes" : 30,
80 "minWireVersion" : 0,
81 "maxWireVersion" : 7,
82 "readOnly" : false,
83 "ok" : 1,
84 "operationTime" : Timestamp(1551503971, 1),
85 "$clusterTime" : {
86 "clusterTime" : Timestamp(1551503971, 1),
87 "signature" : {
88 "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
89 "keyId" : NumberLong(0)
90 }
91 }
92 }

ps:发现主节点宕机之后,mongodb03为新推选出来的主节点

3)在新的主节点mongodb03插入数据,在mongodb02上查看是否同步

 1 [root@mongodb03 ~]# mongo --host 192.168.1.247
 2 
 3 repset:PRIMARY> use wsy
 4 switched to db wsy
 5 repset:PRIMARY> db.wsy.save({"name":"wsy"})
 6 WriteResult({ "nInserted" : 1 })
 7 
 8 [root@mongodb02 ~]# mongo --host 192.168.1.18
 9 
10 repset:SECONDARY> db.getMongo().setSlaveOk();
11 repset:SECONDARY> show tables
12 wsy
13 
14 repset:SECONDARY> db.wsy.find()
15 { "_id" : ObjectId("5c7a133007ebf60ac5870bae"), "name" : "wsy" }

由此可看出,数据同步成功

4)启动先前的mongodb01,查看状态,mongodb成为了当前主节点的副本节点,并且在它宕机期间插入的数据也同步了过来

 1 [root@mongodb01 ~]# /etc/init.d/mongod start
 2 
 3 [root@mongodb01 ~]# mongo --host 192.168.1.230
 4 
 5 repset:SECONDARY> rs.isMaster()
 6 {
 7 "hosts" : [
 8 "192.168.1.230:27017",
 9 "192.168.1.18:27017",
10 "192.168.1.247:27017"
11 ],
12 "setName" : "repset",
13 "setVersion" : 1,
14 "ismaster" : false,
15 "secondary" : true,
16 "primary" : "192.168.1.247:27017",
17 "me" : "192.168.1.230:27017",
18 "lastWrite" : {
19 "opTime" : {
20 "ts" : Timestamp(1551504771, 1),
21 "t" : NumberLong(2)
22 },
23 "lastWriteDate" : ISODate("2019-03-02T05:32:51Z"),
24 "majorityOpTime" : {
25 "ts" : Timestamp(1551504771, 1),
26 "t" : NumberLong(2)
27 },
28 "majorityWriteDate" : ISODate("2019-03-02T05:32:51Z")
29 },
30 "maxBsonObjectSize" : 16777216,
31 "maxMessageSizeBytes" : 48000000,
32 "maxWriteBatchSize" : 100000,
33 "localTime" : ISODate("2019-03-02T05:32:59.225Z"),
34 "logicalSessionTimeoutMinutes" : 30,
35 "minWireVersion" : 0,
36 "maxWireVersion" : 7,
37 "readOnly" : false,
38 "ok" : 1,
39 "operationTime" : Timestamp(1551504771, 1),
40 "$clusterTime" : {
41 "clusterTime" : Timestamp(1551504771, 1),
42 "signature" : {
43 "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
44 "keyId" : NumberLong(0)
45 }
46 }
47 }

#开启副本节点读的权限

1 repset:SECONDARY> db.getMongo().setSlaveOk();
2 repset:SECONDARY> db.wsy.find()
3 { "_id" : ObjectId("5c7a133007ebf60ac5870bae"), "name" : "wsy" }

注:在原来的主节点宕机期间,新选举出来的主机插入的数据,也会同步过来

 五、java程序连接副本集测试

public class TestMongoDBReplSet {
 
        public static void main(String[] args) {
 
               try {
                     List<ServerAddress> addresses = new ArrayList<ServerAddress>();
                     ServerAddress address1 = new ServerAddress("192.168.1.230" , 27017);
                     ServerAddress address2 = new ServerAddress("192.168.1.18" , 27017);
                     ServerAddress address3 = new ServerAddress("192.168.1.247" , 27017);
                     addresses.add(address1);
                     addresses.add(address2);
                     addresses.add(address3);
 
                     MongoClient client = new MongoClient(addresses);
                     DB db = client.getDB( "test");
                     DBCollection coll = db.getCollection( "testdb");
 
                      // 插入
                     BasicDBObject object = new BasicDBObject();
                     object.append( "test2", "testval2" );
 
                     coll.insert(object);
 
                     DBCursor dbCursor = coll.find();
 
                      while (dbCursor.hasNext()) {
                           DBObject dbObject = dbCursor.next();
                           System. out.println(dbObject.toString());
                     }
 
              } catch (Exception e) {
                     e.printStackTrace();
              }
 
       }
 
}

    目前看起来支持完美的故障转移了,这个架构是不是比较完美了?其实还有很多地方可以优化

 六、副本集开启用户认证模式

1、创建keyFile文件

  keyFile文件的作用: 集群之间的安全认证,增加安全认证机制KeyFile(开启keyfile认证就默认开启了auth认证了,为了保证后面可以登录,我已创建了用户) 

openssl rand -base64 666 > /root/keyfile    

 其中666是文件大小           /root/keyfile : 文件存放路径

  ##该key的权限必须是600;也可改为400

   chmod 600 /root/keyfile 

 注:创建秘钥文件可添置mongodb副本集,在创建,防止服务无法启动等原因

2、修改配置文件

security:
  authorization: enabled
  keyFile: /root/keyfile
  clusterAuthMode: keyFile

3、重新启动mongodb节点

[root@ecs-1e54-0001 ~]# mongod -f /etc/mongod.conf
2019-04-20T13:05:11.479+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
about to fork child process, waiting until server is ready for connections.
forked process: 26977
child process started successfully, parent exiting

注:此处启动需使用这种指定文件的方式启动。因为本人安装mongodb采用rpm包安装,故启动是可用/etc/init.d/mongod start的方式启动,但是在副本集开启了用户认证的方式下,在采取这种启动方式,因为启动脚本的内容原因,就会报错permission denied,导致启动服务失败。

4、连接验证

[root@ecs-2c32 mongodb]# mongo 192.168.1.193:27017
MongoDB shell version v4.0.0
connecting to: mongodb://192.168.1.193:27017/test
MongoDB server version: 4.0.0
repset:PRIMARY> show dbs
2019-04-20T13:22:49.421+0800 E QUERY    [js] Error: listDatabases failed:{
    "operationTime" : Timestamp(1555737763, 1),
    "ok" : 0,
    "errmsg" : "command listDatabases requires authentication",
    "code" : 13,
    "codeName" : "Unauthorized",
    "$clusterTime" : {
        "clusterTime" : Timestamp(1555737763, 1),
        "signature" : {
            "hash" : BinData(0,"hKSQT5ImwURihIb0w/CeT4vu44E="),
            "keyId" : NumberLong("6681818258909167617")
        }
    }
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1
shellHelper.show@src/mongo/shell/utils.js:865:19
shellHelper@src/mongo/shell/utils.js:755:15
@(shellhelp2):1:1

 可看出此处需要用户认证方可查看数据

repset:PRIMARY> use admin
switched to db admin
repset:PRIMARY> db.auth('admin','admin')
1
repset:PRIMARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

 七、仲裁节点

  副本集只能由主节点来接收写操作,而副本节点可以专门用来读操作

  当副本节点增多时,主节点的复制压力就会增大,这是就可以添加仲裁节点

  在副本集里,仲裁节点仲裁节点不存储数据,只是负责故障转移的群体投票,这样就少了数据复制的压力

1)添加仲裁节点192.168.1.7

 1 repset:PRIMARY> rs.addArb("192.168.1.7:27017")
 2 {
 3 "ok" : 1,
 4 "operationTime" : Timestamp(1551519513, 1),
 5 "$clusterTime" : {
 6 "clusterTime" : Timestamp(1551519513, 1),
 7 "signature" : {
 8 "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
 9 "keyId" : NumberLong(0)
10 }
11 }
12 }

2)查看节点信息

 1 repset:PRIMARY> rs.isMaster()
 2 {
 3 "hosts" : [
 4 "192.168.1.18:27017",
 5 "192.168.1.247:27017",
 6 "192.168.1.230:27017"
 7 ],
 8 "arbiters" : [
 9 "192.168.1.7:27017"
10 ],
11 "setName" : "repset",
12 "setVersion" : 8,
13 "ismaster" : true,
14 "secondary" : false,
15 "primary" : "192.168.1.18:27017",
16 "me" : "192.168.1.18:27017",
17 "electionId" : ObjectId("7fffffff0000000000000003"),
18 "lastWrite" : {
19 "opTime" : {
20 "ts" : Timestamp(1551519513, 1),
21 "t" : NumberLong(3)
22 },
23 "lastWriteDate" : ISODate("2019-03-02T09:38:33Z"),
24 "majorityOpTime" : {
25 "ts" : Timestamp(1551519513, 1),
26 "t" : NumberLong(3)
27 },
28 "majorityWriteDate" : ISODate("2019-03-02T09:38:33Z")
29 },
30 "maxBsonObjectSize" : 16777216,
31 "maxMessageSizeBytes" : 48000000,
32 "maxWriteBatchSize" : 100000,
33 "localTime" : ISODate("2019-03-02T09:38:45.054Z"),
34 "logicalSessionTimeoutMinutes" : 30,
35 "minWireVersion" : 0,
36 "maxWireVersion" : 7,
37 "readOnly" : false,
38 "ok" : 1,
39 "operationTime" : Timestamp(1551519513, 1),
40 "$clusterTime" : {
41 "clusterTime" : Timestamp(1551519513, 1),
42 "signature" : {
43 "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
44 "keyId" : NumberLong(0)
45 }
46 }
47 }

3)登录仲裁节点查看状态

 1 repset:ARBITER> db.getMongo().setSlaveOk();
 2 repset:ARBITER> rs.isMaster()
 3 {
 4 "hosts" : [
 5 "192.168.1.18:27017",
 6 "192.168.1.247:27017",
 7 "192.168.1.230:27017"
 8 ],
 9 "arbiters" : [
10 "192.168.1.7:27017"
11 ],
12 "setName" : "repset",
13 "setVersion" : 8,
14 "ismaster" : false,
15 "secondary" : false,
16 "primary" : "192.168.1.18:27017",
17 "arbiterOnly" : true,
18 "me" : "192.168.1.7:27017",
19 "lastWrite" : {
20 "opTime" : {
21 "ts" : Timestamp(1551519790, 1),
22 "t" : NumberLong(3)
23 },
24 "lastWriteDate" : ISODate("2019-03-02T09:43:10Z"),
25 "majorityOpTime" : {
26 "ts" : Timestamp(1551519790, 1),
27 "t" : NumberLong(3)
28 },
29 "majorityWriteDate" : ISODate("2019-03-02T09:43:10Z")
30 },
31 "maxBsonObjectSize" : 16777216,
32 "maxMessageSizeBytes" : 48000000,
33 "maxWriteBatchSize" : 100000,
34 "localTime" : ISODate("2019-03-02T09:43:14.154Z"),
35 "logicalSessionTimeoutMinutes" : 30,
36 "minWireVersion" : 0,
37 "maxWireVersion" : 7,
38 "readOnly" : false,
39 "ok" : 1
40 }

posted @ 2019-03-02 14:07  wushaoyu  阅读(2844)  评论(0编辑  收藏  举报