参考地址:https://blog.csdn.net/weixin_43622131/article/details/105984032

已配置好的所有的配置文件下载地址:https://files.cnblogs.com/files/sanduzxcvbnm/conf.zip

MongoDB安装

配置MongoDB的yum源

cd /etc/yum.repos.d
vim mongodb-org-4.4.repo

添加以下内容,这里使用阿里的源,baseurl具体根据使用的操作系统来定

[mngodb-org]
name=MongoDB Repository
baseurl=http://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/4.4/x86_64/
gpgcheck=0
enabled=1

阿里云mongodb源镜像描述













安装MongoDB

yum -y install mongodb-org

安装的软件有如下这些:

正在安装:
mongodb-org
为依赖而安装:
cyrus-sasl
cyrus-sasl-gssapi
cyrus-sasl-plain
mongodb-database-tools
mongodb-org-database-tools-extra
mongodb-org-mongos
mongodb-org-server
mongodb-org-shell
mongodb-org-tools

安装完后可以查看一下MongoDB的位置

# whereis mongod
mongod: /usr/bin/mongod /etc/mongod.conf /usr/share/man/man1/mongod.1 # whereis mongos
mongos: /usr/bin/mongos /usr/share/man/man1/mongos.1

集群的设计

由于资源有限,拿一台虚拟机做了一个伪分布式的集群,但是原理都是一样的,此方法完全可以完全分布式的集群上。

首先集群有六台机器,一共分了六个副本集,S1,S2,S3,S4,S5,S6,在这里一个副本集分了三个节点,同一副本集中的节点有一个主节点,两个从节点,从节点的数据与主节点一致;

OS为mongos进程(路由服务器);C为config server(配置服务器),设置了三个配置服务器。

按图中所示为每个进程分配一个端口,如果想实现完全分布式可以将这些进程按图分配到六台机器上,这是完全可行的。

这个图极其重要,首先要把这个图看懂,看明白是怎么设计的。

设置配置文件

创建目录结构

首先新建一个放集群数据的文件夹,我这里是在/opt目录下新建了一个mongo文件夹,下面的操作都在/opt/mongo文件夹下进行操作。

然后要为每个进程新建一个文件夹,为了模拟六台机器,我在目录中新建了六个文件夹。1号文件夹代表1号机器,2号文件夹代表2号机器。以此类推

在每个文件夹中为每个进程新建一个文件夹,比如1号机器有4个进程,则新建4个文件夹。

最终的目录结构如下所示:

/opt # tree mongo
mongo
├── 1
│   ├── 1
│   ├── 2
│   ├── 3
│   └── 4
├── 2
│   ├── 1
│   ├── 2
│   ├── 3
│   └── 4
├── 3
│   ├── 1
│   ├── 2
│   ├── 3
│   └── 4
├── 4
│   ├── 1
│   ├── 2
│   ├── 3
│   └── 4
├── 5
│   ├── 1
│   ├── 2
│   └── 3
└── 6
├── 1
├── 2
└── 3

修改配置文件

配置文件一共有三种,一个是路由服务器的配置文件即图中的OS进程的配置文件,一个是副本集中节点的配置文件,一个是配置服务器即图中C中节点的配置文件。

首先需要先找一个模板,在/opt/mongo目录下新建一个conf目录,将MongoDB本身带的conf文件拷贝到该目录下,然后重命名为11.conf,说明是1号机器的第一个进程。MongoDB自带配置文件的位置可以通过whereis mongod命令找到:/etc/mongod.conf

最终的目录结构如下:

/opt/mongo # tree conf
conf
├── 11.conf
├── 12.conf
├── 13.conf
├── 14.conf
├── 21.conf
├── 22.conf
├── 23.conf
├── 24.conf
├── 31.conf
├── 32.conf
├── 33.conf
├── 34.conf
├── 41.conf
├── 42.conf
├── 43.conf
├── 44.conf
├── 51.conf
├── 52.conf
├── 53.conf
├── 61.conf
├── 62.conf
├── 63.conf

