Docker方式部署

为了避免过分冗余,并且在主节点挂了,还能顺利自动提升,所以加入仲裁节点

mongodb版本:

环境:一台虚拟机

三个configsvr 副本: 端口为 27020,27021,27022

两个分片:

  shard1:-> 三个副本,端口为 27010,27011,27012

  shard2:-> 三个副本,端口为 27013,27014,27015

一个路由:mongos -> 端口为 27023

前置条件:

创建数据存储文件的目录

mkdir /usr/local/mongodb/data
mkdir /usr/local/mongodb/log
cd /usr/local/mongodb/data
mkdir c0 && mkdir c1 && mkdir c2 && mkdir s100 && mkdir s101 && mkdir s102 && mkdir s200 && mkdir s201 && mkdir s202

生成鉴权需要的keyfile,keyfile 内容不能太长,否则启动不了,权限不能太大,否则也是启动不了

openssl rand -base64 512 > /usr/local/mongodb/keyfile
chmod 600 /usr/local/mongodb/keyfile

副本模式启动configsvr

 mongod --dbpath /usr/local/mongodb/data/c0 --logpath /usr/local/mongodb/log/c0.log --fork --smallfiles --port  --replSet cs --configsvr --bind_ip=192.168.1.9
mongod --dbpath /usr/local/mongodb/data/c1 --logpath /usr/local/mongodb/log/c1.log --fork --smallfiles --port --replSet cs --configsvr --bind_ip=192.168.1.9
mongod --dbpath /usr/local/mongodb/data/c2 --logpath /usr/local/mongodb/log/c2.log --fork --smallfiles --port --replSet cs --configsvr --bind_ip=192.168.1.9

集群配置,登陆任意一个configsvr

mongo 192.168.1.9:
var css={_id:"cs","configsvr":true,members:[{_id:,host:"192.168.1.9:27020"},{_id:,host:"192.168.1.9:27021"},{_id:,host:"192.168.1.9:27022"}]}
rs.initiate(css)

副本模式启动分片1

 mongod --dbpath /usr/local/mongodb/data/s100 --logpath /usr/local/mongodb/log/s100.log --fork --smallfiles --port  --replSet shard1 --shardsvr --bind_ip=192.168.1.9
mongod --dbpath /usr/local/mongodb/data/s101 --logpath /usr/local/mongodb/log/s101.log --fork --smallfiles --port --replSet shard1 --shardsvr --bind_ip=192.168.1.9
mongod --dbpath /usr/local/mongodb/data/s102 --logpath /usr/local/mongodb/log/s102.log --fork --smallfiles --port --replSet shard1 --shardsvr --bind_ip=192.168.1.9

登陆任意一个分片1

use admin;
var cnf={_id:"shard1",members:[{_id:,host:"192.168.1.9:27017"},{_id:,host:"192.168.1.9:27018"}]}
rs.initiate(cnf)
rs.addArb("192.168.1.9:27019")#仲裁节点

副本模式启动分片2

 mongod --dbpath /usr/local/mongodb/data/s100 --logpath /usr/local/mongodb/log/s100.log --fork --smallfiles --port  --replSet shard2 --shardsvr --bind_ip=192.168.1.9
mongod --dbpath /usr/local/mongodb/data/s101 --logpath /usr/local/mongodb/log/s101.log --fork --smallfiles --port --replSet shard2 --shardsvr --bind_ip=192.168.1.9
mongod --dbpath /usr/local/mongodb/data/s102 --logpath /usr/local/mongodb/log/s102.log --fork --smallfiles --port --replSet shard2 --shardsvr --bind_ip=192.168.1.9

登陆任意一个分片2,操作同分片1

启动路由

mongos --logpath /usr/local/mongodb/log/m23.log --port  --fork --configdb cs/192.168.1.9:,192.168.1.9:,192.168.1.9: --bind_ip=192.168.1.9

登陆路由

mongo 192.168.1.9:

添加分片,这里写全了分片的 host, 实际只需要添加其中一个副本或者仲裁host即可 可选192.168.1.9:27010 192.168.1.9:27011 192.168.1.9:27012 其中一个

设置分片数据库,设置片键

mongos> sh.enableSharding("testdb")
mongos> sh.shardCollection("testdb.orderInfo",{"_id":"hashed"})
{ "collectionsharded" : "testdb.orderInfo", "ok" : }

趁还没有加上鉴权,赶紧添加用户

