zoukankan      html  css  js  c++  java
  • MongoDB ShardingCluster

    sharding集群中的组件:

    1、mongos:router,可以通过keepalived实现高可用。
    2、config server:元数据服务器,这里要借助zookeeper存放配置信息。
    3、shard server:数据节点,即mongod实例,生产环境中这里的每一个shard都是一个复制集。

    基于范围切片:range
    基于列表切片:list
    基于hash切片:hash

    生产环境中,mongos需要两个节点实现高可用。config server需要三个节点,借助zookeeper实现高可用。
    https://docs.mongodb.com/v2.6/core/sharded-cluster-architectures-production/

    这里使用4个节点来演示,一个mongos,一个config server,两个shard节点。
    https://docs.mongodb.com/v2.6/core/sharded-cluster-architectures-test/

    准备四台机器,系统版本CentOS7.3。

    `--configsvr`:declare this is a config db of a cluster; default port 27019; default dir /data/configdb. 声明该节点是一个configdb,默认监听27019端口,默认数据目录为/data/configdb。
    `--shardsvr`:declare this is a shard db of a cluster; default port 27018. 声明该节点是一个sharddb,默认监听27018端口。
    

    1、修改hosts

    192.168.135.170         node1
    192.168.135.171         node2
    192.168.135.169         node3
    192.168.135.172         node4
    

    2、校对时间

    #yum install -y ntp ntpdate && ntpdate pool.ntp.org
    

    3、给各节点安装mongo

    #yum install -y mongodb mongodb-server
    

    4、这里把node2作为config server,修改配置文件,启动服务

    #vim /etc/mongod.conf
    #bind_ip = 127.0.0.1
    configsvr = true
    #systemctl start mongod
    #netstat -tnlp
    

    5、这里把node1作为mongos,mongos服务与mongod服务一样也是有mongodb-server包提供,而且同样也监听27017端口。修改配置文件,启动服务

    #vim /etc/mongos.conf
    #bind_ip = 127.0.0.1
    configdb = 192.168.135.171:27019
    #chunkSize = 64 		指定chunk大小,默认为64mb。
    #systemctl start mongos
    #netstat -tnlp
    
    #mongo
    > help
    > show dbs
    admin   (empty)
    config  0.016GB
    
    > sh.status()
    --- Sharding Status --- 
      sharding version: {
    	"_id" : 1,
    	"version" : 4,
    	"minCompatibleVersion" : 4,
    	"currentVersion" : 5,
    	"clusterId" : ObjectId("58c3e3d46f8981dee1679406")
    }
      shards:
      databases:
    	{  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
    

    6、修改两个shard节点的配置文件,并启动服务

    #vim /etc/mongod.conf
    #bind_ip = 127.0.0.1
    #systemctl start mongod
    

    7、在mongos server上添加两个shard节点

    > sh.help()
    sh.addShard( host ):server:port OR setname/server:port. 添加shard节点。
    sh.enableSharding(dbname):enables sharding on the database dbname. 在指定的数据库上启用shard。
    sh.shardCollection(fullName,key,unique):shards the collection. 对指定的collection进行分片。
    sh.getBalancerState():return true if enabled. 查看balancer是否启用。
    sh.isBalancerRunning():return true if the balancer has work in progress on any mongos. 查看balancer是否在运行。
    sh.setBalancerState( <bool on or not> ):turns the balancer on or off true=on, false=off. 设置是否启用balancer。
    
    #mongo
    > sh.addShard("node3:27017") 		这里必须要用引号引起来,要不然会报语法错误
    { "shardAdded" : "shard0000", "ok" : 1 }
    > sh.addShard("node4:27017")
    { "shardAdded" : "shard0001", "ok" : 1 }
    
    > sh.status() 		这时能看到两个shard
    --- Sharding Status --- 
      sharding version: {
    	"_id" : 1,
    	"version" : 4,
    	"minCompatibleVersion" : 4,
    	"currentVersion" : 5,
    	"clusterId" : ObjectId("58c3e3d46f8981dee1679406")
    }
      shards:
    	{  "_id" : "shard0000",  "host" : "node3:27017" }
    	{  "_id" : "shard0001",  "host" : "node4:27017" }
      databases:
    	{  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
    

    8、在指定的库上启用分片。数据库无需事先存在,可以直接启用分片,再创建数据库。

    > use testdb
    switched to db testdb
    > for (i=1;i<=10000;i++) db.students.insert({name: "student"+i, age: (i%120), address: "#85 Wenhua Road, Zhengzhou, China"})
    WriteResult({ "nInserted" : 1 })
    
    > sh.enableSharding("testdb") 		在testdb库上启用分片,注意要用引号
    { "ok" : 1 }
    
    > sh.status()
    --- Sharding Status --- 
      sharding version: {
    	"_id" : 1,
    	"version" : 4,
    	"minCompatibleVersion" : 4,
    	"currentVersion" : 5,
    	"clusterId" : ObjectId("58c3e3d46f8981dee1679406")
    }
      shards:
    	{  "_id" : "shard0000",  "host" : "node3:27017" }
    	{  "_id" : "shard0001",  "host" : "node4:27017" }
      databases:
    	{  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
    	{  "_id" : "testdb",  "partitioned" : true,  "primary" : "shard0000" }
    
    不做分片的collection都保存在primary shard上。	
    

    9、对指定的collection进行分片,这里基于年龄分片

    > sh.shardCollection("testdb.sudents",{"age": 1})
    { "collectionsharded" : "testdb.sudents", "ok" : 1 }
    
    > sh.status()
    --- Sharding Status --- 
      sharding version: {
    	"_id" : 1,
    	"version" : 4,
    	"minCompatibleVersion" : 4,
    	"currentVersion" : 5,
    	"clusterId" : ObjectId("58c3e3d46f8981dee1679406")
    }
      shards:
    	{  "_id" : "shard0000",  "host" : "node3:27017" }
    	{  "_id" : "shard0001",  "host" : "node4:27017" }
      databases:
    	{  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
    	{  "_id" : "testdb",  "partitioned" : true,  "primary" : "shard0000" }
    		testdb.sudents
    			shard key: { "age" : 1 }
    			chunks:
    				shard0000	1
    			{ "age" : { "$minKey" : 1 } } -->> { "age" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0)
    

    10、查看balancer状态

    > sh.getBalancerState()
    true
    > sh.isBalancerRunning()
    false
    
  • 相关阅读:
    Scrum:The Definition of Done —— 作业有没有写完呢?
    中兴通讯 可视化devops 牛啊 屠亚奇
    qunar-dns
    通过业务系统的重构实践DDD
    通过业务系统的重构实践DDD
    一键部署Kubernetes高可用集群
    springboot系列
    Ubuntu · Docker —— 从入门到实践
    容器化操作系统概览
    基于 CentOS7 的 Kubernetes 集群
  • 原文地址:https://www.cnblogs.com/keithtt/p/6536885.html
Copyright © 2011-2022 走看看