mongodb数据库集群及sharding分片配置

时间:2023-03-09 02:37:14
mongodb数据库集群及sharding分片配置

复制集群的配置

1.安装mongodb数据库

在主节点和从节点上都安装mongodb

# rpm -ivh mongo-10gen-2.4.6-mongodb_1.x86_64.rpm mongo-10gen-server-2.4.6-mongodb_1.x86_64.rpm

2.配置数据库

# mkdir -pv /mongodb/data

# chown -R mongod.mongod /mongodb/data





修改配置文件

# vim /etc/mongod.conf

# 数据目录

dbpath=/mongodb/data

# 配置集群名称

replSet = testrs0

mongodb数据库集群及sharding分片配置

mongodb数据库集群及sharding分片配置

3.启动服务

# service mongod start

4.配置复制集群

主节点上执行初始化:

> rs.initiate()

{

"info2" : "no configuration explicitly specified -- making one",

"me" : "node2.chinasoft.com:27017",

"info" : "Config now saved locally.  Should come online in about a minute.",

"ok" : 1

}

> rs.status()

{

"set" : "testrs0",

"date" : ISODate("2016-06-21T08:05:29Z"),

"myState" : 1,

"members" : [

{

"_id" : 0,

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

"health" : 1,

"state" : 1,

"stateStr" : "PRIMARY",

"uptime" : 925,

"optime" : Timestamp(1466496325, 1),

"optimeDate" : ISODate("2016-06-21T08:05:25Z"),

"self" : true

}

],

"ok" : 1

}





testrs0:PRIMARY> db.isMaster()

{

"setName" : "testrs0",

"ismaster" : true,

"secondary" : false,

"hosts" : [

"node2.chinasoft.com:27017"

],

"primary" : "node2.chinasoft.com:27017",

"me" : "node2.chinasoft.com:27017",

"maxBsonObjectSize" : 16777216,

"maxMessageSizeBytes" : 48000000,

"localTime" : ISODate("2016-06-21T08:07:27.528Z"),

"ok" : 1

}





添加从节点:

testrs0:PRIMARY> rs.add("192.168.8.41:27017")

可以看到8.41成为了从节点

testrs0:PRIMARY> rs.status()

{

"set" : "testrs0",

"date" : ISODate("2016-06-21T08:11:11Z"),

"myState" : 1,

"members" : [

{

"_id" : 0,

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

"health" : 1,

"state" : 1,

"stateStr" : "PRIMARY",

"uptime" : 1267,

"optime" : Timestamp(1466496539, 1),

"optimeDate" : ISODate("2016-06-21T08:08:59Z"),

"self" : true

},

{

"_id" : 1,

"name" : "192.168.8.41:27017",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 132,

"optime" : Timestamp(1466496539, 1),

"optimeDate" : ISODate("2016-06-21T08:08:59Z"),

"lastHeartbeat" : ISODate("2016-06-21T08:11:10Z"),

"lastHeartbeatRecv" : ISODate("2016-06-21T08:11:10Z"),

"pingMs" : 1,

"syncingTo" : "node2.chinasoft.com:27017"

}

],

"ok" : 1

}





在主节点上插入数据

testrs0:PRIMARY> use testdb

switched to db testdb

testrs0:PRIMARY> show collections

system.indexes

testrs0:PRIMARY> db.testcoll.insert({name:'jack',age:22,gender:'male'})

testrs0:PRIMARY> db.testcoll.find()

{ "_id" : ObjectId("5768f78d898085b3a157eae4"), "name" : "jack", "age" : 22, "gender" : "male" }





配置从节点:

testrs0:SECONDARY> rs.slaveOk()

testrs0:SECONDARY> use testdb

switched to db testdb

testrs0:SECONDARY> db.testcoll.find()

{ "_id" : ObjectId("5768f78d898085b3a157eae4"), "name" : "jack", "age" : 22, "gender" : "male" }

分片的配置:

MongoDB Sharding技术是MongoDB为了解决随着数据量的增加和读写请求的增加,单个MongoDB实例无法应对的问题.通过使用Sharding,MongoDB将数据切分成多个部分,将数据分布存放在多个shard上.Sharding技术使单个shard处理请求减少和存储容量减小,同时,随着集群的扩大,整个集群的吞吐量和容量都会扩大.

mongodb数据库集群及sharding分片配置

Sharded cluster分片集群有以下几个组件:shards,query routers,config servers.

shards: 用来存储数据,为这个分片集群提供高可用和数据一致性。在一个生产环境中,每个shard都是一个replica set。



