Mongodb复制集由一组Mongod实例(进程)组成,包含一个Primary节点和多个Secondary节点,Mongodb Driver(客户端)的所有数据都写入Primary,Secondary从Primary同步写入的数据,以保持复制集内所有成员存储相同的数据集,提供数据的高可用。
创新互联公司专注于永康企业网站建设,成都响应式网站建设公司,购物商城网站建设。永康网站建设公司,为永康等地区提供建站服务。全流程按需搭建网站,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务
OS:Oracle Linux 6.5
MongoDB:mongodb3.2.3
模拟有三台服务器
10.1.5.123:28001 主节点
10.1.5.123:28002 从节点
10.1.5.123:28003 选举节点
复制集架构:一主,一从,一选举节点
下载地址:https://www.mongodb.org/downloads
mongodb-linux-x86_64-rhel62-3.2.3.tgz
1.创建目录及用户
[root@devtest mongodb]# mkdir conf data log
[root@devtest data]# mkdir 28001 28002 28003
--创建mongo用户
[root@devtest conf]# groupadd mongod
[root@devtest conf]# useradd -g mongod mongod
2.创建配置文件 –配置三个实例的配置文件(修改相应的端口号即可)
–节点1
$ vi /home/mongod/mongodb3.2.3/conf/28001.conf
port=28001
bind_ip=10.1.5.123
logpath=/home/mongod/mongodb3.2.3/log/28001.log
dbpath=/home/mongod/mongodb3.2.3/data/28001/
logappend=true
pidfilepath=/home/mongod/mongodb3.2.3/data/28001/28001.pid
fork=true
oplogSize=1024
replSet=MyMongo
–节点2
$ vi /home/mongod/mongodb3.2.3/conf/28002.conf
port=28002
bind_ip=10.1.5.123
logpath=/home/mongod/mongodb3.2.3/log/28002.log
dbpath=/home/mongod/mongodb3.2.3/data/28002/
logappend=true
pidfilepath=/home/mongod/mongodb3.2.3/data/28002/28002.pid
fork=true
oplogSize=1024
replSet=MyMongo
–节点3
$ vi /home/mongod/mongodb3.2.3/conf/28003.conf
port=28003
bind_ip=10.1.5.123
logpath=/home/mongod/mongodb3.2.3/log/28003.log
dbpath=/home/mongod/mongodb3.2.3/data/28003/
logappend=true
pidfilepath=/home/mongod/mongodb3.2.3/data/28003/28003.pid
fork=true
oplogSize=1024
replSet=MyMongo
3.启动mongo复制集
$ mongod -f /home/mongod/mongodb3.2.3/conf/28001.conf
$ mongod -f /home/mongod/mongodb3.2.3/conf/28002.conf
$ mongod -f /home/mongod/mongodb3.2.3/conf/28003.conf
4.初始化复制集
[mongod@devtest ~]$ mongo 10.1.5.123:28001/admin
MongoDB shell version: 3.2.3
connecting to: 10.1.5.123:28001/admin
Welcome to the MongoDB shell.
For interactive help, type"help".
For more comprehensive documentation, see
• http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
\> db
admin
\> config = {
... "_id":"MyMongo",
... members:[
...{"_id":0,host:"10.1.5.123:28001"},
...{"_id":1,host:"10.1.5.123:28002"},
... {"_id":2,host:"10.1.5.123:28003"}]
... }
{
"id" : "MyMongo",
"members" : [
• {
• "_id" : 0,
• "host" :"10.1.5.123:28001"
• },
• {
• "_id" : 1,
• "host" :"10.1.5.123:28002"
• },
• {
• "_id" : 2,
• "host" :"10.1.5.123:28003"
• }
]
}
–查看复制集成员
\> config.members
[
{
• "_id" : 0,
• "host" :"10.1.5.123:28001"
},
{
• "_id" : 1,
• "host" :"10.1.5.123:28002"
},
{
• "_id" : 2,
• "host" :"10.1.5.123:28003"
}
]
–把节点3修改为arbiter选举节点
\> config.members[2]
{ "_id" : 2, "host" :"10.1.5.123:28003" }
\> config.members[2] ={"_id":2,"host":"10.1.5.123:28003",arbiterOnly:true}
{ "id" : 0, "host" :"10.1.5.123:28003", "arbiterOnly" : true }
–进行复制集初始化
\> rs.initiate(config)
{ "ok" : 1 }
–查看复制集状态
MyMongo:OTHER> rs.status()
{
• "set" : "MyMongo",
• "date" :ISODate("2016-03-15T09:42:47.643Z"),
• "myState" : 1,
• "term" : NumberLong(1),
• "heartbeatIntervalMillis" :NumberLong(2000),
• "members" : [
• {
• "_id" : 0,
• "name" :"10.1.5.123:28001",
• "health" : 1,
• "state" : 1,
• "stateStr" :"PRIMARY",
• "uptime" :1812,
• "optime" : {
• "ts": Timestamp(1458034899, 1),
• "t" :NumberLong(1)
• },
• "optimeDate": ISODate("2016-03-15T09:41:39Z"),
• "infoMessage": "could not find member to sync from",
• "electionTime" : Timestamp(1458034898, 1),
• "electionDate": ISODate("2016-03-15T09:41:38Z"),
• "configVersion" : 1,
• "self" : true
• },
• {
• "_id" : 1,
• "name" :"10.1.5.123:28002",
• "health" : 1,
• "state" : 2,
• "stateStr" :"SECONDARY",
• "uptime" :80,
• "optime" : {
• "ts": Timestamp(1458034899, 1),
• "t" :NumberLong(1)
• },
• "optimeDate": ISODate("2016-03-15T09:41:39Z"),
• "lastHeartbeat" :ISODate("2016-03-15T09:42:46.947Z"),
• "lastHeartbeatRecv" :ISODate("2016-03-15T09:42:46.445Z"),
• "pingMs" :NumberLong(0),
• "syncingTo" :"10.1.5.123:28001",
• "configVersion" : 1
• },
• {
• "_id" : 2,
• "name" :"10.1.5.123:28003",
• "health" : 1,
• "state" : 7,
• "stateStr" :"ARBITER",
• "uptime" :80,
• "lastHeartbeat" :ISODate("2016-03-15T09:42:46.946Z"),
• "lastHeartbeatRecv" :ISODate("2016-03-15T09:42:44.428Z"),
• "pingMs" :NumberLong(0),
• "configVersion": 1
• }
• ],
• "ok" : 1
}
MyMongo:PRIMARY>
\5. 验证复制集同步 分别登录两从节点查看登录状态,分别为secondary和arbite
下面登录从secondary节点进行测试:
在从节点进行查询时报下面的错误,因为在默认情况下,不通过驱动连接mongodb从节点数据库时,如果不开始slaveOK=true,是无法读取从节点数据的。
[mongod@devtest conf]$ mongo10.1.5.123:28002/admin
MongoDB shell version: 3.2.3
connecting to:10.1.5.123:28002/admin
MyMongo:SECONDARY> show dbs
2016-03-15T17:49:47.854+0800 EQUERY [thread1] Error: listDatabasesfailed:{ "ok" : 0, "errmsg" : "not master andslaveOk=false", "code" : 13435 } :
_getErrorWithCode@src/mongo/shell/utils.js:23:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:53:1
shellHelper.show@src/mongo/shell/utils.js:700:19
shellHelper@src/mongo/shell/utils.js:594:15
@(shellhelp2):1:1
MyMongo:SECONDARY>rs.slaveOk(true)
MyMongo:SECONDARY> show dbs
local 0.000GB
test 0.000GB
MyMongo:SECONDARY> use test
switched to db test
MyMongo:SECONDARY> showtables
t1
MyMongo:SECONDARY>db.t1.find()
{ "_id" :ObjectId("56e7dc310ebd75f9ef3546c1"), "name" :"licz" }
下面登录从arbite节点进行测试:
然后登录arbite节点,会发现数据没有同步,因为arbite不参与数据库的同步,即不存储数据
但local数据库大小已经改变了,因为local库存储命名空间(local.ns文件)的内容。
MyMongo:ABITER> rs.slaveOk(true)
MyMongo:ARBITER> show dbs
local 0.000GB
6.模拟主-从故障切换 –杀掉当前主节点为28001端口
[mongod@devtest ~]$ ps -ef|grep mongo
root 7186 7150 0 09:15 pts/2 00:00:00 su - mongod
mongod 7187 7186 0 09:15 pts/2 00:00:00 -bash
mongod 7306 7187 0 09:16 pts/2 00:00:00 ps -ef
mongod 7307 7187 0 09:16 pts/2 00:00:00 grep mongo
mongod 16253 1 2 Mar15 ? 00:22:00 mongod -f/home/mongod/mongodb3.2.3/conf/28001.conf
mongod 16299 1 2 Mar15 ? 00:21:50 mongod -f/home/mongod/mongodb3.2.3/conf/28002.conf
mongod 16345 1 2 Mar15 ? 00:20:05 mongod -f/home/mongod/mongodb3.2.3/conf/28003.conf
[mongod@devtest ~]$ kill -9 16253
–发现主节点已经切换至28002端口
[mongod@devtest ~]$ mongo 10.1.5.123:28002/admin
MongoDB shell version: 3.2.3
connecting to: 10.1.5.123:28002/admin
MyMongo:PRIMARY>
–再次启动28001端口并登录,发现28001已为从节点
[mongod@devtest ~]$ mongod -f /home/mongod/mongodb3.2.3/conf/28001.conf
about to fork child process, waiting untilserver is ready for connections.
forked process: 21410
child process started successfully, parentexiting
[mongod@devtest ~]$ mongo 10.1.5.123:28001/admin
MongoDB shell version: 3.2.3
connecting to: 10.1.5.123:28001/admin
MyMongo:SECONDARY>
分享文章:Mongodb复制集的安装和部署
文章URL:http://www.shufengxianlan.com/qtweb/news12/504012.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联