MongoDB - Sharding - Setup

Prepare

1. 每台机器中配置解析

vim /etc/hosts
192.168.4.13  mdb1
192.168.4.17  mdb2
192.168.3.51  mdb3
192.168.3.52  mdb4

2. 机器中创建目录

mdb1, mdb2, mdb3

mkdir -p /data/mongodb/config/data
mkdir -p /data/mongodb/config/log
mkdir -p /data/mongodb/shard1/data
mkdir -p /data/mongodb/shard1/log
mkdir -p /data/mongodb/shard2/data
mkdir -p /data/mongodb/shard2/log
mkdir -p /data/mongodb/shard3/data
mkdir -p /data/mongodb/shard3/log

3. 配置config server 集群,

# 在机器中执行命令 config: mdb1, mdb2, mdb3

mongod --configsvr --replSet cfgReplSet --dbpath /data/mongodb/config/data --port 20000 --bind_ip_all --logpath /data/mongodb/config/log/config.log --fork

# 登录其中一台

mongo --host  mdb1 --port 21000 

# 初始化集群

rs.initiate({_id:"cfgReplSet",configsvr:true,
          members:[{_id:0,host:"mdb1:21000"},
                   {_id:1,host:"mdb2:21000"},
                   {_id:2,host:"mdb3:21000"}]})

4. 配置shard server

# 在机器中执行命令 shard mdb1, mdb2, mdb3

# 启动三个实例,作为三个shard,每个实例都有Replica

mongod --shardsvr --replSet shard1ReplSet --dbpath /data/mongodb/shard1/data --port 22001 --bind_ip_all --logpath /data/mongodb/shard1/log/config.log --fork

mongod --shardsvr --replSet shard2ReplSet --dbpath /data/mongodb/shard2/data --port 22002 --bind_ip_all --logpath /data/mongodb/shard2/log/config.log --fork

mongod --shardsvr --replSet shard3ReplSet --dbpath /data/mongodb/shard3/data --port 22003 --bind_ip_all --logpath /data/mongodb/shard3/log/config.log --fork

# 分别为3个实例 配置集群

mongo --host mdb1 --port 22001
 rs.initiate({_id:"shard1ReplSet",
          members:[{_id:0,host:"mdb1:22001"},
                   {_id:1,host:"mdb2:22001"},
                   {_id:2,host:"mdb3:22001"}]})

mongo --host mdb1 --port 22002 
 rs.initiate({_id:"shard2ReplSet",
          members:[{_id:0,host:"mdb1:22002"},
                   {_id:1,host:"mdb2:22002"},
                   {_id:2,host:"mdb3:22002"}]})

mongo --host mdb1 --port 22003 
 rs.initiate({_id:"shard3ReplSet",
          members:[{_id:0,host:"mdb1:22003"},
                   {_id:1,host:"mdb2:22003"},
                   {_id:2,host:"mdb3:22003"}]})

5. 配置 router server

# router: mdb4

mkdir -p /data/mongodb/mongos/data
mkdir -p /data/mongodb/mongos/log

# 启动router节点

# 增加configdb信息

mongos  --configdb cfgReplSet/mdb1:21000,mdb2:21000,mdb3:21000 --port 20000 --bind_ip_all --logpath /data/mongodb/mongos/log/mongos.log --fork 

# 为router 增加 shard节点

mongo --host mdb4 --port 20000
 sh.addShard("shard1ReplSet/mdb1:22001,mdb2:22001,mdb3:22001")
 sh.addShard("shard2ReplSet/mdb1:22002,mdb2:22002,mdb3:22002")
 sh.addShard("shard3ReplSet/mdb1:22003,mdb2:22003,mdb3:22003")

6. 使用

# 先登录router

mongo --host mdb4 --port 20000

# 使数据库开启sharding

sh.enableSharding("mdbtest01")

mongos> sh.status()
--- Sharding Status ---
  sharding version: {
  	"_id" : 1,
  	"minCompatibleVersion" : 5,
  	"currentVersion" : 6,
  	"clusterId" : ObjectId("5d70a8a77bef6e176e960934")
  }
  shards:
        {  "_id" : "shard1ReplSet",  "host" : "shard1ReplSet/mdb1:22001,mdb2:22001,mdb3:22001",  "state" : 1 }
        {  "_id" : "shard2ReplSet",  "host" : "shard2ReplSet/mdb1:22002,mdb2:22002,mdb3:22002",  "state" : 1 }
        {  "_id" : "shard3ReplSet",  "host" : "shard3ReplSet/mdb1:22003,mdb2:22003,mdb3:22003",  "state" : 1 }
  active mongoses:
        "4.2.0" : 1
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours:
                No recent migrations
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard2ReplSet	1
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard2ReplSet Timestamp(1, 0)
        {  "_id" : "mdbtest01",  "primary" : "shard1ReplSet",  "partitioned" : true,  "version" : {  "uuid" : UUID("43e21b13-ddab-4793-9005-dc11199ea6a2"),  "lastMod" : 1 } }
                mdbtest01.Log
                        shard key: { "id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard1ReplSet	1
                        { "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : shard1ReplSet Timestamp(1, 0)

# 切换数据库

use mdbtest01
show dbs;
sh.shardCollection("mdbtest01.Log",{id:1})

# 向表 Log 插入数据

for (var i = 1; i <= 10000; i++){
	db.Log.save({id:i, "message": "message" + i});
} 

#  使用hashed 分区

sh.shardCollection("mdbtest01.Test", { id : "hashed" })

for (var i = 1; i <= 10000; i++){
	db.Test.save({id:i, "message": "message" + i});
} 

# 看表状态

db.Log.stats();

db.Test.stats();

发布了213 篇原创文章 · 获赞 7 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/chuckchen1222/article/details/100582306