avatar

目录
MongoDB分片式复制集的配置安装

复制集安装准备

  • 确认本机已安装MongoDB,并且设置好MongoDB环境变量

  • 若没安装,请自行询问度娘:MongoDB安装

创建相应的文件夹

本次配置包含以下内容:

  • 包含三个分片集,每个分片集由一个主节点、一个从节点和一个仲裁节点组成

  • 配置集(config servers)包含三个节点

  • 路由集(mongos)包含两个节点

创建的文件夹为:

  • 主目录:
  • 分片集:(主节点,从节点,仲裁节点各一个文件夹)

每一个节点的配置为: data文件夹 config文件 logs.log文件

  • 配置集:

每一个节点的配置为: data文件夹 config文件 logs.log文件

  • 路由集

每一个节点的配置为: config文件 logs.log文件

配置项:

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
分片集配置项及其含义:

dbpath=数据的存放目录

logpath=日志文件的路径

port=mongod服务的端口

shardsvr=是否为分片集节点(true为是)

replSet=节点集唯一标识

logappend=是否以追加的形式书写日志

oplogSize=操作日志的大小

storageEngine=存储引擎

具体配置如下

l \书写各节点的配置文件**

(下文中配置为绝对路径,也可采用相对路径。如不理解,采用绝对路径)

  • 分片集wutb_sharda、节点wutb_sharda_master配置项和截图:

配置项:

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dbpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_sharda\wutb_sharda_master\data

logpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_sharda\wutb_sharda_master\logs.log

port=11624

shardsvr=true

replSet=wutb_sharda

logappend=true

oplogSize=2048

storageEngine=wiredTiger

截图:

  • 分片集wutb_sharda、节点wutb_sharda_slave配置项和截图:

配置项:

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dbpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_sharda\wutb_sharda_slave\data

logpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_sharda\wutb_sharda_slave\logs.log

port=11625

shardsvr=true

replSet=wutb_sharda

logappend=true

oplogSize=2048

storageEngine=wiredTiger

截图:

  • 分片集wutb_sharda、节点wutb_sharda_arbiter配置项和截图:

配置项:

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dbpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_sharda\wutb_sharda_arbiter\data

logpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_sharda\wutb_sharda_arbiter\logs.log

port=11623

shardsvr=true

replSet=wutb_sharda

logappend=true

oplogSize=2048

storageEngine=wiredTiger

截图:

  • 分片集wutb_shardb、节点wutb_sharda_slave配置项和截图:

配置项:

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dbpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_shardb\wutb_shardb_slave\data

logpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_shardb\wutb_shardb_slave\logs.log

port=21625

shardsvr=true

replSet=wutb_shardb

logappend=true

oplogSize=2048

storageEngine=wiredTiger

截图:

  • 分片集wutb_shardb、节点wutb_shardb_master配置项和截图:

配置项:

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dbpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_shardb\wutb_shardb_master\data

logpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_shardb\wutb_shardb_master\logs.log

port=21624

shardsvr=true

replSet=wutb_shardb

logappend=true

oplogSize=2048

storageEngine=wiredTiger

截图:

  • 分片集wutb_shardb、节点wutb_shardb_arbiter配置项和截图:

配置项:

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dbpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_shardb\wutb_shardb_arbiter\data

logpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_shardb\wutb_shardb_arbiter\logs.log

port=21623

shardsvr=true

replSet=wutb_shardb

logappend=true

oplogSize=2048

storageEngine=wiredTiger

截图:

  • 分片集wutb_shardc、节点wutb_shardc_arbiter配置项和截图:

配置项:

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dbpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_shardc\wutb_shardc_arbiter\data

logpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_shardc\wutb_shardc_arbiter\logs.log

port=31623

shardsvr=true

replSet=wutb_shardc

logappend=true

oplogSize=2048

storageEngine=wiredTiger

截图:

  • 分片集wutb_shardc、节点wutb_shardc_master配置项和截图:

配置项:

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dbpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_shardc\wutb_shardc_master\data

logpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_shardc\wutb_shardc_master\logs.log

port=31624

shardsvr=true

replSet=wutb_shardc

logappend=true

oplogSize=2048

storageEngine=wiredTiger

截图:

  • 分片集wutb_shardc、节点wutb_shardc_slave配置项和截图:

配置项:

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dbpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_shardc\wutb_shardc_slave\data

logpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_shardc\wutb_shardc_slave\logs.log

port=31625

shardsvr=true

replSet=wutb_shardc

logappend=true

oplogSize=2048

storageEngine=wiredTiger

截图:

MongoDB分片式复制集的配置安装\wps9.jpg)

  • 配置集wutb_configs、节点wutb_configs_a配置项和截图:

配置项:

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dbpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_configs\wutb_configs_a\data

logpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_configs\wutb_configs_a\logs.log

port=41624

configsvr=true

logappend=true

oplogSize=2048

storageEngine=wiredTiger

replSet=wutb_configs

截图:

  • 配置集wutb_configs、节点wutb_configs_b配置项和截图:

配置项:

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dbpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_configs\wutb_configs_b\data

logpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_configs\wutb_configs_b\logs.log

port=41625

configsvr=true

logappend=true

