mongodb分片集群安装

news/2024/7/23 15:45:28 标签: mongodb, 数据库, nosql

分片集群的搭建过程

  1. 配置 并启动confifig 节点集群

1.1节点1 confifig-17017.conf
# 数据库文件位置
dbpath=config/config1
#日志文件位置
logpath=config/logs/config1.log
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork = true
bind_ip=0.0.0.0
port = 17017
# 表示是一个配置服务器
configsvr=true
#配置服务器副本集名称
replSet=configsvr
1.2 节点2 confifig-17018.conf
dbpath=config/config2
#日志文件位置
logpath=config/logs/config.log
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork = true
bind_ip=0.0.0.0
port = 17018
# 表示是一个配置服务器
configsvr=true
#配置服务器副本集名称
replSet=configsvr
1.3 节点3 confifig-17019.conf
# 数据库文件位置
dbpath=config/config3
#日志文件位置
logpath=config/logs/config3.log
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork = true
bind_ip=0.0.0.0
port = 17019
# 表示是一个配置服务器
configsvr=true
#配置服务器副本集名称
replSet=configsvr
1.4 启动配置节点
./bin/mongod -f config/config-17017.conf
./bin/mongod -f config/config-17018.conf
./bin/mongod -f config/config-17019.conf
1.5 进入任意节点的mongo shell 并添加 配置节点集群 注意use admin
./bin/mongo --port 17017
use admin
var cfg ={"_id":"configsvr",
"members":[
{"_id":1,"host":"127.0.0.1:17017"},
{"_id":2,"host":"127.0.0.1:17018"},
{"_id":3,"host":"127.0.0.1:17019"}]
};
rs.initiate(cfg)
  1. 配置shard集群

2.1 shard1集群搭建37017到37019
dbpath=shard/shard1/shard1-37017
bind_ip=0.0.0.0
port=37017
fork=true
logpath=shard/shard1/shard1-37017.log
replSet=shard1
shardsvr=true

dbpath=shard/shard1/shard1-37018
bind_ip=0.0.0.0
port=37018
fork=true
logpath=shard/shard1/shard1-37018.log
replSet=shard1
shardsvr=true

dbpath=shard/shard1/shard1-37019
bind_ip=0.0.0.0
port=37019
fork=true
logpath=shard/shard1/shard1-37019.log
replSet=shard1
shardsvr=true

启动每个mongod 然后进入其中一个进行集群配置
var cfg ={"_id":"shard1",
"protocolVersion" : 1,
"members":[
{"_id":1,"host":"127.0.0.1:37017"},
{"_id":2,"host":"127.0.0.1:37018"},
{"_id":3,"host":"127.0.0.1:37019"}
]
};
rs.initiate(cfg)
rs.status()
2.2 shard2集群搭建47017到47019
dbpath=shard/shard2/shard2-47017
bind_ip=0.0.0.0
port=47017
fork=true
logpath=shard/shard2/logs/shard2-47017.log
replSet=shard2
shardsvr=true

dbpath=shard/shard2/shard2-47018
bind_ip=0.0.0.0
port=47018
fork=true
logpath=shard/shard2/logs/shard2-47018.log
replSet=shard2
shardsvr=true

dbpath=shard/shard2/shard2-47019
bind_ip=0.0.0.0
port=47019
fork=true
logpath=shard/shard2/logs/shard2-47019.log
replSet=shard2
shardsvr=true

启动每个mongod 然后进入其中一个进行集群配置
var cfg ={"_id":"shard2",
"protocolVersion" : 1,
"members":[
{"_id":1,"host":"127.0.0.1:47017"},
{"_id":2,"host":"127.0.0.1:47018"},
{"_id":3,"host":"127.0.0.1:47019"}
]
};
rs.initiate(cfg)
rs.status()
  1. 配置和启动 路由节点

vi route-27017.conf

添加以下内容:
port=27018
bind_ip=0.0.0.0
fork=true
logpath=route/logs/route.log
configdb=configsvr/127.0.0.1:17017,127.0.0.1:17018,127.0.0.1:17019

启动路由节点使用 mongos (注意不是mongod)

./bin/mongos -f route/route-27017.conf

4. mongos(路由)中添加分片节点

进入路由mongos

