Mongodb 分片与副本集

Stella981
• 阅读 487

测试搭建
192.168.3.110
mongos 30000,30001,30002
config 40000,40001,40002
shard1 50001,50002,50003
shard2 50004,50005,50006,
shard3 50007,50008,50009
[root@mysql-slave10 data]# mkdir -p /data/config/1/data
[root@mysql-slave10 data]# mkdir -p /data/config/2/data
[root@mysql-slave10 data]# mkdir -p /data/config/3/data
[root@mysql-slave10 data]# mkdir -p /data/config/1/log
[root@mysql-slave10 data]# mkdir -p /data/config/2/log
[root@mysql-slave10 data]# mkdir -p /data/config/3/log
[root@mysql-slave10 data]# chown -R mongodb:mongodb /data/config/
[root@mysql-slave10 shard]# mkdir -p /data/shard/1_1/{data,log}
[root@mysql-slave10 shard]# mkdir -p /data/shard/1_2/{data,log}
[root@mysql-slave10 shard]# mkdir -p /data/shard/2_1/{data,log}
[root@mysql-slave10 shard]# mkdir -p /data/shard/2_2/{data,log}
[root@mysql-slave10 shard]# mkdir -p /data/shard/3_1/{data,log}
[root@mysql-slave10 shard]# mkdir -p /data/shard/3_2/{data,log}
[root@mysql-slave10 data]# chown -R mongodb:mongodb /data/shard/

  1. 启动配置服务器
    /usr/local/mongodb/bin/mongod --configsvr --dbpath=/data/config/1/data --port 40000 --logpath=/data/config/1/log/config.log --logappend --fork
    /usr/local/mongodb/bin/mongod --configsvr --dbpath=/data/config/2/data --port 40001 --logpath=/data/config/2/log/config.log --logappend --fork
    /usr/local/mongodb/bin/mongod --configsvr --dbpath=/data/config/3/data --port 40002 --logpath=/data/config/3/log/config.log --logappend --fork
  2. 启动MONGOS服务器
    [root@mysql-slave10 data]# hostname
    mysql-slave10
    /usr/local/mongodb/bin/mongos --configdb 192.168.3.110:40000,192.168.3.110:40001,192.168.3.110:40002 --port 30000 --logpath=/data/config/1/log/mongos.log --logappend --fork
    /usr/local/mongodb/bin/mongos --configdb 192.168.3.110:40000,192.168.3.110:40001,192.168.3.110:40002 --port 30001 --logpath=/data/config/2/log/mongos.log --logappend --fork
    /usr/local/mongodb/bin/mongos --configdb 192.168.3.110:40000,192.168.3.110:40001,192.168.3.110:40002 --port 30002 --logpath=/data/config/3/log/mongos.log --logappend --fork
  3. 启动分片及其副本集实例

shard1:
/usr/local/mongodb/bin/mongod --shardsvr --replSet shard1 --port 50001 --dbpath=/data/shard/1/data --logpath=/data/shard/1/log/shard.log --nojournal --oplogSize 10 --fork --logappend
/usr/local/mongodb/bin/mongod --shardsvr --replSet shard1 --port 50002 --dbpath=/data/shard/1_1/data --logpath=/data/shard/1_1/log/shard.log --nojournal --oplogSize 10 --fork --logappend
/usr/local/mongodb/bin/mongod --shardsvr --replSet shard1 --port 50003 --dbpath=/data/shard/1_2/data --logpath=/data/shard/1_2/log/shard.log --nojournal --oplogSize 10 --fork --logappend

shard2:
/usr/local/mongodb/bin/mongod --shardsvr --replSet shard2 --port 50004 --dbpath=/data/shard/2/data --logpath=/data/shard/2/log/shard.log --nojournal --oplogSize 10 --fork --logappend
/usr/local/mongodb/bin/mongod --shardsvr --replSet shard2 --port 50005 --dbpath=/data/shard/2_1/data --logpath=/data/shard/2_1/log/shard.log --nojournal --oplogSize 10 --fork --logappend
/usr/local/mongodb/bin/mongod --shardsvr --replSet shard2 --port 50006 --dbpath=/data/shard/2_2/data --logpath=/data/shard/2_2/log/shard.log --nojournal --oplogSize 10 --fork --logappend

