MongoDB 4.2 副本集群及分片集群搭建

一、环境准备

系统版本:Centos 7
软件版本:MongoDB 4.2
关闭防火墙及selinux
#systemctl stop firewalld.service
#setenforce 0

二、角色规划

172.22.12.10 172.22.12.11 172.22.12.12
mongos(27020) mongos(27020) mongos(27020)
Config(27019) Config(27019) Config(27019)
Shard1主节点(27016) Shard1副节点(27016) Shard1仲裁节点(27016)
Shard2仲裁节点(27017) Shard2主节点(27017) Shard2副节点(27017)
Shard3副节点(27018) Shard3仲裁节点(27018) Shard3主节点(27018)

三、MongoDB4.2的安装

(一)添加安装源

#vi /etc/yum.repos.d/mongodb-org-4.2.repo

添加如下内容:

[mongodb-org-4.2] name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1 enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

Esc+wq!保存
(二)安装MongoDB 4.2

#sudo yum install -y mongodb-org-4.2.0 mongodb-org-server-4.2.0 mongodb-org-shell-4.2.0 mongodb-org-mongos-4.2.0 mongodb-org-tools-4.2.0

验证安装版本

#mongo --version

查看安装文件相应目录

#whereis mongo
mongo: /usr/bin/mongo /usr/share/man/man1/mongo.1

关闭系统自启动服务 mongod.service //切记一定要关,不然以后会有大麻烦
(三)创建文件目录
日志目录:

#mkdir -p /senscom/mongodb/logs
#touch /senscom/mongodb/logs/mongod{000,001,002}.log
#touch /senscom/mongodb/logs/csvr.log

存储目录:

#mkdir -p /senscom/mongodb/data/{000,001,002,csvr}

创建配置文件目录:

#mkdir -p /senscom/mongodb/config

配置目录权限:

# chmod 777 /senscom/mongodb/logs/*.log
# chown -R mongod.mongod /senscom/mongodb
# echo "PATH=/usr/bin/mongo/bin:$PATH" >> /etc/profile
# source /etc/profile

四、Config服务器配置

分别登陆三台服务器操作

#vi /senscom/mongodb/config/csvr.yaml
systemLog:
  destination: file
  path: "/senscom/mongodb/logs/csvr.log"
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: "/senscom/mongodb/data/csvr"
  directoryPerDB: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true
net:
  bindIp: 0.0.0.0
  port: 27019
replication:
  oplogSizeMB: 2048
  replSetName: csvr
sharding:
  clusterRole: configsvr
processManagement:
  fork: true

Esc+wq!保存
在主机上执行该文件

#mongod -f /senscom/mongodb/config/csvr.yaml

看一下跑起来没有

#netstat -tulnp
#ps aux|grep mongo

五、副本集群服务配置

(一)创建配置文件
分别登陆三台服务器

#vi /senscom/mongodb/config/mongod000.yaml
systemLog:
  destination: file
  path: "/senscom/mongodb/logs/mongod000.log"
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: "/senscom/mongodb/data/000"
processManagement:
  fork: true
net:
  bindIp: 0.0.0.0
  port: 27016
setParameter:
  enableLocalhostAuthBypass: false
replication:
  replSetName: "rs000"
sharding:
  clusterRole: shardsvr

Esc+wq!保存

#vi /senscom/mongodb/config/mongod001.yaml
systemLog:
  destination: file
  path: "/senscom/mongodb/logs/mongod001.log"
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: "/senscom/mongodb/data/001"
processManagement:
  fork: true
net:
  bindIp: 0.0.0.0
  port: 27017
setParameter:
  enableLocalhostAuthBypass: false
replication:
  replSetName: "rs001"
sharding:
  clusterRole: shardsvr

Esc+wq!保存

#vi /senscom/mongodb/config/mongod002.yaml
systemLog:
  destination: file
  path: "/senscom/mongodb/logs/mongod002.log"
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: "/senscom/mongodb/data/002"
processManagement:
  fork: true
net:
  bindIp: 0.0.0.0
  port: 27018
setParameter:
  enableLocalhostAuthBypass: false
replication:
  replSetName: "rs002"
sharding:
  clusterRole: shardsvr

Esc+wq!保存
(二)初始化副本集
启动副本集,在每台机器上都执行

#mongod -f /senscom/mongodb/config/mongod000.yaml
#mongod -f /senscom/mongodb/config/mongod001.yaml
#mongod -f /senscom/mongodb/config/mongod002.yaml

任意一台机器执行,进去到mongo shell

#mongo -prot 27016

在mongo shell中执行

rs.initiate( { _id : “rs000”, members: [
{ _id: 0, host: “172.22.12.10:27016” },
{ _id: 1, host: “172.22.12.11:27016” },
{ _id: 2, host: “172.22.12.12:27016” } ]})

任意一台机器执行,进去到mongo shell

#mongo -prot 27017

在mongo shell中执行

rs.initiate( { _id : “rs001”, members: [
{ _id: 0, host: “172.22.12.10:27017” },
{ _id: 1, host: “172.22.12.11:27017” },
{ _id: 2, host: “172.22.12.12:27017” } ]})

任意一台机器执行,进去到mongo shell

#mongo -prot 27018

在mongo shell中执行

rs.initiate( { _id : “rs002”, members: [
{ _id: 0, host: “172.22.12.10:27018” },
{ _id: 1, host: “172.22.12.11:27018” },
{ _id: 2, host: “172.22.12.12:27018” } ]})

六、路由服务器配置

分别登陆三台服务器

#vi /senscom/mongodb/config/mongos.yaml
systemLog:
  destination: file
  path: "/senscom/mongodb/logs/mongos000.log"
  logAppend: true
net:
  bindIp: 0.0.0.0
  port: 27020
sharding:
configDB: csvr/172.22.12.10:27019,172.22.12.11:27019,172.22.12.12:27019
processManagement:
  fork: true

Esc+wq!保存
在主机上执行该文件

#mongos -f /senscom/mongodb/config/mongos.yaml 

看一下跑起来没有

#netstat -tulnp
#ps aux|grep mongo

七、分片集群服务配置

登录到mongos
任意一台主机执行都可以,当前在172.22.12.10上执行

#mongo --host 172.22.12.10 --port 27020
> use admin 
> db.runCommand( { addshard :"rs000/172.22.12.10:27016,172.22.12.11:27016,172.22.12.12:27016",name:"shard1"} ) 
> db.runCommand( { addshard :"rs001/172.22.12.10:27017,172.22.12.11:27017,172.22.12.12:27017",name:"shard2"} )

此处先添加两各分片服务器,还有一个,待会添加
查看群集状态

mongos> sh.status()

添加新的分片

>db.runCommand( { addshard : "rs002/172.22.12.10:27018,172.22.12.11:27018,172.22.12.12:27018",name:"shard3"} )

查看群集状态

mongos> sh.status()

移除分片

db.runCommand( { removeshard: "your_shard_name" } ) 

八、功能测试

在mongo shell中,这里设置一个测试数据库,用来测试功能.

>db.runCommand( { enablesharding : "testdb" } )
>db.runCommand( { shardcollection : "testdb.users",key : {id: 1} } )

创建测试数据,用来测试分片功能,下面的代码向数据库中插入了200w条数据,由于数据量比较大,所以批量插入的时候,时间会稍微有点久.

>var arr=[];for(var i=0;i<2000000;i++){var uid = i;var name = "mongodb"+i;arr.push({"id":uid,"name":name});}
>db.users.insertMany(arr);

查询状态
通过sh.status()函数查看当前分片的状态
查看副本集的状态通过rs.status()

你可能感兴趣的:(MongoDB)