MongoDB分片笔记2

前言

本文将通过3台虚拟机来搭建一个分片集群,最终搭建完成后的样子如下:

192.168.1.14

shard1 master:27017

shard2 arbiter:27018

shard3 secondary:27019

mongos:30000

shard config server:30001

192.168.1.10

shard1 secondary:27017

shard2 master:27018

shard3 arbiter:27019

mongos:30000(非必要)

shard config server:30001(非必要)

192.168.1.13

shard1 arbiter:27017

shard2 secondary:27018

shard3 master:27019

mongos:30000(非必要)

shard config server:30001(非必要)

副本集

shard1_rs

shard2_rs

shard3_rs

分片集群必须的三个主要组件,配置分片集群实际就是配置并运行这三个组件

  • Shard
  • Config Server
  • mongos

步骤

第一步:下载安装包

前往mongodb官网下载mongodb安装包 地址MongoDB Community Download | MongoDB

在centos根目录新建mongodb509文件夹,解压下载的安装包

第二步:新建文件夹

在三台虚拟机上的/mongodb509目录下分别建立如下内容,在后面搭建时配置文件中需要用到这些文件和文件夹:

mongodb

|--data //文件夹

| |--mongos //文件夹

| |--shard1 //文件夹

| |--shard2 //文件夹

| |--shard3 //文件夹

| |--shard_config_server //文件夹

|-- log //文件夹

|-- pid //文件夹

| --shard1.conf //配置文件

| --shard2.conf //配置文件

| --shard3.conf //配置文件

| --shard_config_server.conf //配置文件

第三步:创建Config Server副本集【Mongodb3.4以后Config Server必须是副本集】

1.将下面内容复制并覆盖到三台虚拟机上的shard_config_server.conf中:

# mongod.conf
 
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /mongodb509/mongodb/log/shard_config_server.log #第一步创建的log文件夹
 
# Where and how to store data.
storage:
  dbPath: /mongodb509/mongodb/data/shard_config_server #第一步创建的文件夹
  journal:
    enabled: true
#  engine:
#  wiredTiger:
 
# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /mongodb509/mongodb/pid/shard_config_server.pid #第一步创建的文件夹
  timeZoneInfo: /usr/share/zoneinfo #默认,无需修改
 
# network interfaces
net:
  port: 30001
  bindIp: 0.0.0.0
 
#security:
 
#operationProfiling:
 
replication:
  replSetName: shard_config_server_rs  #因为必须搭建副本集,所以这里要设置
 
sharding:
  clusterRole: configsvr #因为要配置分片,所以这里也要设置,固定写法
 
## Enterprise-Only Options
 
#auditLog:
 
#snmp:

2.在三台虚拟机上分别运行下面命令,启动Server Config

# 路径是第一步创建的文件路径 ./mongod -f /mongodb509/mongodb/shard_config_server.conf

3.通过初始化命令,将三个虚拟机上的Server Config组成副本集

# 第一步:先通过mongosh连接上任意一个刚才启动的Config Server
./mongo --port 30001 #一定要指定端口,否则默认用27017端口
 
# 第二步:初始化,组成副本集,别忘了先关闭防火墙
rs.initiate( {
   _id : "shard_config_server_rs",
   members: [
      { _id: 0, host: "192.168.1.14:30001" },
      { _id: 1, host: "192.168.1.10:30001" },
      { _id: 2, host: "192.168.1.13:30001" }
   ]
})

这一步在其中一个虚拟机上执行一次就行,不要三个虚拟机都执行。切记! 

第四步:创建shard

所谓创建shard,就是创建mongod实例,然后用来保存分片后的某一部分数据。

1.分别在三个虚拟机上复制如下内容到配置文件shard1.conf

# mongod.conf
 
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /mongodb509/mongodb/log/shard1.log
 
# Where and how to store data.
storage:
  dbPath: /mongodb509/mongodb/data/shard1
  journal:
    enabled: true
#  engine:
#  wiredTiger:
 
# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /mongodb509/mongodb/pid/shard1.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo
 
# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0   # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
 
#security:
 
#operationProfiling:
 
replication:
  replSetName: shard1_rs  # 因为也要通过三个虚拟机组成副本集,所以需要设置副本集名称
 
sharding:
  clusterRole: shardsvr  # 因为是保存分片数据,所以要设置这个属性
 
## Enterprise-Only Options
 
#auditLog:
 
#snmp:

2.分别在三个虚拟机上复制如下内容到配置文件shard2.conf

# mongod.conf
 
# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/
 
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /mongodb509/mongodb/log/shard2.log
 
# Where and how to store data.
storage:
  dbPath: /mongodb509/mongodb/data/shard2
  journal:
    enabled: true
#  engine:
#  wiredTiger:
 
# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /mongodb509/mongodb/pid/shard2.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo
 
# network interfaces
net:
  port: 27018
  bindIp: 0.0.0.0   # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
 
#security:
 
#operationProfiling:
 
replication:
  replSetName: shard2_rs
 
sharding:
  clusterRole: shardsvr
 
## Enterprise-Only Options
 
#auditLog:
 
#snmp:

3.分别在三个虚拟机上复制如下内容到配置文件shard3.conf

# mongod.conf
 
# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/
 
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /mongodb509/mongodb/log/shard3.log
 
# Where and how to store data.
storage:
  dbPath: /mongodb509/mongodb/data/shard3
  journal:
    enabled: true
