mongodb搭建集群

本文涉及的产品
云数据库 MongoDB,通用型 2核4GB
简介:
如果想配置2个mongos,1个config,多个mongod也可以 
mongos比较耗cpu,mongod比较耗内存,但其实负载都很低。 

机器1:1个mongos和1个config 
[root@iZ28s3djhumZ ~]# ps -ef|grep mongo 
root     13617     1  0 17:12 ?        00:00:07 mongod --dbpath=/mongo_config --port 27030 --fork --logpath=/mongo_config/mongo_config.log 
root     13654     1  0 17:19 ?        00:00:01 mongos --port 27031 --configdb=42.96.130.106:27030 --fork --logpath=/mongos/mongos.log 

机器2:1个mongos和1个mongod 
[root@iZ28b2hr3cxZ /]# ps -ef|grep mongo 
root      9693     1  0 17:15 ?        00:00:05 mongod --dbpath=/mongo2 --port 27032 --fork --logpath=/mongo2/mongo2.log 
root      9704     1  0 17:16 ?        00:00:02 mongos --port 27033 --configdb=42.96.130.106:27030 --fork --logpath=/mongos2/mongos.log 

机器3-n:都是mongod 
root     29486     1  0 17:17 ?        00:00:05 mongod --dbpath=/mongo --port 27036 --fork --logpath=/mongo/mongo5.log 

使用命令: 
[root@iZ28s3djhumZ ~]# mongo 42.96.130.106:27031/admin 
[root@iZ28b2hr3cxZ /]# mongo 121.42.169.214:27033/admin 
一边操作,另一边可以同步看到结果,因此就相当于一个副本。 


以上的配置,当数据量达到1300万的时候,mongodb的插入变慢,约为100w/h   之前可以达到120-130w/h 
现在考虑要做负载均衡和修改chunk值来减少数据的迁移 



开始进行集群搭建 
--- 
1、理解概念 
  mongos:首先我们要了解”片键“的概念,也就是说拆分集合的依据是什么?按照什么键值进行拆分集合....好了,mongos就是一个路由服务器,它会根据管理员设置的“片键”将数据分摊到自己管理的mongod集群,数据和片的对应关系以及相应的配置信息保存在"config服务器"上。 
  mongod:一个普通的数据库实例,如果不分片的话,我们会直接连上mongod。 
首先我们准备4个mongodb程序,直接在12.104,12.107两条机器上部署,可以做多个文件夹的形式。 

2、开启config服务器 
 先前也说了,mongos要把mongod之间的配置放到config服务器里面,理所当然首先开启它,我这里就建立27018端口。 
mongod --dbpath=/mongo --port 27018 
config 
192.168.12.107 /mongo    --端口27018 

3、开启mongos服务器 
 这里要注意的是我们开启的是mongos,不是mongod,同时指定下config服务器 
mongod --dbpath=/mongos  --port=27020  --configdb=192.168.12.107:27018 
mongos 
192.168.12.107 /mongos   --端口27020 
[root@viptest2 /]# mongos --port 27020 --configdb=127.0.0.1:27018  --不能指定本地localhost或者127.0.0.1,要使用ip 
[root@viptest2 /]# mongos --port 27020 --configdb=192.168.12.107:27018 

4、启动mongod服务器 
  对分片来说,也就是要添加片了,端口为:27017,27019。 
[root@testvip1 /]# mkdir mongo1 
[root@testvip1 /]# mkdir mongo2 
[root@testvip1 /]# mongod --dbpath=/mongo1  --port 27017 

再开一个窗口 
[root@testvip1 /]# mongod --dbpath=/mongo2  --port 27019 


5、服务配置 
 <1> 先前图中也可以看到,我们client直接跟mongos打交道,也就说明我们要连接mongos服务器,然后将27017,27019的mongod交给mongos,添加分片也就是addshard()。 
  这里要注意的是,在addshard中,我们也可以添加副本集,这样能达到更高的稳定性。 
在12.107 mongos上配置分片信息 
[root@viptest2 ~]# mongo 192.168.12.107:27020/admin 
MongoDB shell version: 2.4.6 
connecting to: 192.168.12.107:27020/admin 
Welcome to the MongoDB shell. 
For interactive help, type "help". 
For more comprehensive documentation, see 
http://docs.mongodb.org/ 
Questions? Try the support group 
http://groups.google.com/group/mongodb-user 


addshard 遇到的错误 
db.runCommand({addshard:”192.168.12.104:27017″}) 

“ok” : 0, 
“errmsg”: “can’t use localhost as a shard since all shards need to communicate. either use all shards and configdbs in localhost or all in actual IPs host: 192.168.12.104:27017 isLocalHost:0″ 


