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
    
  • 相关阅读:
    Mac 卸载MySql的方法
    关于mysql的wait_timeout参数 设置不生效的问题
    linux下利用nohup后台运行jar文件包程序
    MySql创建视图
    spring mvc获取header
    Spring Data Jpa 查询返回自定义对象
    Caused by: org.xml.sax.SAXParseException: The reference to entity "characterEncoding" must end with the ';' delimiter.
    eclipse Reference 功能之——项目之间的引用
    Mac 在启动eclipse时 Failed to load JavaHL Library解决方法
    MySQL Workbench update语句错误Error Code: 1175.
  • 原文地址:https://www.cnblogs.com/keithtt/p/6536885.html
Copyright © 2011-2022 走看看