副本集中节点的配置

首先修改的是副本集中节点的配置文件。

这一块的path要修改成刚刚创建给该进程的目录,1号机器的第一个进程所以是/opt/mongo/1/1/mongod.log

同样这里也要改dbPath

这里要改pidFilePath,同样的道理

然后是端口号,刚刚图中给1号机器的第一个进程分配的端口是9011,同时要把bindIp改为0.0.0.0

这一块原本是注释掉的,需要取消注释,并增加replSetName,后面跟的是自己的副本集的编号,与设计集群的图所对应,一定要注意replSetName前面要有缩进,冒号后面要有空格,否则会报错

这个刚开始也是被注释掉的,同样要增加clusterRole,同时也要注意缩进和空格,副本集中节点的作用为shardsvr

12.conf表示的是第一台机器的第二个进程,参考上面的样式进行相应的修改,尤其要注意的是replSetName要参考集群设计图,是S5,不是上一步的S1,其他的也都类似操作

按照集群设计图,需要这样做的有,11,12,13,21,22,23,31,32,33,41,42,43,51,52,53,61,62,63共15个配置文件,注意replSetName参考集群设计图上的

配置服务器节点的配置

由于刚刚已经配置好了11.conf,所有节点的配置文件都大致相同,所以直接将该文件复制多份,其余文件只需要略微修改即可。

下面修改配置服务器节点的配置,按照设计图应该是24,34,44共三个配置文件

以24为例。

只有这两处不同,replSetName要设置为自己所在的组名C

作用修改位configsvr,其余的只需要将路径改为为其创建的文件夹的路径,端口改为为其分配的端口号即可

路由服务器的配置

路由服务器只有一个,设计图中编号为14。

首先需要把storage这块注释掉,因为路由服务器不需要进行存储。

同样replication模块也要注释掉

sharding模块的内容需要修改

sharding:
configDB: C/192.168.0.253:9024,192.168.0.253:9034,192.168.0.253:9044

其中C为配置服务器的组名,后面跟上配置服务器进程的地址:端口即可,在这里配置的是三台配置服务器的地址和端口

已配置好的所有的配置文件下载地址:https://files.cnblogs.com/files/sanduzxcvbnm/conf.zip

启动mongod

逐个启动很麻烦,可以在/opt/mongo/conf目录下写一个脚本如图:

#!/bin/bash

mongod -f 11.conf
mongod -f 12.conf
mongod -f 13.conf
mongod -f 21.conf
mongod -f 22.conf
mongod -f 23.conf
mongod -f 24.conf
mongod -f 31.conf
mongod -f 32.conf
mongod -f 33.conf
mongod -f 34.conf
mongod -f 41.conf
mongod -f 42.conf
mongod -f 43.conf
mongod -f 44.conf
mongod -f 51.conf
mongod -f 52.conf
mongod -f 53.conf
mongod -f 61.conf
mongod -f 62.conf
mongod -f 63.conf

注意:没有14.conf文件,下面的操作会用到这个配置文件

启动成功后查看一下进程是否都成功启动,如果有未启动的可以看下配置文件是否有误,或者查看一下日志。

注意:若配置文件有错误需要批量杀死这些进程,然后再次启动:ps -ef|grep "mongod -f "|grep -v grep|cut -c 9-15|xargs kill -9

分组

然后要对节点进行分组,按照之前设计图上分组即可。首先进入该组中任意节点,声明其组内成员。

节点和端口严格按照集群设计图上的来配置