use admin
db.createUser(
{
user: "dba",
pwd: "dba",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
use testdb
mongos> db.createUser(
...   {
...     user: "testuser",
...     pwd: "testuser",
...     roles: [ { role: "readWrite", db: "testdb" } ]
...   }
... )
Successfully added user: {
"user" : "testuser",
"roles" : [
{
"role" : "readWrite",
"db" : "testdb"
}
]
}
mongos> db.auth("testuser","testuser") mongos> exit

然后依次关闭mongodb,等下添加鉴权再启动.

因为懒,我选择重启,自己的电脑,随便整,别太较真.......

依次启动mongod,这次加上鉴权参数   --keyFile /usr/local/mongodb/keyfile

mongod --dbpath /usr/local/mongodb/data/c0 --logpath /usr/local/mongodb/log/c0.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port  --replSet cs --configsvr --bind_ip=192.168.1.9
mongod --dbpath /usr/local/mongodb/data/c1 --logpath /usr/local/mongodb/log/c1.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port --replSet cs --configsvr --bind_ip=192.168.1.9
mongod --dbpath /usr/local/mongodb/data/c2 --logpath /usr/local/mongodb/log/c2.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port --replSet cs --configsvr --bind_ip=192.168.1.9 mongod --dbpath /usr/local/mongodb/data/s100 --logpath /usr/local/mongodb/log/s100.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port --replSet shard1 --shardsvr --bind_ip=192.168.1.9
mongod --dbpath /usr/local/mongodb/data/s101 --logpath /usr/local/mongodb/log/s101.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port --replSet shard1 --shardsvr --bind_ip=192.168.1.9
mongod --dbpath /usr/local/mongodb/data/s102 --logpath /usr/local/mongodb/log/s102.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port --replSet shard1 --shardsvr --bind_ip=192.168.1.9 mongod --dbpath /usr/local/mongodb/data/s100 --logpath /usr/local/mongodb/log/s100.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port --replSet shard2 --shardsvr --bind_ip=192.168.1.9
mongod --dbpath /usr/local/mongodb/data/s101 --logpath /usr/local/mongodb/log/s101.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port --replSet shard2 --shardsvr --bind_ip=192.168.1.9
mongod --dbpath /usr/local/mongodb/data/s102 --logpath /usr/local/mongodb/log/s102.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port --replSet shard2 --shardsvr --bind_ip=192.168.1.9
mongos --logpath /usr/local/mongodb/log/m23.log --port --fork --keyFile /usr/local/mongodb/keyfile --configdb cs/192.168.1.9:,192.168.1.9:,192.168.1.9: --bind_ip=192.168.1.9

测试:

[root@ conf]# mongo 192.168.1.9:
MongoDB shell version v3.4.10
connecting to: 192.168.1.9:
MongoDB server version: 3.4.
mongos> use testdb
switched to db testdb
mongos> db.auth("testuser","testuser") mongos> db.auth("testuser","testuser") mongos> exit
bye

或者

mongo 192.168.1.9:/testdb -u testuser -p

测试不使用账号密码

使用dba登陆,创建超级管理员用户,否则没有sh权限

mongos> use admin
switched to db admin
mongos> db.createUser(
... {
... user: "root",
... pwd: "root",
... roles: [ { role: "root", db: "admin" } ]
... }
... )
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
mongos> db.auth("root","root")
1
mongos> sh.status();
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5a0d5d371c121ebe9dcbdace")
}
shards:
{ "_id" : "shard1", "host" : "shard1/192.168.1.9:27010,192.168.1.9:27011,192.168.1.9:27012", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/192.168.1.9:27013,192.168.1.9:27014,192.168.1.9:27015", "state" : 1 }
active mongoses:
"3.4.10" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
NaN
Failed balancer rounds in last 5 attempts: 2
Last reported error: could not find host matching read preference { mode: "primary" } for set shard1
Time of Reported error: Fri Nov 17 2017 08:33:47 GMT+0800 (CST)
Migration Results for the last 24 hours:
28 : Success
1 : Failed with error 'aborted', from shard2 to shard1
databases:
{ "_id" : "testdb", "primary" : "shard2", "partitioned" : true }
testdb.orderInfo
shard key: { "taxNo" : 1, "lastModifyDate" : 1 }
unique: false
balancing: true
chunks:
shard1 28
shard2 29
too many chunks to print, use verbose if you want to force print

补充:

集群用户不能用来认证单个shard节点,必须要在shard节点单独建立用户

以下测试,片键为公司编码以及用户名,复合键片,实现多热点

插入500W 单文档数据,不包含内嵌文档,在NVME 固态硬盘下表现 速度非常快.只用了56秒

完成插入查看数据分布情况

分布不太均衡,等一会再看一次

非常均衡,因为我用了9个公司编码,14个随机用户名插入.但是只有两个分片,所以导致数据会分发倒第一次插入的分片中,导致数据需要频繁自动均衡.建议有条件的,初始化的时候,创建多个分片

mongodb 3.4 分片 一主 一副 一仲 鉴权集群部署.的更多相关文章

  1. Docker mongodb 3.4 分片 一主 一副 一仲 鉴权集群部署.

    非docker部署 为了避免过分冗余,并且在主节点挂了,还能顺利自动提升,所以加入仲裁节点 为什么要用docker部署,因为之前直接在虚拟机启动10个mongod 进程.多线程并发测试的时候,mong ...

  2. 搭建一个分布式MongoDB鉴权集群

    今天休假在家,测试并搭建了一个replica set shard MongoDB鉴权集群.replica set shard 鉴权集群中文资料比较少,本文是个人笔记,同时也希望对后来者有所帮助.本文仅 ...

  3. MongoDB DBA 实践8-----Linux系统Mongodb分片集群部署

    在Linux系统中,主要是使用命令行进行mongodb的分片集群部署 一.先决条件 mongodb安装成功,明确路径, MongoDB的几个路径: /var/lib/mongodb /var/log/ ...

  4. MongoDB(7):集群部署实践,包含复制集,分片

    注: 刚开始学习MongoDB,写的有点麻烦了,网上教程都是很少的代码就完成了集群的部署, 纯属个人实践,错误之处望指正!有好的建议和资料请联系我QQ:1176479642 集群架构: 2mongos ...

  5. MongoDB分片集群部署方案

    前言 副本集部署是对数据的冗余和增加读请求的处理能力,却不能提高写请求的处理能力:关键问题是随着数据增加,单机硬件配置会成为性能的瓶颈.而分片集群可以很好的解决这一问题,通过水平扩展来提升性能.分片部 ...

  6. Mongodb集群部署ReplicaSet+Sharding -摘自网络

    网上关于三种集群方式的搭建方式很多,都是分开来介绍的.Replica Set (复制集成)主要是做主从库的,但是没法实现负载均衡的效果,真正实现这个的,是Sharding(分片集群),通过数据分布在每 ...

  7. mtools 是由MongoDB 官方工程师实现的一套工具集,可以很快速的日志查询分析、统计功能,此外还支持本地集群部署管理.

    mtools 是由MongoDB 官方工程师实现的一套工具集,可以很快速的日志查询分析.统计功能,此外还支持本地集群部署管理 https://www.cnblogs.com/littleatp/p/9 ...

  8. 图解MongoDB集群部署原理(3)

    MongoDB的集群部署方案中有三类角色:实际数据存储结点.配置文件存储结点和路由接入结点. 连接的客户端直接与路由结点相连,从配置结点上查询数据,根据查询结果到实际的存储结点上查询和存储数据.Mon ...

  9. RocketMQ学习笔记(16)----RocketMQ搭建双主双从(异步复制)集群

    1. 修改RocketMQ默认启动端口 由于只有两台机器,部署双主双从需要四个节点,所以只能修改rocketmq的默认启动端口,从官网下载rocketmq的source文件,解压后使用idea打开,全 ...

随机推荐

  1. MySql按日期进行统计

    1 数据库字段pk_time(Varchar) 当天的数据 SELECT * FROM 表 WHERE date(fk_time) = curdate(); 当月的数据 SELECT *FROM 表 ...

  2. android 弹起键盘把ui顶上去的解决办法

    键盘输入框上面的ui布局必须为Relative相对布局.然后设置 <activityandroid:name=".activity.HomeActivity"Android: ...

  3. 【ASP.NET Core】MVC中自定义视图的查找位置

    .NET Core 的内容处处可见,刷爆全球各大社区,所以,老周相信各位大伙伴已经看得不少了,故而,老周不考虑一个个知识点地去写,那样会成为年度最大的屁话,何况官方文档也很详尽.老周主要扯一下大伙伴们 ...

  4. PHP定义字符串的四种方式

    1.简介 在PHP中这门语言中,因为是弱类型语言,因此使用变量时不需提前定义即可使用. 我们在使用php进行开发的时候,大多数使用双引号.单引号进行定义字符串.既然有这两种方式,那么他们之间肯定是有区 ...

  5. iOS-主线程刷新UI【+单例】

    主线程刷新UI dispatch_async(dispatch_get_main_queue(), ^{ /// }); 单例 static Tools *_sharedManger; @implem ...

  6. java线程池原理及实现方式

    线程池的定义 线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务.线程池线程都是后台线程 为什么要使用线程池 1.减少在创建和销毁线程上所花的时间以及系统资源的开 ...

  7. BZOJ 2916: [Poi1997]Monochromatic Triangles [计数]

    题意:空间中有n个点,任意3个点不共线.每两个点用红线或者蓝线连接,如果一个三角形的三边颜色相同,那么称为同色三角形.给你一组数据,计算同色三角形的总数. 考虑补集,异色三角形 每个点的边红色和蓝色两 ...

  8. HDU D Tree [点分治]

    传送门 题意:找路径积$\mod 1e6+3 = k$的字典序最小点对 作为一个点分治蒟蒻,写这道题花了两节课.... 显然只要开一个桶$c[i]$记录当前路径积为$i$的最小点 然后处理一个子树时一 ...

  9. 在ASP.NET MVC中使用Web API和EntityFramework构建应用程序

    最近做了一个项目技术预研:在ASP.NET MVC框架中使用Web API和EntityFramework,构建一个基础的架构,并在此基础上实现基本的CRUD应用. 以下是详细的步骤. 第一步 在数据 ...

  10. react native 常用组件汇总

    react-native-uploader //文件上传https://github.com/aroth/react-native-uploader jpush-react-native //官方版本 ...