环境准备

服务器规划

服务器[192.168.0.75] 服务器[192.168.0.84] 服务器[192.168.0.86]
mongos mongos mongos
config server config server config server
shard server1 主节点 shard server1 副节点 shard server1 仲裁
shard server2 仲裁 shard server2 主节点 shard server2 副节点
shard server3 副节点 shard server3 仲裁 shard server3 主节点

端口分配

mongos:20000

config:21000

shard1:27001

shard2:27002

shard3:27003

集群搭建

1、安装mongodb

分别在每台机器建立conf、mongos、config、shard1、shard2、shard3六个目录,因为mongos不存储数据,只需要建立日志文件目录即可。

mkdir -p /usr/local/mongodb/conf
mkdir -p /usr/local/mongodb/mongos/log
mkdir -p /usr/local/mongodb/config/data
mkdir -p /usr/local/mongodb/config/log
mkdir -p /usr/local/mongodb/shard1/data
mkdir -p /usr/local/mongodb/shard1/log
mkdir -p /usr/local/mongodb/shard2/data
mkdir -p /usr/local/mongodb/shard2/log
mkdir -p /usr/local/mongodb/shard3/data
mkdir -p /usr/local/mongodb/shard3/log

配置环境变量

vim /etc/profile
# 内容
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
# 使立即生效
source /etc/profile

2、config server配置服务器

mongodb3.4以后要求配置服务器也创建副本集,不然集群搭建不成功。

添加配置文件

vi /usr/local/mongodb/conf/config.conf

## 配置文件内容
pidfilepath = /usr/local/mongodb/config/log/configsrv.pid
dbpath = /usr/local/mongodb/config/data
logpath = /usr/local/mongodb/config/log/congigsrv.log
logappend = true bind_ip = 0.0.0.0
port = 21000
fork = true #declare this is a config db of a cluster;
configsvr = true #副本集名称
replSet=configs #设置最大连接数
maxConns=20000

启动三台服务器的config server

mongod -f /usr/local/mongodb/conf/config.conf

登录任意一台配置服务器,初始化配置副本集

#连接
mongo --port 21000
#config变量
config = {
... _id : "configs",
... members : [
... {_id : 0, host : "192.168.0.75:21000" },
... {_id : 1, host : "192.168.0.84:21000" },
... {_id : 2, host : "192.168.0.86:21000" }
... ]
... } #初始化副本集
rs.initiate(config)

其中,”_id” : “configs”应与配置文件中配置的 replicaction.replSetName 一致,”members” 中的 “host” 为三个节点的 ip 和 port

3、配置分片副本集(三台机器)

设置第一个分片副本集

配置文件

vi /usr/local/mongodb/conf/shard1.conf

#配置文件内容
#——————————————–
pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid
dbpath = /usr/local/mongodb/shard1/data
logpath = /usr/local/mongodb/shard1/log/shard1.log
logappend = true bind_ip = 0.0.0.0
port = 27001
fork = true #打开web监控
httpinterface=true
rest=true #副本集名称
replSet=shard1 #declare this is a shard db of a cluster;
shardsvr = true #设置最大连接数
maxConns=20000

启动三台服务器的shard1 server

mongod -f /usr/local/mongodb/conf/shard1.conf

登陆任意一台服务器,初始化副本集