遇到这样的错误是由于某些服务启动在localhost 地址。 
经过检查发现route 启动时,读取config 服务是读取的localhost 地址: 
将localhost 修改为IP 地址,问题解决。 

重新进入: 
[root@viptest2 ~]# mongo 192.168.12.107:27020/admin 
MongoDB shell version: 2.4.6 
connecting to: 192.168.12.107:27020/admin 
mongos> 

mongos> db.runCommand({addshard:"192.168.12.104:27019",allowLocal:true }) 
当路由进程和分片在同一台机器上要指定allowLocal为true,因为MongoDB尽量避免错误的配置,将集群配置在本地,所以这个配置指明当前仅仅是用于开发。 

因为路由和分片不在同一台机器上,因此不需要执行true,如下添加分片成功 
mongos> db.runCommand({"addshard":"192.168.12.104:27017"}); 
{ "shardAdded" : "shard0000", "ok" : 1 } 
mongos> db.runCommand({"addshard":"192.168.12.104:27019"}); 
{ "shardAdded" : "shard0001", "ok" : 1 } 


<2>mongos不知道该如何切分数据,也就是我们先前所说的片键,在mongodb中设置片键要做两步 
  ①:开启数据库分片功能,命令很简单 enablesharding(),这里我就开启test数据库。 
  ②:指定集合中分片的片键,这里我就指定为person.name字段。 

mongos> show collections;  --查看表 
mongos> db.runCommand({"drop":"t1"}) --删除表  ---db.t1.drop()一样的效果 

"nIndexesWas" : 1, 
"msg" : "indexes dropped for collection", 
"ns" : "testdb.t1", 
"ok" : 1 

mongos> show dbs;  --查看数据库 
admin (empty) 
config 0.1875GB 

mongos> use testdb  --创建数据库,必须做一些操作,如果是建的空库,就会自动被删除 
switched to db testdb 
mongos> db.usr.insert({"name":"tom"}); --创建表 
mongos> show collections; 
system.indexes 
usr 
mongos> db.usr.find();   --查找表 
{ "_id" : ObjectId("541a4f38156a058cc29cce8e"), "name" : "tom" } 
mongos> db.usr.insert({"name":"tom","id":1,"address":"China","phone":"15926492390","sex":"M"})--再插入一条 
mongos> db.usr.find();  --结果显示两条 
{ "_id" : ObjectId("541a4f38156a058cc29cce8e"), "name" : "tom" } 
{ "_id" : ObjectId("541a4fcd156a058cc29cce8f"), "name" : "tom", "id" : 1, "address" : "China", "phone" : "15926492390", "sex" : "M" } 

mongos> db.runCommand({"enablesharding":"testdb"}) 
{ "ok" : 0, "errmsg" : "access denied - use admin db" } --要使用admin数据库进行配置 

mongos> use admin 
switched to db admin 
mongos> db.runCommand({"enablesharding":"testdb"}) --切换到admin果然配置成功 
{ "ok" : 1 } 

表进行分片 
mongos> db.runCommand({"shardcollection":"testdb.usr","key":{"id":1}}) 

"proposedKey" : { 
"id" : 1 
}, 
"curIndexes" : [ 

"v" : 1, 
"key" : { 
"_id" : 1 
}, 
"ns" : "testdb.usr", 
"name" : "_id_" 

], 
"ok" : 0, 
"errmsg" : "please create an index that starts with the shard key before sharding." --提示需要先建索引 


原来判断分支是查找是否有可用的索引存在,当无可用的索引,并且表不为空时,就会出现这个错误信息。 
好了找到根源了,现在解决问题:在分片key上个建立索引 
use testdb 
db.usr.ensureIndex({"id":1}) 
然后对表进行分片 
mongos> use admin 
switched to db admin 
mongos> db.runCommand({"shardcollection":"testdb.usr","key":{"id":1}}) 

"ok" : 0, 
"errmsg" : "found missing value in key { id: null } for doc: { _id: ObjectId('541a4f38156a058cc29cce8e'), name: \"tom\" }"--最开始插入的一条有问题,没有id这个字段 

修改这条记录: 
mongos> db.usr.update({"name":"tom"},{"name":"tina","id":2,"address":"US","phone":"18707551657","sex":"F"}) 
mongos> db.usr.find(); 
{ "_id" : ObjectId("541a4fcd156a058cc29cce8f"), "name" : "tom", "id" : 1, "address" : "China", "phone" : "15926492390", "sex" : "M" } 
{ "_id" : ObjectId("541a4f38156a058cc29cce8e"), "name" : "tina", "id" : 2, "address" : "US", "phone" : "18707551657", "sex" : "F" } 
结果显示: 
mongos> db.runCommand({"shardcollection":"testdb.usr","key":{"id":1}})  
{ "collectionsharded" : "testdb.usr", "ok" : 1 }   --创建成功,以id为分片的主键 

