04数据库分片的概念及MongoDB sharding的实现
成都创新互联是专业的都江堰网站建设公司,都江堰接单;提供成都网站设计、做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行都江堰网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
配置环境:
node1:192.168.1.121CentOS release 6.7
node2:192.168.1.122CentOS release 6.7
node3:192.168.1.123CentOS release 6.7
[root@node1 ~]# vim /etc/hosts
添加
192.168.1.121 node1
192.168.1.122 node2
192.168.1.123 node3
[root@node1 ~]# scp /etc/hosts node2:/etc
[root@node1 ~]# scp /etc/hosts node3:/etc
[root@node1 ~]# service mongod stop
[root@node1 ~]# vim /etc/mongod.conf
修改
#replSet=setname
为
replSet=testSet
replIndexPrefetch=_id_only
[root@node1 ~]# service mongod start
[root@node1 ~]# mongo
MongoDB shell version: 2.6.4
connecting to: test
> show dbs
admin (empty)
local 0.078GB
testdb 0.078GB
> use local
switched to db local
> show collections
startup_log
system.indexes
> exit
bye
[root@node1 mongodb-2.6.4]# scp mongodb-org-server-2.6.4-1.x86_64.rpm mongodb-org-tools-2.6.4-1.x86_64.rpm mongodb-org-shell-2.6.4-1.x86_64.rpm node2:/root
[root@node1 mongodb-2.6.4]# scp mongodb-org-server-2.6.4-1.x86_64.rpm mongodb-org-tools-2.6.4-1.x86_64.rpm mongodb-org-shell-2.6.4-1.x86_64.rpm node3:/root
[root@node2 ~]# yum -y install *rpm
[root@node2 ~]# mkdir -p /mongodb/data
[root@node2 ~]# chown -R mongod.mongod /mongodb/
[root@node3 ~]# yum -y install *rpm
[root@node3 ~]# mkdir -p /mongodb/data
[root@node3 ~]# chown -R mongod.mongod /mongodb/
[root@node1 ~]# scp /etc/mongod.conf node2:/etc/
[root@node1 ~]# scp /etc/mongod.conf node3:/etc/
[root@node2 ~]# service mongod start
[root@node3 ~]# service mongod start
[root@node1 ~]# mongo
MongoDB shell version: 2.6.4
connecting to: test
> rs.status()
{
"startupStatus" : 3,
"info" : "run rs.initiate(...) if not yet done for the set",
"ok" : 0,
"errmsg" : "can't get local.system.replset config from self or any seed (EMPTYCONFIG)"
}
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "node1:27017",
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
> rs.status()
{
"set" : "testSet",
"date" : ISODate("2017-01-08T14:33:14Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "node1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1316,
"optime" : Timestamp(1483885955, 1),
"optimeDate" : ISODate("2017-01-08T14:32:35Z"),
"electionTime" : Timestamp(1483885956, 1),
"electionDate" : ISODate("2017-01-08T14:32:36Z"),
"self" : true
}
],
"ok" : 1
}
#添加节点
testSet:PRIMARY> rs.add("192.168.1.122")
{ "ok" : 1 }
testSet:PRIMARY> rs.status()
{
"set" : "testSet",
"date" : ISODate("2017-01-08T14:38:50Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "node1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1652,
"optime" : Timestamp(1483886304, 1),
"optimeDate" : ISODate("2017-01-08T14:38:24Z"),
"electionTime" : Timestamp(1483885956, 1),
"electionDate" : ISODate("2017-01-08T14:32:36Z"),
"self" : true
},
{
"_id" : 1,
"name" : "192.168.1.122:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 26,
"optime" : Timestamp(1483886304, 1),
"optimeDate" : ISODate("2017-01-08T14:38:24Z"),
"lastHeartbeat" : ISODate("2017-01-08T14:38:48Z"),
"lastHeartbeatRecv" : ISODate("2017-01-08T14:38:48Z"),
"pingMs" : 1,
"syncingTo" : "node1:27017"
}
],
"ok" : 1
}
[root@node2 ~]# mongo
MongoDB shell version: 2.6.4
connecting to: test
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
testSet:SECONDARY> show dbs
admin (empty)
local 1.078GB
testdb 0.078GB
testSet:SECONDARY> use testdb;
switched to db testdb
testSet:SECONDARY> rs.slaveOk()
testSet:SECONDARY> rs.status()
{
"set" : "testSet",
"date" : ISODate("2017-01-09T12:02:14Z"),
"myState" : 2,
"syncingTo" : "node1:27017",
"members" : [
{
"_id" : 0,
"name" : "node1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 77028,
"optime" : Timestamp(1483886304, 1),
"optimeDate" : ISODate("2017-01-08T14:38:24Z"),
"lastHeartbeat" : ISODate("2017-01-09T12:02:13Z"),
"lastHeartbeatRecv" : ISODate("2017-01-09T12:02:13Z"),
"pingMs" : 1,
"electionTime" : Timestamp(1483885956, 1),
"electionDate" : ISODate("2017-01-08T14:32:36Z")
},
{
"_id" : 1,
"name" : "192.168.1.122:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 77851,
"optime" : Timestamp(1483886304, 1),
"optimeDate" : ISODate("2017-01-08T14:38:24Z"),
"self" : true
}
],
"ok" : 1
}
testSet:SECONDARY> rs.isMaster()
{
"setName" : "testSet",
"setVersion" : 2,
"ismaster" : false,
"secondary" : true,
"hosts" : [
"192.168.1.122:27017",
"node1:27017"
],
"primary" : "node1:27017",
"me" : "192.168.1.122:27017",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 1000,
"localTime" : ISODate("2017-01-09T12:03:59.702Z"),
"maxWireVersion" : 2,
"minWireVersion" : 0,
"ok" : 1
}
testSet:PRIMARY> rs.isMaster()
{
"setName" : "testSet",
"setVersion" : 2,
"ismaster" : true,
"secondary" : false,
"hosts" : [
"node1:27017",
"192.168.1.122:27017"
],
"primary" : "node1:27017",
"me" : "node1:27017",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 1000,
"localTime" : ISODate("2017-01-09T12:05:47.182Z"),
"maxWireVersion" : 2,
"minWireVersion" : 0,
"ok" : 1
}
#增加新节点
testSet:PRIMARY> rs.add("192.168.1.123")
{ "ok" : 1 }
[root@node3 ~]# mongo
MongoDB shell version: 2.6.4
connecting to: test
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
testSet:SECONDARY> rs.slaveOk()
testSet:SECONDARY> rs.status()
{
"set" : "testSet",
"date" : ISODate("2017-01-09T12:10:20Z"),
"myState" : 2,
"syncingTo" : "node1:27017",
"members" : [
{
"_id" : 0,
"name" : "node1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 78,
"optime" : Timestamp(1483963739, 1),
"optimeDate" : ISODate("2017-01-09T12:08:59Z"),
"lastHeartbeat" : ISODate("2017-01-09T12:10:18Z"),
"lastHeartbeatRecv" : ISODate("2017-01-09T12:10:19Z"),
"pingMs" : 1,
"electionTime" : Timestamp(1483885956, 1),
"electionDate" : ISODate("2017-01-08T14:32:36Z")
},
{
"_id" : 1,
"name" : "192.168.1.122:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 78,
"optime" : Timestamp(1483963739, 1),
"optimeDate" : ISODate("2017-01-09T12:08:59Z"),
"lastHeartbeat" : ISODate("2017-01-09T12:10:18Z"),
"lastHeartbeatRecv" : ISODate("2017-01-09T12:10:18Z"),
"pingMs" : 1,
"syncingTo" : "node1:27017"
},
{
"_id" : 2,
"name" : "192.168.1.123:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 78317,
"optime" : Timestamp(1483963739, 1),
"optimeDate" : ISODate("2017-01-09T12:08:59Z"),
"self" : true
}
],
"ok" : 1
}
testSet:SECONDARY> use testdb
switched to db testdb
testSet:SECONDARY> db.students.findOne()
{ "_id" : ObjectId("5871e94113222f399a5240a3"), "name" : "tom", "age" : 23 }
testSet:SECONDARY> rs.conf()
{
"_id" : "testSet",
"version" : 3,
"members" : [
{
"_id" : 0,
"host" : "node1:27017"
},
{
"_id" : 1,
"host" : "192.168.1.122:27017"
},
{
"_id" : 2,
"host" : "192.168.1.123:27017"
}
]
}
testSet:PRIMARY> use testdb
switched to db testdb
testSet:PRIMARY> db.classes.insert({class: "One",nostu: 40})
WriteResult({ "nInserted" : 1 })
testSet:PRIMARY> show collections;
classes
students
system.indexes
testSet:SECONDARY> db.classes.findOne()
{
"_id" : ObjectId("58737e8606a316aec46edfdc"),
"class" : "One",
"nostu" : 40
}
testSet:SECONDARY> db.classes.insert({class: "Two", nostu: 50})
WriteResult({ "writeError" : { "code" : undefined, "errmsg" : "not master" } })
testSet:SECONDARY> rs.conf()
{
"_id" : "testSet",
"version" : 3,
"members" : [
{
"_id" : 0,
"host" : "node1:27017"
},
{
"_id" : 1,
"host" : "192.168.1.122:27017"
},
{
"_id" : 2,
"host" : "192.168.1.123:27017"
}
]
}
#使主结点“下台”
testSet:PRIMARY> rs.stepDown()
2017-01-09T20:23:48.978+0800 DBClientCursor::init call() failed
2017-01-09T20:23:48.980+0800 Error: error doing query: failed at src/mongo/shell/query.js:81
2017-01-09T20:23:48.982+0800 trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2017-01-09T20:23:48.984+0800 reconnect 127.0.0.1:27017 (127.0.0.1) ok
testSet:SECONDARY> rs.status()
{
"set" : "testSet",
"date" : ISODate("2017-01-09T12:24:27Z"),
"myState" : 2,
"syncingTo" : "192.168.1.123:27017",
"members" : [
{
"_id" : 0,
"name" : "node1:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 79989,
"optime" : Timestamp(1483964038, 1),
"optimeDate" : ISODate("2017-01-09T12:13:58Z"),
"infoMessage" : "syncing to: 192.168.1.123:27017",
"self" : true
},
{
"_id" : 1,
"name" : "192.168.1.122:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 78363,
"optime" : Timestamp(1483964038, 1),
"optimeDate" : ISODate("2017-01-09T12:13:58Z"),
"lastHeartbeat" : ISODate("2017-01-09T12:24:25Z"),
"lastHeartbeatRecv" : ISODate("2017-01-09T12:24:25Z"),
"pingMs" : 1,
"lastHeartbeatMessage" : "syncing to: node1:27017",
"syncingTo" : "node1:27017"
},
{
"_id" : 2,
"name" : "192.168.1.123:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 928,
"optime" : Timestamp(1483964038, 1),
"optimeDate" : ISODate("2017-01-09T12:13:58Z"),
"lastHeartbeat" : ISODate("2017-01-09T12:24:26Z"),
"lastHeartbeatRecv" : ISODate("2017-01-09T12:24:25Z"),
"pingMs" : 1,
"electionTime" : Timestamp(1483964629, 1),
"electionDate" : ISODate("2017-01-09T12:23:49Z")
}
],
"ok" : 1
}
testSet:PRIMARY> db.printReplicationInfo()
configured oplog size: 990MB
log length start to end: 299secs (0.08hrs)
oplog first event time: Mon Jan 09 2017 20:08:59 GMT+0800 (CST)
oplog last event time: Mon Jan 09 2017 20:13:58 GMT+0800 (CST)
now: Mon Jan 09 2017 20:27:20 GMT+0800 (CST)
testSet:SECONDARY> db.printReplicationInfo()
configured oplog size: 990MB
log length start to end: 77734secs (21.59hrs)
oplog first event time: Sun Jan 08 2017 22:38:24 GMT+0800 (CST)
oplog last event time: Mon Jan 09 2017 20:13:58 GMT+0800 (CST)
now: Mon Jan 09 2017 20:28:01 GMT+0800 (CST)
testSet:SECONDARY> rs.status()
{
"set" : "testSet",
"date" : ISODate("2017-01-09T12:29:38Z"),
"myState" : 2,
"syncingTo" : "node1:27017",
"members" : [
{
"_id" : 0,
"name" : "node1:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 78672,
"optime" : Timestamp(1483964038, 1),
"optimeDate" : ISODate("2017-01-09T12:13:58Z"),
"lastHeartbeat" : ISODate("2017-01-09T12:29:37Z"),
"lastHeartbeatRecv" : ISODate("2017-01-09T12:29:37Z"),
"pingMs" : 1,
"syncingTo" : "192.168.1.123:27017"
},
{
"_id" : 1,
"name" : "192.168.1.122:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 79495,
"optime" : Timestamp(1483964038, 1),
"optimeDate" : ISODate("2017-01-09T12:13:58Z"),
"self" : true
},
{
"_id" : 2,
"name" : "192.168.1.123:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1238,
"optime" : Timestamp(1483964038, 1),
"optimeDate" : ISODate("2017-01-09T12:13:58Z"),
"lastHeartbeat" : ISODate("2017-01-09T12:29:37Z"),
"lastHeartbeatRecv" : ISODate("2017-01-09T12:29:37Z"),
"pingMs" : 1,
"electionTime" : Timestamp(1483964629, 1),
"electionDate" : ISODate("2017-01-09T12:23:49Z")
}
],
"ok" : 1
}
#node1节点下线
testSet:SECONDARY> exit
bye
[root@node1 ~]# service mongod stop
Stopping mongod: [ OK ]
#保存配置文件至cfg中(必须在主节点配置)
testSet:PRIMARY> cfg=rs.conf()
{
"_id" : "testSet",
"version" : 3,
"members" : [
{
"_id" : 0,
"host" : "node1:27017"
},
{
"_id" : 1,
"host" : "192.168.1.122:27017"
},
{
"_id" : 2,
"host" : "192.168.1.123:27017"
}
]
}
#设定节点优先级
testSet:PRIMARY> cfg.members[1].priority=2
2
#重读cfg配置文件
testSet:PRIMARY> rs.reconfig(cfg)
2017-01-09T21:08:58.403+0800 DBClientCursor::init call() failed
2017-01-09T21:08:58.404+0800 Error: error doing query: failed at src/mongo/shell/query.js:81
2017-01-09T21:08:58.406+0800 trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2017-01-09T21:08:58.407+0800 reconnect 127.0.0.1:27017 (127.0.0.1) ok
testSet:SECONDARY>
testSet:SECONDARY> rs.status()
{
"set" : "testSet",
"date" : ISODate("2017-01-09T13:09:46Z"),
"myState" : 2,
"syncingTo" : "192.168.1.122:27017",
"members" : [
{
"_id" : 0,
"name" : "node1:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 98,
"optime" : Timestamp(1483967288, 1),
"optimeDate" : ISODate("2017-01-09T13:08:08Z"),
"lastHeartbeat" : ISODate("2017-01-09T13:09:45Z"),
"lastHeartbeatRecv" : ISODate("2017-01-09T13:09:45Z"),
"pingMs" : 1,
"lastHeartbeatMessage" : "syncing to: 192.168.1.122:27017",
"syncingTo" : "192.168.1.122:27017"
},
{
"_id" : 1,
"name" : "192.168.1.122:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 98,
"optime" : Timestamp(1483967288, 1),
"optimeDate" : ISODate("2017-01-09T13:08:08Z"),
"lastHeartbeat" : ISODate("2017-01-09T13:09:45Z"),
"lastHeartbeatRecv" : ISODate("2017-01-09T13:09:46Z"),
"pingMs" : 1,
"electionTime" : Timestamp(1483967290, 1),
"electionDate" : ISODate("2017-01-09T13:08:10Z")
},
{
"_id" : 2,
"name" : "192.168.1.123:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 81883,
"optime" : Timestamp(1483967288, 1),
"optimeDate" : ISODate("2017-01-09T13:08:08Z"),
"infoMessage" : "syncing to: 192.168.1.122:27017",
"self" : true
}
],
"ok" : 1
}
testSet:SECONDARY> rs.conf()
{
"_id" : "testSet",
"version" : 4,
"members" : [
{
"_id" : 0,
"host" : "node1:27017"
},
{
"_id" : 1,
"host" : "192.168.1.122:27017",
"priority" : 2
},
{
"_id" : 2,
"host" : "192.168.1.123:27017"
}
]
}
testSet:PRIMARY> cfg=rs.conf()
{
"_id" : "testSet",
"version" : 4,
"members" : [
{
"_id" : 0,
"host" : "node1:27017"
},
{
"_id" : 1,
"host" : "192.168.1.122:27017",
"priority" : 2
},
{
"_id" : 2,
"host" : "192.168.1.123:27017"
}
]
}
testSet:PRIMARY> cfg.members[2].arbiterOnly=true
true
testSet:PRIMARY> rs.reconfig(cfg)
{
"errmsg" : "exception: arbiterOnly may not change for members",
"code" : 13510,
"ok" : 0
}
testSet:PRIMARY> rs.conf()
{
"_id" : "testSet",
"version" : 4,
"members" : [
{
"_id" : 0,
"host" : "node1:27017"
},
{
"_id" : 1,
"host" : "192.168.1.122:27017",
"priority" : 2
},
{
"_id" : 2,
"host" : "192.168.1.123:27017"
当前标题:57-4数据库分片概念及mongodbsharding的实现
文章网址:http://cqwzjz.cn/article/jedids.html