mongo --port 27017
sh.status()
sh.addShard("shard1/127.0.0.1:37017,127.0.0.1:37018,127.0.0.1:37019");
sh.addShard("shard2/127.0.0.1:47017,127.0.0.1:47018,127.0.0.1:47019");
sh.shardCollection("lagou_resume.lagou_resume_datas",{"name":"hashed"})
sh.status()

5. 开启数据库和集合分片(指定片键)

继续使用mongos完成分片开启和分片大小设置

数据库开启分片功能
sh.enableSharding("lagou_resume")
为指定集合开启分片功能
sh.shardCollection("lagou_resume.lagou_resume_datas",{"name":"hashed"})

6. 向集合中插入数据测试

通过路由循环向集合中添加数

use lagou_resume;
for(var i=1;i<= 1000;i++){
db.lagou_resume_datas.insert({"name":"test"+i,
salary:(Math.random()*20000).toFixed(2)});
}
  1. 验证分片效果


http://www.niftyadmin.cn/n/172866.html

相关文章

车载系统开发—【car-aosp】开发环境配置

简述 基于Android系统作为一个庞大的开源项目&#xff0c;除了一些谷歌自带服务之外&#xff0c;其他所有代码均以AOSP&#xff08;Android Open Source Project&#xff09;的形式开源。对于框架开发者来说&#xff0c;熟悉AOSP是必不可少的知识。即使是普通开发者&#xff0…

产品速看面试高频题,答案都给你了。

互联网中高薪&#xff0c;门槛相对较低的产品经理&#xff0c;正成为越来越多人的职业选择。而“工作流程”则是产品经理经常遇到的面试问题。 我们知道&#xff0c;产品经理的工作是非常琐碎的&#xff0c;如果细数下来&#xff0c;工作步骤需要将近20个环节&#xff0c;但面…

OpenLDAP+iRedMail+GitLab统一身份认证邮件代码仓库系统的解决方案

前言&#xff1a; iRedMail官方提供有各平台的自动安装脚本&#xff0c;整个安装过程并不复杂&#xff0c;重点在于安装后接入LDAP的配置。OpenLDAP的配置是出了名的恶心&#xff0c;尤其是网上大部分文档过于老旧&#xff0c;大多数情况下并不适用于当下。一、说明本文档操作环…

‘/’ 和 ‘%’ 在编程中的作用【附加练习题】

‘/’和‘%’在编程中有非常重要的作用&#xff0c;使用它们可以说是在使用一种简单算法&#xff0c;不仅易于理解&#xff0c;而且会极大的减少你的代码量&#xff0c;让你的程序看起来高级一点点&#x1f92a;&#xff01;/ 除我们通常都是除10的倍数&#xff0c;比如‘10’只…

程序员的代码行数越少越好?

有些人可能会认为&#xff0c;应用程序中的代码行越少&#xff0c;就越容易阅读。这句话只有部分正确&#xff0c;我认为代码可读性的度量标准包括&#xff1a;代码应具备一致性代码应具备自我描述性代码应具备良好的文档代码应使用稳定的现代功能代码不应过于复杂代码的性能不…

十七、网上商城项目(3)

本章概要 商品列表 商品列表项组件商品列表组件 分类商品和搜索结果页面 Loading 组件Books 组件 17.4 商品列表 商品列表页面以列表形式显示所有商品&#xff0c;将商品列表和商品列表项分别定义为单独的组件&#xff0c;商品列表组件作为父组件在其内部循环渲染商品列表项…

AVL树大讲堂

1.基础概念介绍 首先在前面我们介绍了二叉搜索树&#xff0c;但是如果当存储的数据接近有序或者恰巧有序的时候&#xff0c;二叉搜索树将逐渐退化为单支树&#xff0c;导致搜索效率降低&#xff0c;因此我们的avl树便为了解决这一问题而诞生了。 基础性质&#xff1a;当向二叉…

HTTP协议详解(二)

目录 1.HTTP 响应详解 1.1认识状态码(status code) 1.2 认识响应报头(header) 1.3 认识响应正文(body) 2.构造 HTTP 请求 2.1 通过form表单构造请求 2.2 通过ajax构造请求 2.3 使用第三方工具构造请求 开始之前我们先复习一下http协议格式 1.HTTP 响应详解 我们先抓包…