基于docker的mongodb的分片集群

1、环境描述

172.18.62.23           node1          shard服务器
172.18.62.24           node2          shard服务器
172.18.62.25           node3          配置服务器
172.18.62.26           node4          配置服务器
172.18.62.27           node5          路由服务器

2、启动容器

bs create mongodb
bs ps

启动容器
3、进入容器

docker exec -it mongodb_node1_1 bash
docker exec -it mongodb_node2_1 bash
docker exec -it mongodb_node3_1 bash
docker exec -it mongodb_node4_1 bash
docker exec -it mongodb_node5_1 bash

4、分别在容器安装mongodb

apt update
apt upgrade
apt install mongodb -y

5、修改配置文件
node1

dbpath=/var/lib/mongodb
logpath=/var/log/mongodb/mongodb.log
logappend=true
bind_ip = 0.0.0.0
port = 27017
shardsvr=true
journal=true

node2

dbpath=/var/lib/mongodb
logpath=/var/log/mongodb/mongodb.log
logappend=true
bind_ip = 0.0.0.0
port = 27017
shardsvr=true
journal=true

启动服务

/etc/init.d/mongodb start

node3

dbpath=/var/lib/mongodb
logpath=/var/log/mongodb/mongodb.log
logappend=true
bind_ip = 0.0.0.0
port = 27017
replSet = configsrv/172.18.62.26:27017
configsvr=true
journal=true

node4

dbpath=/var/lib/mongodb
logpath=/var/log/mongodb/mongodb.log
logappend=true
bind_ip = 0.0.0.0
port = 27017
replSet = configsrv/172.18.62.25:27017
configsvr=true
journal=true

启动服务

/etc/init.d/mongodb start

初始化副本集

>config = 
		{
			_id: 'configsrv', 
			members: 
			[
				{
					"_id":1,
					"host":"172.18.62.25:27017",
					priority: 3
				},
				{
					"_id":2,
					"host":"172.18.62.26:27017",
					priority: 2
				}
			]
		}

node3
node4
node5

logpath=/var/log/mongodb/mongodb.log
logappend=true
bind_ip = 0.0.0.0
port = 27017
configdb=configsrv/172.18.62.25:27017,172.18.62.26:27017

启动服务

mongos -f /etc/mongodb.conf &
root@mongodb-node5:~# netstat -antp |grep mongo
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      1187/mongos         
tcp        0      0 172.18.62.27:39072      172.18.62.25:27017      ESTABLISHED 1187/mongos         
tcp        0      0 172.18.62.27:39062      172.18.62.25:27017      ESTABLISHED 1187/mongos         
tcp        0      0 172.18.62.27:39074      172.18.62.25:27017      ESTABLISHED 1187/mongos         
tcp        0      0 172.18.62.27:44610      172.18.62.26:27017      ESTABLISHED 1187/mongos         
tcp        0      0 172.18.62.27:44616      172.18.62.26:27017      ESTABLISHED 1187/mongos         
tcp        0      0 172.18.62.27:44620      172.18.62.26:27017      ESTABLISHED 1187/mongos         
tcp        0      0 172.18.62.27:44614      172.18.62.26:27017      ESTABLISHED 1187/mongos         
tcp        0      0 172.18.62.27:39068      172.18.62.25:27017      ESTABLISHED 1187/mongos

5、登录路由服务器,添加分片服务器

use admin
db.runCommand({addshard: "172.18.62.23:27017",allowLocal:true})
db.runCommand({addshard: "172.18.62.24:27017",allowLocal:true})

添加分片服务器

6、在路由服务器上设置针对哪个数据库进行分片

db.runCommand({enablesharding: "testdb"})
db.runCommand({shardcollection: "testdb.tb01",key:{id:1}})

数据分片
7、测试结果,连接路由服务器存储数据

use testdb
function add(){
		for(var i=0;i<20000;i++){
			db.tb01.insert({"id":i+10,"name":"jim"})
			}
		}
add()

存储数据
8、查看分片

use testdb
sh.status()
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
  	"_id" : 1,
  	"minCompatibleVersion" : 5,
  	"currentVersion" : 6,
  	"clusterId" : ObjectId("60a387f5b498bbf740fe514e")
  }
  shards:
        {  "_id" : "shard0000",  "host" : "172.18.62.23:27017",  "state" : 1 }
        {  "_id" : "shard0001",  "host" : "172.18.62.24:27017",  "state" : 1 }
  active mongoses:
        "3.6.8" : 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: 
                1 : Success
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard0000	1
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0) 
        {  "_id" : "db02",  "primary" : "shard0001",  "partitioned" : true }
                db02.tb01
                        shard key: { "id" : "hashed" }
                        unique: false
                        balancing: true
                        chunks:
                                shard0000	2
                                shard0001	2
                        { "id" : { "$minKey" : 1 } } -->> { "id" : NumberLong("-4611686018427387902") } on : shard0000 Timestamp(2, 2) 
                        { "id" : NumberLong("-4611686018427387902") } -->> { "id" : NumberLong(0) } on : shard0000 Timestamp(2, 3) 
                        { "id" : NumberLong(0) } -->> { "id" : NumberLong("4611686018427387902") } on : shard0001 Timestamp(2, 4) 
                        { "id" : NumberLong("4611686018427387902") } -->> { "id" : { "$maxKey" : 1 } } on : shard0001 Timestamp(2, 5) 
        {  "_id" : "testdb",  "primary" : "shard0000",  "partitioned" : true }
                testdb.tb01
                        shard key: { "id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard0000	1
                        { "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值