shard3:
/usr/local/mongodb/bin/mongod --shardsvr --replSet shard3 --port 50007 --dbpath=/data/shard/3/data --logpath=/data/shard/3/log/shard.log --nojournal --oplogSize 10 --fork --logappend
/usr/local/mongodb/bin/mongod --shardsvr --replSet shard3 --port 50008 --dbpath=/data/shard/3_1/data --logpath=/data/shard/3_1/log/shard.log --nojournal --oplogSize 10 --fork --logappend
/usr/local/mongodb/bin/mongod --shardsvr --replSet shard3 --port 50009 --dbpath=/data/shard/3_2/data --logpath=/data/shard/3_2/log/shard.log --nojournal --oplogSize 10 --fork --logappend

启动分片副本集
登录各个副本集中的任意实例,进行副本集初始化

/usr/local/mongodb/bin/mongo 192.168.3.110:50001/admin
config={_id:"shard1",members:[
{_id:0,host:"192.168.3.110:50001","priority":2},
{_id:1,host:"192.168.3.110:50002"},
{_id:2,host:"192.168.3.110:50003",arbiterOnly:true}
]
}
> rs.initiate(config)
{ "ok" : 1 }

/usr/local/mongodb/bin/mongo 192.168.3.110:50004/admin
config={_id:"shard2",members:[
{_id:0,host:"192.168.3.110:50006",arbiterOnly:true},
{_id:1,host:"192.168.3.110:50005","priority":2},
{_id:2,host:"192.168.3.110:50004"}
]
}

> rs.initiate(config)
{ "ok" : 1 }

/usr/local/mongodb/bin/mongo 192.168.3.110:50007/admin
config={_id:"shard3",members:[
{_id:0,host:"192.168.3.110:50007"},
{_id:1,host:"192.168.3.110:50008",arbiterOnly:true},
{_id:2,host:"192.168.3.110:50009","priority":2}
]
}
> rs.initiate(config)
{ "ok" : 1 }
5) 设置分片,让分片生效
连接mongos,比如
/usr/local/mongodb/bin/mongo 192.168.3.110:30000
use admin
db.runCommand({addshard:"shard1/192.168.3.110:50002,192.168.3.110:50001,192.168.3.110:50003"});
db.runCommand({addshard:"shard2/192.168.3.110:50004,192.168.3.110:50005,192.168.3.110:50006"});
db.runCommand({addshard:"shard3/192.168.3.110:50007,192.168.3.110:50008,192.168.3.110:50009"});

[mongodb@mysql-slave10 bin]$ /usr/local/mongodb/bin/mongo 192.168.3.110:30000
MongoDB shell version: 3.2.11
connecting to: 192.168.3.110:30000/test
mongos> use admin
switched to db admin
mongos> db.runCommand({addshard:"shard1/192.168.3.110:50002,192.168.3.110:50001,192.168.3.110:50003"});
{ "shardAdded" : "shard1", "ok" : 1 }
mongos> db.runCommand({addshard:"shard2/192.168.3.110:50004,192.168.3.110:50005,192.168.3.110:50006"});
{ "shardAdded" : "shard2", "ok" : 1 }
mongos> db.runCommand({addshard:"shard3/192.168.3.110:50007,192.168.3.110:50008,192.168.3.110:50009"});
{ "shardAdded" : "shard3", "ok" : 1 }
mongos>
6) 查看分片
mongos> db.runCommand( { listshards : 1 } );
{
"shards" : [
{
"_id" : "shard1",
"host" : "shard1/192.168.3.110:50001,192.168.3.110:50002"
},
{
"_id" : "shard2",
"host" : "shard2/192.168.3.110:50004,192.168.3.110:50005"
},
{
"_id" : "shard3",
"host" : "shard3/192.168.3.110:50007,192.168.3.110:50009"
}
],
"ok" : 1
}

测试分片
mongos> db.runCommand({enablesharding:"testdb"}); #开启testdb分片功能
{ "ok" : 1 }
mongos> db.runCommand({shardcollection:"testdb.table1",key:{id:1}}); ##再对集合table1进行分片,id字段是片键。片键的选择:利于分块、分散写请求、查询数据
{ "collectionsharded" : "testdb.table1", "ok" : 1 }
mongos> use testdb;
switched to db testdb
mongos> for(var i=1;i<=100000;i++) db.table1.save({id:i,"test1":"testval1"});
WriteResult({ "nInserted" : 1 })