mongo --port 9011
cfg={_id:"S1", members:[{_id:0,host:'192.168.0.253:9011'}, {_id:1,host:'192.168.0.253:9021'}, {_id:2,host:'192.168.0.253:9031'}]};
rs.initiate(cfg) mongo --port 9022
cfg={_id:"S2", members:[{_id:0,host:'192.168.0.253:9022'}, {_id:1,host:'192.168.0.253:9032'}, {_id:2,host:'192.168.0.253:9041'}]};
rs.initiate(cfg) mongo --port 9033
cfg={_id:"S3", members:[{_id:0,host:'192.168.0.253:9033'}, {_id:1,host:'192.168.0.253:9042'}, {_id:2,host:'192.168.0.253:9051'}]};
rs.initiate(cfg) mongo --port 9043
cfg={_id:"S4", members:[{_id:0,host:'192.168.0.253:9043'}, {_id:1,host:'192.168.0.253:9052'}, {_id:2,host:'192.168.0.253:9061'}]};
rs.initiate(cfg) mongo --port 9053
cfg={_id:"S5", members:[{_id:0,host:'192.168.0.253:9053'}, {_id:1,host:'192.168.0.253:9062'}, {_id:2,host:'192.168.0.253:9012'}]};
rs.initiate(cfg) mongo --port 9063
cfg={_id:"S6", members:[{_id:0,host:'192.168.0.253:9063'}, {_id:1,host:'192.168.0.253:9013'}, {_id:2,host:'192.168.0.253:9023'}]};
rs.initiate(cfg) mongo --port 9024
cfg={_id:"C", members:[{_id:0,host:'192.168.0.253:9024'}, {_id:1,host:'192.168.0.253:9034'}, {_id:2,host:'192.168.0.253:9044'}]};
rs.initiate(cfg)

启动并连接mongos

mongos -f 14.conf

增加分片

下面均在mongos的命令行中进行操作

sh.addShard("S1/192.168.0.253:9011")
sh.addShard("S2/192.168.0.253:9022")
sh.addShard("S3/192.168.0.253:9033")
sh.addShard("S4/192.168.0.253:9043")
sh.addShard("S5/192.168.0.253:9053")
sh.addShard("S6/192.168.0.253:9063")

启动数据分片功能

use admin
db.runCommand({enableSharding:"stumis"})
db.runCommand({shardCollection:"stumis.students",key:{"_id":"hashed"}})

批量插入数据

use stumis
for(var i=1; i<=100; i++) { db.students.insert( {name:"S"+i,age:i});}

查看分片效果

