文章目录
简述
本文是简述MongoDB在Ubuntu18.04上配置副本集的方法,为学习环境版本,旨在了解如何安装配置MongoDB副本集的方法。
本次安装是直接在一台机器上启动3个Mongo服务来实现集群的效果。
环境准备
系统:Ubuntu18.04。
MongoDB版本:ubuntu1804-4.4.6。(下载地址:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1804-4.4.6.tgz)
安装配置流程
-
将压缩包放在需要安装的机器中,我是放在/home/adv/中,然后解压
tar -zxvf mongodb-linux-x86_64-ubuntu1804-4.4.6.tgz
-
将解压后的文件移动到/usr/local下,并重命名为mongodb
mv mongodb-linux-x86_64-ubuntu1804-4.4.6 /usr/local/mongodb
-
执行以下命令
mkdir -p /usr/local/mongodb/server/27017 /usr/local/mongodb/server/27018 /usr/local/mongodb/server/27019
-
进入/usr/local/mongodb/server,依次进入27017、27018、27019文件夹执行以下命令
mkdir -p data/db log pid
-
在/usr/local/mongodb/server下创建mongodb.conf,编辑
vim /usr/local/mongodb/server/mongodb.conf
-
复制以下内容
# mongodb的服务端口 port=27017 # 数据存放目录 dbpath=/usr/local/mongodb/server/27017/data/db # 指定日志文件 logpath=/usr/local/mongodb/server/27017/log/mongodb.log # 写日志的模式,设置为true为追加。默认是覆盖。如果未指定此设置,启动时MongoDB的将覆盖现有的日志文件。 logappend=true # 绑定地址,默认127.0.0.1,只能通过本地连接 bind_ip=0.0.0.0 # 是否后台运行,true为后台运行,默认false fork=true # 是否开启操作日志 journal=true # 副本集配置,所有主机必须有相同的名称作为同一个副本集 replSet=shard1
-
将mongodb.conf复制进27017、27018、27019文件夹下,27018与27019文件夹下的mongodb.conf需要做修改,主要是修改port、dbpath、logpath
# 27018 # mongodb的服务端口 port=27018 # 数据存放目录 dbpath=/usr/local/mongodb/server/27018/data/db # 指定日志文件 logpath=/usr/local/mongodb/server/27018/log/mongodb.log # 写日志的模式,设置为true为追加。默认是覆盖。如果未指定此设置,启动时MongoDB的将覆盖现有的日志文件。 logappend=true # 绑定地址,默认127.0.0.1,只能通过本地连接 bind_ip=0.0.0.0 # 是否后台运行,true为后台运行,默认false fork=true # 是否开启操作日志 journal=true # 副本集配置,所有主机必须有相同的名称作为同一个副本集 replSet=shard1
# 27019 # mongodb的服务端口 port=27019 # 数据存放目录 dbpath=/usr/local/mongodb/server/27019/data/db # 指定日志文件 logpath=/usr/local/mongodb/server/27019/log/mongodb.log # 写日志的模式,设置为true为追加。默认是覆盖。如果未指定此设置,启动时MongoDB的将覆盖现有的日志文件。 logappend=true # 绑定地址,默认127.0.0.1,只能通过本地连接 bind_ip=0.0.0.0 # 是否后台运行,true为后台运行,默认false fork=true # 是否开启操作日志 journal=true # 副本集配置,所有主机必须有相同的名称作为同一个副本集 replSet=shard1
-
启动mongodb
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/server/27017/mongodb.conf
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/server/27018/mongodb.conf
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/server/27019/mongodb.conf
-
在任意一个节点上登录mongodb,在这里我选择27017
/usr/local/mongodb/bin/mongo localhost:27017
-
执行如下命令
cfg={_id: 'shard1', members: [ {_id: 0,host: 'localhost:27017'}, {_id: 1,host: 'localhost:27018'}] } rs.initiate(cfg)
出现如下信息就代表成功了
{ "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1620806982, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1620806982, 1) }
-
查看replica set的状态
{ "set" : "shard1", "date" : ISODate("2021-05-12T08:10:04.529Z"), "myState" : 1, "term" : NumberLong(1), "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 2, "writeMajorityCount" : 2, "votingMembersCount" : 2, "writableVotingMembersCount" : 2, "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1620806993, 1), "t" : NumberLong(1) }, "lastCommittedWallTime" : ISODate("2021-05-12T08:09:53.555Z"), "readConcernMajorityOpTime" : { "ts" : Timestamp(1620806993, 1), "t" : NumberLong(1) }, "readConcernMajorityWallTime" : ISODate("2021-05-12T08:09:53.555Z"), "appliedOpTime" : { "ts" : Timestamp(1620806993, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1620806993, 1), "t" : NumberLong(1) }, "lastAppliedWallTime" : ISODate("2021-05-12T08:09:53.555Z"), "lastDurableWallTime" : ISODate("2021-05-12T08:09:53.555Z") }, "lastStableRecoveryTimestamp" : Timestamp(1620806992, 3), "electionCandidateMetrics" : { "lastElectionReason" : "electionTimeout", "lastElectionDate" : ISODate("2021-05-12T08:09:52.458Z"), "electionTerm" : NumberLong(1), "lastCommittedOpTimeAtElection" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "lastSeenOpTimeAtElection" : { "ts" : Timestamp(1620806982, 1), "t" : NumberLong(-1) }, "numVotesNeeded" : 2, "priorityAtElection" : 1, "electionTimeoutMillis" : NumberLong(10000), "numCatchUpOps" : NumberLong(0), "newTermStartDate" : ISODate("2021-05-12T08:09:52.542Z"), "wMajorityWriteAvailabilityDate" : ISODate("2021-05-12T08:09:53.399Z") }, "members" : [ { "_id" : 0, "name" : "192.168.158.129:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 2237, "optime" : { "ts" : Timestamp(1620806993, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2021-05-12T08:09:53Z"), "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "Could not find member to sync from", "electionTime" : Timestamp(1620806992, 1), "electionDate" : ISODate("2021-05-12T08:09:52Z"), "configVersion" : 1, "configTerm" : 1, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "192.168.158.129:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 22, "optime" : { "ts" : Timestamp(1620806993, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1620806993, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2021-05-12T08:09:53Z"), "optimeDurableDate" : ISODate("2021-05-12T08:09:53Z"), "lastHeartbeat" : ISODate("2021-05-12T08:10:04.477Z"), "lastHeartbeatRecv" : ISODate("2021-05-12T08:10:03.492Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "192.168.158.129:27017", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1, "configTerm" : 1 } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1620806993, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1620806993, 1) }
在replica set的信息中,其中重要的是:
myState,如果是1代表当前登录的是primary;如果是2代表当前登录的是secondary。
成员信息中比较重要的是
state:1表示该host是当前可以进行读写,2:不能读写
health:1表示该host目前是正常的,0:异常 -
测试副本集数据复制功能
(1)登录primary的mongod,插入一条数据
shard1:PRIMARY> db.book.insert({'title':'java'})
(2)登录secondary查看是否有数据
/usr/local/mongodb/bin/mongo localhost:<secondary的端口号>
shard1:SECONDARY> use test switched to db test shard1:SECONDARY> db.book.find() Error: error: { "topologyVersion" : { "processId" : ObjectId("609b84d872706685afca8f2c"), "counter" : NumberLong(5) }, "operationTime" : Timestamp(1620811173, 1), "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435, "codeName" : "NotPrimaryNoSecondaryOk", "$clusterTime" : { "clusterTime" : Timestamp(1620811173, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } }
发现报错,这里还需要做最后一步,在需要读取数据的secondary上执行
shard1:SECONDARY> db.getMongo().setSlaveOk() WARNING: setSlaveOk() is deprecated and may be removed in the next major release. Please use setSecondaryOk() instead. shard1:SECONDARY> db.book.find() { "_id" : ObjectId("609b8e098cb30b347f43a434"), "title" : "java" }
-
增加一个仲裁节点,只负责仲裁,不做数据存储
shard1:PRIMARY> rs.addArb("localhost:27019"); { "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1620807241, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1620807241, 1) }
-
查看现有环境
{ "_id" : "shard1", "version" : 2, "term" : 1, "protocolVersion" : NumberLong(1), "writeConcernMajorityJournalDefault" : true, "members" : [ { "_id" : 0, "host" : "192.168.158.129:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 1, "host" : "192.168.158.129:27018", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 2, "host" : "192.168.158.129:27019", "arbiterOnly" : true, "buildIndexes" : true, "hidden" : false, "priority" : 0, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatIntervalMillis" : 2000, "heartbeatTimeoutSecs" : 10, "electionTimeoutMillis" : 10000, "catchUpTimeoutMillis" : -1, "catchUpTakeoverDelayMillis" : 30000, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 }, "replicaSetId" : ObjectId("609b8d463457d6bc0e76737d") } }