热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

MongoDB数据库配置主从复制和分片集群

ShardingOne192.168.100.208192.168.100.209192.168.100.210tarzxvfmongodb-linux-x86_64-2.2.0.tgzmvmongodb-linux-x86_64-2.2.0/usr/local/mongo192.168.100.208mkdir-p/

Sharding  One

192.168.100.208
192.168.100.209
192.168.100.210

tar zxvf mongodb-linux-x86_64-2.2.0.tgz
mv mongodb-linux-x86_64-2.2.0 /usr/local/mongo

192.168.100.208
mkdir -p /usr/local/mongo/data/shard1_1
mkdir -p /usr/local/mongo/data/shard2_1
mkdir -p /usr/local/mongo/conf
mkdir -p /usr/local/mongo/log

mongod --shardsvr --replSet shard1 --port 27017 --dbpath=/usr/local/mongo/data/shard1_1 --logpath=/usr/local/mongo/logshard1_1.log --logappend --fork

192.168.100.209
mkdir -p /usr/local/mongo/data/shard1_2
mkdir -p /usr/local/mongo/data/shard2_2
mkdir -p /usr/local/mongo/conf
mkdir -p /usr/local/mongo/log
mongod --shardsvr --replSet shard1 --port 27017 --dbpath=/usr/local/mongo/data/shard1_2 --logpath=/usr/local/mongo/logshard1_2.log --logappend --fork

192.168.100.210
mkdir -p /usr/local/mongo/data/shard1_3
mkdir -p /usr/local/mongo/data/shard2_3
mkdir -p /usr/local/mongo/conf
mkdir -p /usr/local/mongo/log
mongod --shardsvr --replSet shard1 --port 27017 --dbpath=/usr/local/mongo/data/shard1_3 --logpath=/usr/local/mongo/logshard1_3.log --logappend --fork

#用mongo 连接其中一台机器的27017 端口的mongod,初始化Replica Sets“shard1”,执行:

mongo --port 27017
MongoDB shell version: 2.2.0
connecting to: 127.0.0.1:27017/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
> cOnfig= {_id: 'shard1', members: [cOnfig= {_id: 'shard1', members: [
... {_id: 0, host: '192.168.100.208:27017'},{_id: 0, host: '192.168.100.208:27017'},
... {_id: 1, host: '192.168.100.209:27017'},{_id: 1, host: '192.168.100.209:27017'},
... {_id: 2, host: '192.168.100.210:27017'}]{_id: 2, host: '192.168.100.210:27017'}]
... }}
{
       "_id" : "shard1",
       "members" : [
               {
     "_id" : 0,
     "host" : "192.168.100.208:27017"
               },
               {
     "_id" : 1,
     "host" : "192.168.100.209:27017"
               },
               {
     "_id" : 2,
     "host" : "192.168.100.210:27017"
               }
       ]
}

> rs.initiate(config)
{
       "info" : "Config now saved locally.  Should come online in about a minute.",
       "ok" : 1
}
>

shard1:PRIMARY> rs.status()rs.status()
{
       "set" : "shard1",
       "date" : ISODate("2012-12-03T10:24:16Z"),
       "myState" : 1,
       "members" : [
               {
     "_id" : 0,
     "name" : "192.168.100.208:27017",
     "health" : 1,
     "state" : 1,
     "stateStr" : "PRIMARY",
     "uptime" : 444,
     "optime" : Timestamp(1354530155000, 1),
     "optimeDate" : ISODate("2012-12-03T10:22:35Z"),
     "self" : true
               },
               {
     "_id" : 1,
     "name" : "192.168.100.209:27017",
     "health" : 1,
     "state" : 2,
     "stateStr" : "SECONDARY",
     "uptime" : 90,
     "optime" : Timestamp(1354530155000, 1),
     "optimeDate" : ISODate("2012-12-03T10:22:35Z"),
     "lastHeartbeat" : ISODate("2012-12-03T10:24:15Z"),
     "pingMs" : 32
               },
               {
     "_id" : 2,
     "name" : "192.168.100.210:27017",
     "health" : 1,
     "state" : 2,
     "stateStr" : "SECONDARY",
     "uptime" : 90,
     "optime" : Timestamp(1354530155000, 1),
     "optimeDate" : ISODate("2012-12-03T10:22:35Z"),
     "lastHeartbeat" : ISODate("2012-12-03T10:24:14Z"),
     "pingMs" : 244
               }
       ],
       "ok" : 1
}