sh.status()
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5fa91462ee38d319820f96ad")
}
shards:
{ "_id" : "S1", "host" : "S1/192.168.0.253:9011,192.168.0.253:9021,192.168.0.253:9031", "state" : 1 }
{ "_id" : "S2", "host" : "S2/192.168.0.253:9022,192.168.0.253:9032,192.168.0.253:9041", "state" : 1 }
{ "_id" : "S3", "host" : "S3/192.168.0.253:9033,192.168.0.253:9042,192.168.0.253:9051", "state" : 1 }
{ "_id" : "S4", "host" : "S4/192.168.0.253:9043,192.168.0.253:9052,192.168.0.253:9061", "state" : 1 }
{ "_id" : "S5", "host" : "S5/192.168.0.253:9012,192.168.0.253:9053,192.168.0.253:9062", "state" : 1 }
{ "_id" : "S6", "host" : "S6/192.168.0.253:9013,192.168.0.253:9023,192.168.0.253:9063", "state" : 1 }
active mongoses:
"4.4.0" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: yes
Collections with active migrations:
config.system.sessions started at Tue Nov 10 2020 10:12:52 GMT+0800 (CST)
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
68 : Success
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
S1 956
S2 14
S3 13
S4 14
S5 14
S6 13
too many chunks to print, use verbose if you want to force print
{ "_id" : "stumis", "primary" : "S3", "partitioned" : true, "version" : { "uuid" : UUID("3b546bcb-26dc-47f2-97b0-d66f55790186"), "lastMod" : 1 } }
stumis.students
shard key: { "_id" : "hashed" }
unique: false
balancing: true
chunks:
S1 2
S2 2
S3 2
S4 2
S5 2
S6 2
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : NumberLong("-7686143364045646500") } on : S1 Timestamp(1, 0)
{ "_id" : NumberLong("-7686143364045646500") } -->> { "_id" : NumberLong("-6148914691236517200") } on : S1 Timestamp(1, 1)
{ "_id" : NumberLong("-6148914691236517200") } -->> { "_id" : NumberLong("-4611686018427387900") } on : S2 Timestamp(1, 2)
{ "_id" : NumberLong("-4611686018427387900") } -->> { "_id" : NumberLong("-3074457345618258600") } on : S2 Timestamp(1, 3)
{ "_id" : NumberLong("-3074457345618258600") } -->> { "_id" : NumberLong("-1537228672809129300") } on : S3 Timestamp(1, 4)
{ "_id" : NumberLong("-1537228672809129300") } -->> { "_id" : NumberLong(0) } on : S3 Timestamp(1, 5)
{ "_id" : NumberLong(0) } -->> { "_id" : NumberLong("1537228672809129300") } on : S4 Timestamp(1, 6)
{ "_id" : NumberLong("1537228672809129300") } -->> { "_id" : NumberLong("3074457345618258600") } on : S4 Timestamp(1, 7)
{ "_id" : NumberLong("3074457345618258600") } -->> { "_id" : NumberLong("4611686018427387900") } on : S5 Timestamp(1, 8)
{ "_id" : NumberLong("4611686018427387900") } -->> { "_id" : NumberLong("6148914691236517200") } on : S5 Timestamp(1, 9)
{ "_id" : NumberLong("6148914691236517200") } -->> { "_id" : NumberLong("7686143364045646500") } on : S6 Timestamp(1, 10)
{ "_id" : NumberLong("7686143364045646500") } -->> { "_id" : { "$maxKey" : 1 } } on : S6 Timestamp(1, 11)
mongos>

其他客户端连接

只需要连接提供路由的那台主机地址和端口就行了,本文中指的是:192.168.0.253:9014

最新文章

  1. window常见事件
  2. android 常见分辨率(mdpi、hdpi 、xhdpi、xxhdpi )屏幕适配
  3. C#知识点总结【1】
  4. 简单风格 在线音乐播放器(支持wav,MP3等)
  5. jQuery图片轮播的具体实现
  6. Java多线程核心技术(五)单例模式与多线程
  7. Linux基本命令总结(一)
  8. Python3编写网络爬虫09-数据存储方式二-JSON文件存储
  9. OpenJuege 兔子与星空
  10. centos中,tomcat项目创建文件的权限
  11. 使用Newtonsoft进行JSON序列化时将枚举序列化为字符串的方法
  12. go jwt OAuth2.0
  13. 为什么java代码中要避免多层深度for循环嵌套
  14. CODEVS 2171 棋盘覆盖
  15. GISer面对创业的困惑
  16. CentOS 6.5 x64下Hadoop安装
  17. spring boot快速入门 5: 事务管理
  18. 提高SQL Server数据库效率常用方法
  19. B/S和C/S架构简单理解
  20. 在Android源码中如何吧so库打包编译进入apk, 集成第三方库(jar和so库)

热门文章

  1. Idea 的Test测试报错:java.lang.IllegalStateException: Failed to load ApplicationContext
  2. 我的sql没问题为什么还是这么慢|MySQL加锁规则
  3. c:找到出现次数最多的递增数字串
  4. CF1705A Mark the Photographer 题解
  5. 小A的柱状图_via牛客网
  6. impl和dyn用法
  7. SpringCloud gateway自定义请求的 httpClient
  8. nodejs学习总结02
  9. iOS自动化打包 Jenkins+Gitlab+Fastlane+蒲公英+钉钉
  10. 使用python3.7和opencv4.1来实现人脸识别和人脸特征比对以及模型训练