mongo --port 27001
#使用admin数据库
use admin
#定义副本集配置,第三个节点的 "arbiterOnly":true 代表其为仲裁节点。
config = {
... _id : "shard1",
... members : [
... {_id : 0, host : "192.168.0.75:27001" },
... {_id : 1, host : "192.168.0.84:27001" },
... {_id : 2, host : "192.168.0.86:27001” , arbiterOnly: true }
... ]
... }
#初始化副本集配置
rs.initiate(config);

设置第二个分片副本集

配置文件

vi /usr/local/mongodb/conf/shard2.conf

#配置文件内容
#——————————————–
pidfilepath = /usr/local/mongodb/shard2/log/shard2.pid
dbpath = /usr/local/mongodb/shard2/data
logpath = /usr/local/mongodb/shard2/log/shard2.log
logappend = true bind_ip = 0.0.0.0
port = 27002
fork = true #打开web监控
httpinterface=true
rest=true #副本集名称
replSet=shard2 #declare this is a shard db of a cluster;
shardsvr = true #设置最大连接数
maxConns=20000

启动三台服务器的shard2 server

mongod -f /usr/local/mongodb/conf/shard2.conf

登陆任意一台服务器,初始化副本集

mongo --port 27002
#使用admin数据库
use admin
#定义副本集配置
config = {
... _id : "shard2",
... members : [
... {_id : 0, host : "192.168.0.75:27002" , arbiterOnly: true },
... {_id : 1, host : "192.168.0.84:27002" },
... {_id : 2, host : "192.168.0.86:27002" }
... ]
... } #初始化副本集配置
rs.initiate(config);

设置第三个分片副本集

配置文件

vi /usr/local/mongodb/conf/shard3.conf

#配置文件内容
#——————————————–
pidfilepath = /usr/local/mongodb/shard3/log/shard3.pid
dbpath = /usr/local/mongodb/shard3/data
logpath = /usr/local/mongodb/shard3/log/shard3.log
logappend = true bind_ip = 0.0.0.0
port = 27003
fork = true #打开web监控
httpinterface=true
rest=true #副本集名称
replSet=shard3 #declare this is a shard db of a cluster;
shardsvr = true #设置最大连接数
maxConns=20000

启动三台服务器的shard3 server

mongod -f /usr/local/mongodb/conf/shard3.conf

登陆任意一台服务器,初始化副本集

mongo --port 27003
#使用admin数据库
use admin
#定义副本集配置
config = {
... _id : "shard3",
... members : [
... {_id : 0, host : "192.168.0.75:27003" },
... {_id : 1, host : "192.168.0.84:27003" , arbiterOnly: true},
... {_id : 2, host : "192.168.0.86:27003" }
... ]
... } #初始化副本集配置
rs.initiate(config);

4、配置路由服务器 mongos

先启动配置服务器和分片服务器,后启动路由实例:(三台机器)

vi /usr/local/mongodb/conf/mongos.conf

#内容
pidfilepath = /usr/local/mongodb/mongos/log/mongos.pid
logpath = /usr/local/mongodb/mongos/log/mongos.log
logappend = true bind_ip = 0.0.0.0
port = 20000
fork = true #监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字
configdb = configs/192.168.0.75:21000,192.168.0.84:21000,192.168.0.86:21000 #设置最大连接数
maxConns=20000

启动三台服务器的mongos server

mongos -f /usr/local/mongodb/conf/mongos.conf

5、启用分片

目前搭建了mongodb配置服务器、路由服务器,各个分片服务器,不过应用程序连接到mongos路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效。

登陆任意一台mongos

mongo --port 20000
#使用admin数据库
use admin
#串联路由服务器与分配副本集
sh.addShard("shard1/192.168.0.75:27001,192.168.0.84:27001,192.168.0.86:27001")
sh.addShard("shard2/192.168.0.75:27002,192.168.0.84:27002,192.168.0.86:27002")
sh.addShard("shard3/192.168.0.75:27003,192.168.0.84:27003,192.168.0.86:27003")
#查看集群状态
sh.status()
③测试

目前配置服务、路由服务、分片服务、副本集服务都已经串联起来了,但我们的目的是希望插入数据,数据能够自动分片。连接在mongos上,准备让指定的数据库、指定的集合分片生效。

#指定testdb分片生效
db.runCommand( { enablesharding :"testdb"});
#指定数据库里需要分片的集合和片键
db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )

我们设置testdb的 table1 表需要分片,根据 id 自动分片到 shard1 ,shard2,shard3 上面去。要这样设置是因为不是所有mongodb 的数据库和表 都需要分片!

测试分片配置结果

mongo  127.0.0.1:20000
#使用testdb
use testdb;
#插入测试数据
for (var i = 1; i <= 100000; i++)
db.table1.save({id:i,"test1":"testval1"});
#查看分片情况如下,部分无关信息省掉了
db.table1.stats();{"sharded" : true,
"ns" : "testdb.table1",
"count" : 100000,
"numExtents" : 13,
"size" : 5600000,
"storageSize" : 22372352,
"totalIndexSize" : 6213760,
"indexSizes" : {"_id_" : 3335808,
"id_1" : 2877952
},
"avgObjSize" : 56,
"nindexes" : 2,
"nchunks" : 3,
"shards" : {"shard1" : {"ns" : "testdb.table1",
"count" : 42183,
"size" : 0,
...
"ok" : 1
},
"shard2" : {"ns" : "testdb.table1",
"count" : 38937,
"size" : 2180472,
...
"ok" : 1
},
"shard3" : {"ns" : "testdb.table1",
"count" :18880,
"size" : 3419528,
...
"ok" : 1
}},
"ok" : 1
}

可以看到数据分到3个分片,各自分片数量为: shard1 “count” : 42183,shard2 “count” : 38937,shard3 “count” : 18880。已经成功了!

后期运维

启动关闭

mongodb的启动顺序是,先启动配置服务器,在启动分片,最后启动mongos.

mongod -f /usr/local/mongodb/conf/config.conf
mongod -f /usr/local/mongodb/conf/shard1.conf
mongod -f /usr/local/mongodb/conf/shard2.conf
mongod -f /usr/local/mongodb/conf/shard3.conf
mongod -f /usr/local/mongodb/conf/mongos.conf

关闭时,直接killall杀掉所有进程

killall mongod
killall mongos
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