6、 查看效果 
  好了,至此我们的分片操作全部结束,接下来我们通过mongos向mongodb插入多条记录,然后通过printShardingStatus命令查看mongodb的数据分片情况。 

  这里主要看三点信息: 
  ① shards: 我们清楚的看到已经别分为两个片了,shard0000和shard0001。 
  ② databases: 这里有个partitioned字段表示是否分区,这里清楚的看到已经分区。 
  ③ chunks: 集合 

mongos> use testdb 
switched to db testdb 
mongos> db.printShardingStatus() 
--- Sharding Status --- 
  sharding version: { 
"_id" : 1, 
"version" : 3, 
"minCompatibleVersion" : 3, 
"currentVersion" : 4, 
"clusterId" : ObjectId("541a47a9124d847f09e99204") 

  shards: 
{  "_id" : "shard0000",  "host" : "192.168.12.104:27017" } 
{  "_id" : "shard0001",  "host" : "192.168.12.104:27019" } 
  databases: 
{  "_id" : "admin",  "partitioned" : false,  "primary" : "config" } 
{  "_id" : "testdb",  "partitioned" : true,  "primary" : "shard0000" } 
testdb.usr 
shard key: { "id" : 1 } 
chunks: 
shard0000 1 
{ "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0) 

--插入1000条数据 
mongos> for (var i=0;i<1000;i++){db.usr.insert({"name":"tina"+i,"id":3+i,"address":"China","phone":15926492390+i,"sex":"M"})} 


--在路由mongo上连接单个分片 
[root@testvip1 ~]# mongo 192.168.12.104:27017   
MongoDB shell version: 2.4.6 
connecting to: 192.168.12.104:27017/test 
> use testdb 
switched to db testdb 
> show collections 
system.indexes 
usr 
> db.usr.find() 
{ "_id" : ObjectId("541a4fcd156a058cc29cce8f"), "name" : "tom", "id" : 1, "address" : "China", "phone" : "15926492390", "sex" : "M" } 
{ "_id" : ObjectId("541a4f38156a058cc29cce8e"), "name" : "tina", "id" : 2, "address" : "US", "phone" : "18707551657", "sex" : "F" } 
{ "_id" : ObjectId("541a5455156a058cc29cce91"), "id" : 3, "name" : "tina0", "address" : "China", "phone" : 15926492390, "sex" : "M" } 
Type "it" for more 
> db.usr.status() 
Thu Sep 18 14:54:19.410 TypeError: Property 'status' of object testdb.usr is not a function 
> db.usr.status 
testdb.usr.status 
> db.usr.status() 
Thu Sep 18 14:54:31.494 TypeError: Property 'status' of object testdb.usr is not a function 
> db.usr.count()    --以id做key,结果全部分到一个分片上了。 
35407 

------------------- 
相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。 &nbsp; 相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
相关文章
|
5月前
|
NoSQL Cloud Native MongoDB
MongoDB 主从集群 2
MongoDB 主从集群 2
|
6月前
|
运维 NoSQL 安全
【最佳实践】高可用mongodb集群(1分片+3副本):规划及部署
结合我们的生产需求,本次详细整理了最新版本 MonogoDB 7.0 集群的规划及部署过程,具有较大的参考价值,基本可照搬使用。 适应数据规模为T级的场景,由于设计了分片支撑,后续如有大数据量需求,可分片横向扩展。
485 1
|
5月前
|
存储 NoSQL 网络协议
MongoDB 主从集群 1
MongoDB 主从集群 1
|
1月前
|
NoSQL MongoDB
搭建MongoDB分片式集群
搭建MongoDB分片式集群
13 0
|
2月前
|
存储 缓存 NoSQL
MongoDB详解(三)——MongoDB集群
MongoDB详解(三)——MongoDB集群
35 4
|
6月前
|
NoSQL MongoDB
MongoDB分片+副本集高可用集群的启停步骤
MongoDB分片+副本集高可用集群的启停步骤
139 0
|
7月前
|
存储 NoSQL 网络安全
如何开通MongoDB的专属集群
本案例旨在展示如何开通MongoDB的专属集群。
68 1
|
4月前
|
NoSQL MongoDB Docker
docker搭建mongodb集群
docker搭建mongodb集群
86 0
|
4月前
|
Kubernetes NoSQL MongoDB
k8s教程(pod篇)-使用StatefulSet搭建MongoDB集群
k8s教程(pod篇)-使用StatefulSet搭建MongoDB集群
445 1
|
7月前
|
存储 NoSQL 关系型数据库
MongoDB 的集群架构与设计
MongoDB 的集群架构与设计
1805 0