查阅数据分布情况
db.table1.stats();
查阅分片信息
分片集群的信息保存在congfig服务器,名为config的数据库里。通过mongos可以进行访问。
/usr/local/mongodb/bin/mongo 192.168.3.110:40000

  1. 查询当前的版本
    mongos> db.getCollection("version").findOne()
  2. 查询当前的配置CHUNKSIZE的大小
    mongos> db.settings.find()
  3. 查询整个SHARD集群的成员
    mongos> db.shards.find()
  4. 查询被水平拆分的集合:
    mongos> db.collections.find()
  5. 查询被水平拆分的集合分成的CHUNK分布
    mongos> db.chunks.find()
  6. 查询当前MongoDB里面的数据库的SHARD信息:
    mongos> db.databases.find()
  7. 查询MONGOS集合
    mongos> db.mongos.find()
  8. 查看整个MONGODB 数据库SHARD的详细信息
    mongos> printShardingStatus();
    db.CatchedFeature.getShardVersion();
    sh.status();

[mongodb@mysql-slave10 bin]$ /usr/local/mongodb/bin/mongo 192.168.3.110:40000
MongoDB shell version: 3.2.11
connecting to: 192.168.3.110:40000/test
Server has startup warnings:
2016-12-12T07:37:38.973-0800 I CONTROL [initandlisten]
2016-12-12T07:37:38.973-0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-12-12T07:37:38.973-0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-12-12T07:37:38.973-0800 I CONTROL [initandlisten]
2016-12-12T07:37:38.973-0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-12-12T07:37:38.973-0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-12-12T07:37:38.973-0800 I CONTROL [initandlisten]
configsvr> show dbs
config 0.001GB
local 0.000GB
configsvr> use config
switched to db config
configsvr> db.getCollection("version").findOne()
{
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("584ec460f771e03d38bcf2e1")
}
configsvr> db.settings.find()
{ "_id" : "chunksize", "value" : NumberLong(64) }
configsvr> db.shards.find()
{ "_id" : "shard1", "host" : "shard1/192.168.3.110:50001,192.168.3.110:50002" }
{ "_id" : "shard2", "host" : "shard2/192.168.3.110:50004,192.168.3.110:50005" }
{ "_id" : "shard3", "host" : "shard3/192.168.3.110:50007,192.168.3.110:50009" }
configsvr> db.collections.find()
{ "_id" : "testdb.table1", "lastmodEpoch" : ObjectId("584ec8bff771e03d38bcf359"), "lastmod" : ISODate("1970-02-19T17:02:47.296Z"), "dropped" : false, "key" : { "id" : 1 }, "unique" : false }
configsvr> db.chunks.find()
{ "_id" : "testdb.table1-id_MinKey", "lastmod" : Timestamp(2, 0), "lastmodEpoch" : ObjectId("584ec8bff771e03d38bcf359"), "ns" : "testdb.table1", "min" : { "id" : { "$minKey" : 1 } }, "max" : { "id" : 2 }, "shard" : "shard2" }
{ "_id" : "testdb.table1-id_2.0", "lastmod" : Timestamp(3, 0), "lastmodEpoch" : ObjectId("584ec8bff771e03d38bcf359"), "ns" : "testdb.table1", "min" : { "id" : 2 }, "max" : { "id" : 20 }, "shard" : "shard3" }
{ "_id" : "testdb.table1-id_20.0", "lastmod" : Timestamp(3, 1), "lastmodEpoch" : ObjectId("584ec8bff771e03d38bcf359"), "ns" : "testdb.table1", "min" : { "id" : 20 }, "max" : { "id" : { "$maxKey" : 1 } }, "shard" : "shard1" }
configsvr> db.databases.find()
{ "_id" : "testdb", "primary" : "shard1", "partitioned" : true }
configsvr> db.mongos.find()
{ "_id" : "mysql-slave10:30000", "ping" : ISODate("2016-12-12T16:01:32.959Z"), "up" : NumberLong(1403), "waiting" : true, "mongoVersion" : "3.2.11" }
{ "_id" : "mysql-slave10:30001", "ping" : ISODate("2016-12-12T16:01:33.379Z"), "up" : NumberLong(1396), "waiting" : true, "mongoVersion" : "3.2.11" }
{ "_id" : "mysql-slave10:30002", "ping" : ISODate("2016-12-12T16:01:37.317Z"), "up" : NumberLong(1394), "waiting" : true, "mongoVersion" : "3.2.11" }