第43章:MongoDB-集群--Sharding(分片)--多机的搭建的更多相关文章

  1. 第41章:MongoDB-集群--Sharding(分片)

    ①Sharding分片概念 分片(sharding)是指将数据库拆分,将其分散在不同的机器上的过程.将数据分散到不同的机器上,不需要功能强大的服务器就可以存储更多的数据和处理更大的负载. 分片是每个分 ...

  2. Mongodb集群与分片 1

    分片集群   Mongodb中数据分片叫做chunk,它是一个Collection中的一个连续的数据记录,但是它有一个大小限制,不可以超过200M,如果超出产生新的分片.   下面是一个简单的分片集群 ...

  3. MongoDB集群之分片

    原文:点击打开链接 MongoDB分片 分片(sharding)是将数据拆分,将其分散在不同的机器上的过程.MongoDB支持自动分片 片键(shard key)设置分片时,需要从集合里面选一个键,用 ...

  4. mongodb集群配置分片集群

    测试环境 操作系统:CentOS 7.2 最小化安装 主服务器IP地址:192.168.197.21 mongo01 从服务器IP地址:192.168.197.22 mongo02 从服务器IP地址: ...

  5. Mongodb集群与分片 2

    前面我们介绍了简单的集群配置实例.在简单实例中,虽然MongoDB auto-Sharding解决了海量存储问题,和动态扩容问题,但是离我们在真实环境下面所需要的高可靠性和高可用性还有一定的距离. 下 ...

  6. mongodb 集群部署--分片服务器搭建

    部署分片服务器 1.分片 为了突破单点数据库服务器的I/O能力限制,对数据库存储进行水平扩展,严格地说,每一个服务器或者实例或者复制集就是一个分片. 2.优势 提供类似现行增·长架构 提高数据可用性 ...

  7. MongoDB集群之分片技术应用 —— 学习笔记

    课程链接:https://www.imooc.com/learn/501 一.什么是分片? 分片:将数据进行2拆分,将数据水平的分散到不同的服务器上. 二.为什么要分片? 架构上:读写均衡.去中心化 ...

  8. 第42章:MongoDB-集群--Sharding(分片)--单机的搭建

    ①配置服务器 在大型的集群中,建议配置3台配置服务器,就足够用了.启动配置服务器的方式: 1:先创建几个存放数据的文件夹,比如在前面的dbs下面创建confdb文件夹,然后在confdb下面创建con ...

  9. ubuntu docker 下mongodb集群和分片

    首先我们计划启动了三个mongo服务:master,salve,arbiter 1.准备工作 新建文件夹如图(每个文件夹下面有db和configdb文件夹): 生成认证文件并修改权限 openssl ...

随机推荐

  1. Linux上Oracle18C 系统表空间大小调整 adjust the system tablespace for oracle18c on linux

    1. 测试过程中,发现  system tablespace is not enough, need to adjustment 初步解决方案: 查询一下 system tablespace的大小 S ...

  2. 用word发布CSDN文章

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  3. 查找运行时间超过1天的frmweb进程

    EBS中经常会出现一些zombie的frmweb进程,特征是运行时间很长,占用CPU都(1个frmweb进程占用1个CPU线程100%资源)下面脚本是利用awk找到运行时间超过1天的frmweb进程, ...

  4. getBoundingClientRect获取元素在页面上的位置

    getBoundingClientRect用于获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置. getBoundingClientRect是DOM元素到浏览器可视范围的距离(不包含文档卷起 ...

  5. MemoryStream说明

    MemoryStream 是内存流,为系统内存提供读写操作,由于 MemoryStream 是通过无符号字节数组组成的,可以说 MemoryStream 的性能可以算比较出色,所以它担当起了一些其他流 ...

  6. tensorflow models api:ValueError: Tensor conversion requested dtype string for Tensor with dtype float32: 'Tensor("arg0:0", shape=(), dtype=float32, device=/device:CPU:0)'

    tensorflow models api:ValueError: Tensor conversion requested dtype string for Tensor with dtype flo ...

  7. linux 下oracle导入dmp文件

    2017年08月01日 19:10:49 xuxie13 阅读数:17788   最近要到某公司进行poc演示,整了个新项目,需要我数据库修改项目,这才有了这篇博客. 首先进入linux下oracle ...

  8. shell 启动和停止脚本

    启动脚本 start_kmeans_v3.sh #!/bin/bash #用于kmeans_data_v3_hadle启动 ps -ef | grep kmeans_data_v3_hadle.py ...

  9. Linux下使用ps命令查看某个进程文件的启动位置

    ps -ef|grep shutdown ls -al /proc/4170

  10. python 网络编程粘包解决方案2 + ftp上传 + socketserver

    一.struct 神奇的打包工具 struct 代码: import struct num = 156 #将int类型的数据打包成4个字节的数据 num_stru = struct.pack('i', ...