##Sharding two

#192.168.100.208

mongod --shardsvr --replSet shard2 --port 27018 --dbpath=/usr/local/mongo/data/shard2_1 --logpath=/usr/local/mongo/logshard2_1.log --logappend --fork --auth

#192.168.100.209

mongod --shardsvr --replSet shard2 --port 27018 --dbpath=/usr/local/mongo/data/shard2_2 --logpath=/usr/local/mongo/logshard2_2.log --logappend --fork --auth

#192.168.100.210

mongod --shardsvr --replSet shard2 --port 27018 --dbpath=/usr/local/mongo/data/shard2_3 --logpath=/usr/local/mongo/logshard2_3.log --logappend --fork --auth

#用mongo 连接其中一台机器的27018 端口的mongod,初始化Replica Sets “shard2”,执行:
mongo  admin --port 27018
MongoDB shell version: 2.2.0
connecting to: 127.0.0.1:27018/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

> cOnfig= {_id: 'shard2', members: [
... {_id: 0, host: '192.168.100.208:27018'},
... {_id: 1, host: '192.168.100.209:27018'},
... {_id: 2, host: '192.168.100.210:27018'}]
... }}
{
       "_id" : "shard2",
       "members" : [
               {
     "_id" : 0,
     "host" : "192.168.100.208:27018"
               },
               {
     "_id" : 1,
     "host" : "192.168.100.209:27018"
               },
               {
     "_id" : 2,
     "host" : "192.168.100.210:27018"
               }
       ]
}

> rs.initiate(config)

#Config Server
在Server 192.168.100.208、192.168.100.209、192.168.100.210上执行:

mongod --configsvr --dbpath=/usr/local/mongo/conf --port 20000 --logpath=/usr/local/mongo/log/config.log --logappend --fork

#配置3 台Route Process
在Server 192.168.100.208、192.168.100.209、192.168.100.210上执行:
mongos --configdb 192.168.100.208:20000,192.168.100.209:20000,192.168.100.210:20000 --port 30000 --chunkSize 1 --logpath=/usr/local/mongo/log/mongos.log --logappend
--fork

配置Shard Cluster
连接到其中一台机器的端口30000 的mongos 进程,并切换到admin 数据库做以下配置
mongo  admin  --port 30000
MongoDB shell version: 2.2.0
connecting to: 127.0.0.1:30000/admin
mongos> db.runCommand({addshard:"shard1/192.168.100.208:27017,192.168.100.209:27017,192.168.100.210:27017"});db.runCommand({addshard:"shard1/192.168.100.208:27017,192.168.100.209:27017,192.168.100.210:27017"});
{ "shardAdded" : "shard1", "ok" : 1 }
mongos> db.runCommand({addshard:"shard2/192.168.100.208:27018,192.168.100.209:27018,192.168.100.210:27018"});db.runCommand({addshard:"shard2/192.168.100.208:27018,192.168.100.209:27018,192.168.100.210:27018"});
{ "shardAdded" : "shard2", "ok" : 1 }

激活数据库及集合的分片
mongos> db.runCommand({ enablesharding:"hong" })
{ "ok" : 1 }
mongos> db.runCommand({ shardcollection: "hong.users", key: { _id:1 }})
{ "collectionsharded" : "andylhz.users", "ok" : 1 }
插入测试数据
use andylhz
for(var i=1;i<=2000000;i++) db.users.insert({id:i,addr_1:"Beijing",addr_2:"Shanghai"});


推荐阅读
author-avatar
agree_6398026768
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有