oplogSize=2048

storageEngine=wiredTiger

replSet=wutb_configs

截图:

  • 分片集wutb_configs、节点wutb_configs_c配置项和截图:

配置项:

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dbpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_configs\wutb_configs_c\data

logpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_configs\wutb_configs_c\logs.log

port=41626

configsvr=true

logappend=true

oplogSize=2048

storageEngine=wiredTiger

replSet=wutb_configs

截图:

  • 路由集wutb_mongos、节点wutb_mongos_a配置项和截图:

配置项:

Code
1
2
3
4
5
6
7
logpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_mongos\wutb_mongos_a\logs.log

port=51624

logappend=true

configdb=wutb_configs/127.0.0.1:41624,127.0.0.1:41625,127.0.0.1:41626

截图:

  • 路由集wutb_mongos、节点wutb_mongos_b配置项和截图:

配置项:

Code
1
2
3
4
5
6
7
logpath=G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_mongos\wutb_mongos_b\logs.log

port=51625

logappend=true

configdb=wutb_configs/127.0.0.1:41624,127.0.0.1:41625,127.0.0.1:41626

截图:

启动各分片服务器

新建 .bat windows批量处理文件

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
rem 启动分片a
start mongod --config G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_sharda\wutb_sharda_arbiter\config
start mongod --config G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_sharda\wutb_sharda_master\config
start mongod --config G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_sharda\wutb_sharda_slave\config

rem 启动分片b
start mongod --config G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_shardb\wutb_shardb_arbiter\config
start mongod --config G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_shardb\wutb_shardb_master\config
start mongod --config G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_shardb\wutb_shardb_slave\config

rem 启动分片c
start mongod --config G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_shardc\wutb_shardc_arbiter\config
start mongod --config G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_shardc\wutb_shardc_master\config
start mongod --config G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_shardc\wutb_shardc_slave\config

rem 启动配置集
start mongod --config G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_configs\wutb_configs_a\config
start mongod --config G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_configs\wutb_configs_b\config
start mongod --config G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_configs\wutb_configs_c\config

rem 启动路由集
start mongos.exe --config G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_mongos\wutb_mongos_a\config
start mongos.exe --config G:\mangoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.10\Wutb_Final\wutb_mongos\wutb_mongos_b\config

注意会弹出14个mongo服务器弹出,若没出现,请依次启动去查看错误所在,并查看日志文件

组合各分片、配置集和整个集群

进入分片a的主节点或者从节点的mongo:(这里都进入主节点)

mong -port 11624

\分片sharda:\

Code
1
2
3
config={_id: 'wutb_sharda', members:[{_id: 0,host:'127.0.0.1:11624',priority:2},{_id:1,host:'127.0.0.1:11625',priority:1},{_id:2,host:'127.0.0.1:11623',arbiterOnly:true}]}

rs.initiate(config)

进入分片a的主节点或者从节点的mongo:(这里都进入主节点)

mong -port 21624

*\分片shardb:\

Code
1
2
3
config={_id: 'wutb_shardb', members:[{_id: 0,host:'127.0.0.1:21624',priority:2},{_id:1,host:'127.0.0.1:21625',priority:1},{_id:2,host:'127.0.0.1:21623',arbiterOnly:true}]}

rs.initiate(config)

进入分片c的主节点或者从节点的mongo:(这里都进入主节点)

mong -port 31624

\分片shardc:**

Code
1
2
3
config={_id: 'wutb_shardc', members:[{_id: 0,host:'127.0.0.1:31624',priority:2},{_id:1,host:'127.0.0.1:31625',priority:1},{_id:2,host:'127.0.0.1:31623',arbiterOnly:true}]}

rs.initiate(config)

进入配置集的主节点或者从节点的mongo:

mong -port 41624

配置集wutb_configs:

Code
1
2
3
config = {_id : "wutb_configs",members : [{_id : 0, host : "127.0.0.1:41624" },{_id : 1, host : "127.0.0.1:41625" },{_id:2,host:"127.0.0.1:41626"}]}

rs.initiate(config)

进入路由的mongo:

mong -port 51624

Mongos配置:加入分片

Code
1
2
3
4
5
sh.addShard("wutb_sharda/127.0.0.1:11624,127.0.0.1:11625,127.0.0.1:11623")

sh.addShard("wutb_shardb/127.0.0.1:21624,127.0.0.1:21625,127.0.0.1:21623")

sh.addShard("wutb_shardc/127.0.0.1:31624,127.0.0.1:31625,127.0.0.1:31623")

数据导入例子

操作代码:

在mongos进行操作,必须在admin数据库下

Code
1
2
3
4
5
use admin

db.runCommand({enablesharding:"2017441624_wutianbao_db"})
# 哈希片键
db.runCommand({shardcollection:"2017441624_wutianbao_db.wutb_coll",key:{recru_num:"hashed"}})
文章作者: 止戈-
文章链接: http://vchamps.cn/2020/03/17/MongoDB%E5%88%86%E7%89%87%E5%BC%8F%E5%A4%8D%E5%88%B6%E9%9B%86%E7%9A%84%E9%85%8D%E7%BD%AE%E5%AE%89%E8%A3%85/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Ever-Wu