#  engine:
#  wiredTiger:
 
# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /mongodb509/mongodb/pid/shard3.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo
 
# network interfaces
net:
  port: 27019
  bindIp: 0.0.0.0   # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
 
#security:
 
#operationProfiling:
 
replication:
  replSetName: shard3_rs
 
sharding:
  clusterRole: shardsvr
 
## Enterprise-Only Options
 
#auditLog:
 
#snmp:

4.将如下3个命令在每个虚拟机上都执行一次,目的是在每个虚拟机上都创建shard1,shard2,shard3三个shard

# 第一步创建的配置文件的路径
./mongod -f /mongodb509/mongodb/shard1.conf
./mongod -f /mongodb509/mongodb/shard2.conf
./mongod -f /mongodb509/mongodb/shard3.conf

这一步执行完后,三个虚拟机会同时存在shard1,shard2,shard3(看开头的表格),然后下面会将3个虚拟机上的shard1组成一个副本集,shard2组成一个副本集,shard3组成一个副本集。

5.在三个虚拟机中随便找一个,然后分别执行下面3段命令,将3个shard1组成副本集,3个shard2组成副本集,3个shard3组成副本集

 通过初始化,将三个shard1的分片组成副本集,命令如下:

#第一步:通过mongosh连接上mongod
./mongo --port 27017
 
#第二步:初始化,组成副本集
rs.initiate( {
   _id : "shard1_rs",
   members: [
      { _id: 0, host: "192.168.1.14:27017" },
      { _id: 1, host: "192.168.1.10:27017" },
      { _id: 2, host: "192.168.1.13:27017" }
   ]})

将三个shard2的分片组成副本集

#第一步:通过mongosh连接上mongod
./mongo --port 27018
 
#第二步:初始化,组成副本集
rs.initiate( {
   _id : "shard2_rs",
   members: [
      { _id: 0, host: "192.168.1.14:27018" },
      { _id: 1, host: "192.168.1.10:27018" },
      { _id: 2, host: "192.168.1.13:27018" }
   ]})

将三个shard3的分片通过初始化组成副本集

#第一步:通过mongosh连接上mongod
./mongo --port 27019
 
#第二步:初始化,组成副本集
rs.initiate( {
   _id : "shard3_rs",
   members: [
      { _id: 0, host: "192.168.1.14:27019" },
      { _id: 1, host: "192.168.1.10:27019" },
      { _id: 2, host: "192.168.1.13:27019" }
   ]})

第五步:创建mongos

mongos作为一个路由的角色,我们要操作上面创建的shard1,shard2,shard3分片集群中的数据时,都要通过mongos为入口来进行操作。

mongos既可以启动一个,也可以跟上面Config Server或者shard一样创建副本集的形式

1.分别在三个虚拟机上复制如下内容到配置文件mongos.conf

# mongod.conf
 
# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/
 
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /mongodb509/mongodb/log/mongos.log
 
# Where and how to store data.
#  engine:
#  wiredTiger:
 
# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /mongodb509/mongodb/pid/mongos.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo
 
# network interfaces
net:
  port: 30000
  bindIp: 0.0.0.0
  
#开启用户认证证书保存
#security:
    
#operationProfiling:
 
#replication:
#  replSetName: "rs0"
#这个属性就是用来设置路由的,不能忽略
sharding:
  configDB: shard_config_server_rs/192.168.1.14:30001,192.168.1.10:30001,192.168.1.13:30001
#shard_config_server_rs就是我们Config Server副本集的名字,就是shard_config_server.conf中replSetName的值
## Enterprise-Only Options
 
#auditLog:
 
#snmp:

注意:

因为mongos主要用来作为路由使用,所以不需要配置dbpath,只需要配置log的path就行了,所以我把storage属性都删除了。否则会报错误【Unrecognized option: storage.dbPath】。另外因为他的作用是路由,所以配置中还需要设定configDB这个属性来指定Config Server副本集的地址和端口

2.启动mongos

./mongos -f /mongodb509/mongodb/mongos.conf

3.添加shard到cluster中,分别执行下面三条指令:

上边通过mongosh连接到mongos后,执行下面3条指令,将之前创建的shard1,shard2,shard3的副本集机器的地址和端口配置到mongos中

#shard1_rs,shard2_rs,shard3_rs就是我们前面配置文件中配置的shard副本集中的replSetName的值
sh.addShard( "shard1_rs/192.168.1.14:27017,192.168.1.10:27017,192.168.1.13:27017")
sh.addShard( "shard2_rs/192.168.1.14:27018,192.168.1.10:27018,192.168.1.13:27018")
sh.addShard( "shard3_rs/192.168.1.14:27019,192.168.1.10:27019,192.168.1.13:27019")

 =========================================================

到此为止,配置就完成了,下面开始使用分片=========================================================

第六步:使用分片集群

1.开启分片功能(想让哪个数据库有分片的功能,就开启那个数据库)

use admin #在admin库中进行操作 sh.enableSharding("dbname") #dbname就是要开启分片的数据库名字

2.指定哪些数据库中的表可以进行分片

sh.shardCollection("dbname.tb1", { userId: 1} ) # tb1是表,userId是表中的一个字段,用这个字段来作为分片索引,如果此时tb1中还没记录,那么这里会自动将userId作为索引,如果表中已经有数据,需要先手动去把userId这个字段设置为索引

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值