---------
/usr/local/mongodb/bin/mongo 192.168.3.110:50001
[mongodb@mysql-slave10 bin]$ /usr/local/mongodb/bin/mongo 192.168.3.110:50001
MongoDB shell version: 3.2.11
connecting to: 192.168.3.110:50001/test
Server has startup warnings:
2016-12-12T07:38:43.936-0800 I CONTROL [initandlisten]
2016-12-12T07:38:43.936-0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-12-12T07:38:43.936-0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-12-12T07:38:43.936-0800 I CONTROL [initandlisten]
2016-12-12T07:38:43.936-0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-12-12T07:38:43.936-0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-12-12T07:38:43.936-0800 I CONTROL [initandlisten]
shard1:PRIMARY> show dbs;
local 0.005GB
testdb 0.003GB
shard1:PRIMARY> use testdb
switched to db testdb
shard1:PRIMARY> show tables;
table1
shard1:PRIMARY> db.table1.find().count()
99981
[mongodb@mysql-slave10 bin]$ /usr/local/mongodb/bin/mongo 192.168.3.110:50005
MongoDB shell version: 3.2.11
connecting to: 192.168.3.110:50005/test
Server has startup warnings:
2016-12-12T07:39:04.749-0800 I CONTROL [initandlisten]
2016-12-12T07:39:04.749-0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-12-12T07:39:04.749-0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-12-12T07:39:04.749-0800 I CONTROL [initandlisten]
2016-12-12T07:39:04.749-0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-12-12T07:39:04.749-0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-12-12T07:39:04.749-0800 I CONTROL [initandlisten]
shard2:PRIMARY> show dbs
local 0.000GB
testdb 0.000GB
shard2:PRIMARY> use testdb
switched to db testdb
shard2:PRIMARY> show tables;
table1
shard2:PRIMARY> db.table1.find().count()
1
shard2:PRIMARY> db.table1.find()
{ "_id" : ObjectId("584ec8ca2463dbba9dd3d488"), "id" : 1, "test1" : "testval1" }
shard2:PRIMARY> exit
bye
[mongodb@mysql-slave10 bin]$ /usr/local/mongodb/bin/mongo 192.168.3.110:50009
MongoDB shell version: 3.2.11
connecting to: 192.168.3.110:50009/test
Server has startup warnings:
2016-12-12T07:39:34.419-0800 I CONTROL [initandlisten]
2016-12-12T07:39:34.419-0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-12-12T07:39:34.419-0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-12-12T07:39:34.419-0800 I CONTROL [initandlisten]
2016-12-12T07:39:34.419-0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-12-12T07:39:34.419-0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-12-12T07:39:34.419-0800 I CONTROL [initandlisten]
shard3:PRIMARY> use testdb
switched to db testdb
shard3:PRIMARY> db.table1.find().count()
18
shard3:PRIMARY> db.table1.find()
{ "_id" : ObjectId("584ec8ca2463dbba9dd3d489"), "id" : 2, "test1" : "testval1" }
{ "_id" : ObjectId("584ec8ca2463dbba9dd3d48a"), "id" : 3, "test1" : "testval1" }
{ "_id" : ObjectId("584ec8ca2463dbba9dd3d48b"), "id" : 4, "test1" : "testval1" }
{ "_id" : ObjectId("584ec8ca2463dbba9dd3d48c"), "id" : 5, "test1" : "testval1" }
{ "_id" : ObjectId("584ec8ca2463dbba9dd3d48d"), "id" : 6, "test1" : "testval1" }
{ "_id" : ObjectId("584ec8ca2463dbba9dd3d48e"), "id" : 7, "test1" : "testval1" }
{ "_id" : ObjectId("584ec8ca2463dbba9dd3d48f"), "id" : 8, "test1" : "testval1" }
{ "_id" : ObjectId("584ec8ca2463dbba9dd3d490"), "id" : 9, "test1" : "testval1" }
{ "_id" : ObjectId("584ec8ca2463dbba9dd3d491"), "id" : 10, "test1" : "testval1" }
{ "_id" : ObjectId("584ec8ca2463dbba9dd3d492"), "id" : 11, "test1" : "testval1" }
{ "_id" : ObjectId("584ec8ca2463dbba9dd3d493"), "id" : 12, "test1" : "testval1" }
{ "_id" : ObjectId("584ec8ca2463dbba9dd3d494"), "id" : 13, "test1" : "testval1" }
{ "_id" : ObjectId("584ec8ca2463dbba9dd3d495"), "id" : 14, "test1" : "testval1" }
{ "_id" : ObjectId("584ec8ca2463dbba9dd3d496"), "id" : 15, "test1" : "testval1" }
{ "_id" : ObjectId("584ec8ca2463dbba9dd3d497"), "id" : 16, "test1" : "testval1" }
{ "_id" : ObjectId("584ec8ca2463dbba9dd3d498"), "id" : 17, "test1" : "testval1" }
{ "_id" : ObjectId("584ec8ca2463dbba9dd3d499"), "id" : 18, "test1" : "testval1" }
{ "_id" : ObjectId("584ec8ca2463dbba9dd3d49a"), "id" : 19, "test1" : "testval1" }

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4天前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
5个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这