MongoDB 主从+分片集群配置

 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"});
 
 
本文完
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值