query routers: 或者是mongos实例,用于与应用程序交互,将请求转发到后端的shards,然后将请求结果返回给客户端。一个分片集群可以有多个query router即mongos实例用于分摊客户端的请求压力。如果使用多个mongos实例,可以使用HAProxy或者LVS等代理来转发客户端请求到后端的mongos,必须要配置成client affinity模式保证来自同一个客户端的请求转发到后端相同的mongos.通常会将mongos实例部署到应用服务器上。



config servers: 用于存储分片集群的元数据。这些元数据包含整个集群的数据集合data sets与后端shards的对应关系。query router使用这些元数据来将客户端请求定位到后端相应的shards。生产环境的分片集群正好有3个config servers。config servers里的数据非常重要,如果config servers全部挂掉,整个分片集群将不可用。在生产环境中,每个config server都必须放置到不同的服务器上,并且每个分片集群的config
server不能共用,必须分开部署。

mongodb数据库集群及sharding分片配置

mongodb数据库集群及sharding分片配置

实验拓扑:

mongodb数据库集群及sharding分片配置

configdb: 192.168.8.41

# vim /etc/mongod.conf

configsvr = true



app server: 192.168.8.39



删除掉数据库

# rm -rf /mongodb/data

# vim /etc/mongod.conf

configdb = 192.168.8.41:27019

#将数据库路径注释掉

#dbpath=/mongodb/data





# mongos -f /etc/mongod.conf 

Tue Jun 21 17:02:35.708 warning: running with 1 config server should be done only for testing purposes and is not recommended for production

about to fork child process, waiting until server is ready for connections.

forked process: 36341

all output going to: /var/log/mongo/mongod.log

child process started successfully, parent exiting





sharding: 192.168.8.42、192.168.8.43





# mkdir -pv /mongodb/data

# chown -R mongod.mongod /mongodb/data





修改配置文件

# vim /etc/mongod.conf

# 数据目录

dbpath=/mongodb/data





启动服务

# service mongod start

mongodb数据库集群及sharding分片配置



配置文件服务器上,

# mongo --host 192.168.8.39
#添加节点
mongos> sh.addShard("192.168.8.42:27017")
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> sh.addShard("192.168.8.43:27017")
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" : ObjectId("576902ab487f8f95f4d66e55")
}
shards:
{ "_id" : "shard0000", "host" : "192.168.8.42:27017" }
{ "_id" : "shard0001", "host" : "192.168.8.43:27017" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" } mongos> sh.enableSharding("testdb")
{ "ok" : 1 }
mongos> sh.shardCollection("testdb.testcoll",{age:1,name:1})
{ "collectionsharded" : "testdb.testroll", "ok" : 1 }
mongos> use testdb
switched to db testdb
mongos> show collections
system.indexes
testroll
# 插入数据
mongos> for (i=1;i<=1000000;i++) db.testcoll.insert({name:'user'+i,age:(i%150),address:'#+i'+',xuegang north road,shenzhen',preferbooks:['book'+i,'hello world']})

在8.39上操作分片

添加索引

mongos> db.testcoll.ensureIndex({name:1})

mongos> sh.shardCollection("testdb.testcoll",{name:1})

mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" : ObjectId("576902ab487f8f95f4d66e55")
}
shards:
{ "_id" : "shard0000", "host" : "192.168.8.42:27017" }
{ "_id" : "shard0001", "host" : "192.168.8.43:27017" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : false, "primary" : "shard0000" }
{ "_id" : "testdb", "partitioned" : true, "primary" : "shard0000" }
testdb.testcoll
shard key: { "name" : 1 }
chunks:
shard0001 1
shard0000 4
{ "name" : { "$minKey" : 1 } } -->> { "name" : "user288741" } on : shard0001 Timestamp(2, 0)
{ "name" : "user288741" } -->> { "name" : "user477486" } on : shard0000 Timestamp(2, 1)
{ "name" : "user477486" } -->> { "name" : "user66623" } on : shard0000 Timestamp(1, 2)
{ "name" : "user66623" } -->> { "name" : "user854975" } on : shard0000 Timestamp(1, 3)
{ "name" : "user854975" } -->> { "name" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 4)
testdb.testroll
shard key: { "age" : 1, "name" : 1 }
chunks:
shard0000 1
{ "age" : { "$minKey" : 1 }, "name" : { "$minKey" : 1 } } -->> { "age" : { "$maxKey" : 1 }, "name" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0)