【MongoDB 高可用篇】MongoDB Sharding Cluster集群环境搭建
目录
分片(sharding)是指将数据拆分,将其存放在不同机器上的过程,有时也成为分区(partitioning)。将数据分散到不同的机器上,不需要功能强大的大型计算机就可以存储更多的数据,处理更大的负载。MongoDB的分片机制可以创建一个包含许多台机器(分片)的集群,将数据分散在集群中,每个分片维护着一个数据集合的子集,与单机服务器和副本集相比,使用集群架构可以使应用程序具有更大的数据处理能力。本篇将演示如何搭建MongoDB分片集群环境。
1 软件环境
使用的软件分别为:
- VirtualBox 6.0
- Oracle Linux 6.7
- MongoDB 4.2.0
2 集群架构
3 集群搭建过程
3.1 创建配置服务器副本集
分别在192.168.56.102-----192.168.56.104执行如下操作:
1)创建用户和组
[root@mongos01 ~]# groupadd -g 1001 mongod
[root@mongos01 ~]# useradd -g 1001 mongod
[root@mongos01 ~]# id mongod
uid=500(mongod) gid=1001(mongod) groups=1001(mongod)
[root@mongos01 ~]# passwd mongod
Changing password for user mongod.
New password:
BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
2)创建目录
192.168.56.102如下目录:
[root@mongos01 ~]# mkdir -p /u01/shard1/data
[root@mongos01 ~]# mkdir -p /u01/shard1/log
[root@mongos01 ~]#
[root@mongos01 ~]# mkdir -p /u01/config/data
[root@mongos01 ~]# mkdir -p /u01/config/log
[root@mongos01 ~]#
[root@mongos01 ~]# mkdir -p /u01/mongos
[root@mongos01 ~]# mkdir -p /u01/conf
[root@mongos01 ~]#
[root@mongos01 ~]# chown -R mongod:mongod /u01/
192.168.56.103如下目录:
[root@mongos02 ~]# mkdir -p /u01/shard2/data
[root@mongos02 ~]# mkdir -p /u01/shard2/log
[root@mongos02 ~]#
[root@mongos02 ~]# mkdir -p /u01/config/data
[root@mongos02 ~]# mkdir -p /u01/config/log
[root@mongos02 ~]#
[root@mongos02 ~]# mkdir -p /u01/mongos
[root@mongos02 ~]# mkdir -p /u01/conf
[root@mongos02 ~]#
[root@mongos02 ~]# chown -R mongod:mongod /u01/
192.168.56.104如下目录:
[root@mongos03 ~]# mkdir -p /u01/config/data
[root@mongos03 ~]# mkdir -p /u01/config/log
[root@mongos03 ~]#
[root@mongos03 ~]# mkdir -p /u01/mongos
[root@mongos03 ~]# mkdir -p /u01/conf
3)解压缩安装包
[root@mongos01 ~]# tar zxvf mongodb-linux-x86_64-rhel62-4.2.0.tgz
mongodb-linux-x86_64-rhel62-4.2.0/THIRD-PARTY-NOTICES.gotools
mongodb-linux-x86_64-rhel62-4.2.0/README
mongodb-linux-x86_64-rhel62-4.2.0/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel62-4.2.0/MPL-2
mongodb-linux-x86_64-rhel62-4.2.0/LICENSE-Community.txt
mongodb-linux-x86_64-rhel62-4.2.0/bin/mongodump
mongodb-linux-x86_64-rhel62-4.2.0/bin/mongorestore
mongodb-linux-x86_64-rhel62-4.2.0/bin/mongoexport
mongodb-linux-x86_64-rhel62-4.2.0/bin/mongoimport
mongodb-linux-x86_64-rhel62-4.2.0/bin/mongostat
mongodb-linux-x86_64-rhel62-4.2.0/bin/mongotop
mongodb-linux-x86_64-rhel62-4.2.0/bin/bsondump
mongodb-linux-x86_64-rhel62-4.2.0/bin/mongofiles
mongodb-linux-x86_64-rhel62-4.2.0/bin/mongoreplay
mongodb-linux-x86_64-rhel62-4.2.0/bin/mongod
mongodb-linux-x86_64-rhel62-4.2.0/bin/mongos
mongodb-linux-x86_64-rhel62-4.2.0/bin/mongo
mongodb-linux-x86_64-rhel62-4.2.0/bin/install_compass
[root@mongos01 ~]#
4)安装MongoDB
[root@mongos01 ~]# mv mongodb-linux-x86_64-rhel62-4.2.0 mongodb
[root@mongos01 ~]# mv mongodb /usr/local/
[root@mongos01 ~]# cd /usr/local/
[root@mongos01 local]# chown -R mongod:mongod mongodb/
5)生成Key File文件
[mongod@mongos01 conf]$ openssl rand -base64 741 > /u01/conf/keyfile
[mongod@mongos01 conf]$ chmod 400 /u01/conf/keyfile
6)编写配置文件
[mongod@mongos01 ~]$ cat /u01/conf/config.cnf
systemLog:
path: /u01/config/log/config.log
logAppend: true
destination: file
processManagement:
fork: true
pidFilePath: /u01/config/config.pid
net:
port: 27019
bindIp: localhost,192.168.56.102
security:
keyFile: /u01/conf/keyfile
storage:
dbPath: /u01/config/data
journal:
enabled: true
directoryPerDB: true
engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 0.5
directoryForIndexes: true
replication:
oplogSizeMB: 512
replSetName: configReplSet
enableMajorityReadConcern: true
sharding:
clusterRole: configsvr
7)启动所有的配置服务器
[mongod@mongos01 ~]$ mongod -f /u01/conf/config.cnf
about to fork child process, waiting until server is ready for connections.
forked process: 3164
child process started successfully, parent exiting
8)连接到某一个配置服务器
[mongod@mongos01 ~]$ mongo localhost:27019
MongoDB shell version v4.2.0
connecting to: mongodb://localhost:27019/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1987b9e3-5248-4c21-bf48-d0f2b5f4264f") }
MongoDB server version: 4.2.0
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
>
9)初始化副本集
> cfg={
... _id:'configReplSet',
... configsvr:true,
... members:[
... {_id:0,host:'192.168.56.102:27019'},
... {_id:1,host:'192.168.56.103:27019'},
... {_id:2,host:'192.168.56.104:27019'}
... ]
... }
{
"_id" : "configReplSet",
"configsvr" : true,
"members" : [
{
"_id" : 0,
"host" : "192.168.56.102:27019"
},
{
"_id" : 1,
"host" : "192.168.56.103:27019"
},
{
"_id" : 2,
"host" : "192.168.56.104:27019"
}
]
}
> rs.initiate(cfg)
{
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(1570337121, 1),
"electionId" : ObjectId("000000000000000000000000")
},
"lastCommittedOpTime" : Timestamp(0, 0)
}
configReplSet:SECONDARY>
10)创建root用户
configReplSet:PRIMARY> use admin
switched to db admin
configReplSet:PRIMARY> db.createUser({user:'root',pwd:'root',roles:[{role:'root',db:'admin'}]})
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
3.2 创建分片副本集
下面的操作对分片SD1和SD2都需操作,用于创建分片副本集,以分片sd1的副本集为例:
1)启动所有分片副本集成员
[mongod@mongos01 conf]$ mongod -f sd1.cnf
about to fork child process, waiting until server is ready for connections.
forked process: 3746
child process started successfully, parent exiting
[mongod@mongod_sd0101 conf]$ mongod -f sd1.cnf
about to fork child process, waiting until server is ready for connections.
forked process: 3234
child process started successfully, parent exiting
[mongod@mongod_sd0102 conf]$ mongod -f sd1.cnf
about to fork child process, waiting until server is ready for connections.
forked process: 3205
child process started successfully, parent exiting
2)连接到某一个副本集成员
[mongod@mongos01 conf]$ mongo localhost:27018/admin
MongoDB shell version v4.2.0
connecting to: mongodb://localhost:27018/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("98156909-d21d-4cd2-89ae-cdcde0d607c9") }
MongoDB server version: 4.2.0
>
3)初始化副本集
如果没有指定arbiterOnly=true,则创建的是辅助成员,不是仲裁成员。
> cfg={
... _id:'rs01',
... members:[
... {_id:0,host:'192.168.56.102:27018'},
... {_id:1,host:'192.168.56.105:27018'},
... {_id:2,host:'192.168.56.106:27018'}
... ]
... }
{
"_id" : "rs01",
"members" : [
{
"_id" : 0,
"host" : "192.168.56.102:27018"
},
{
"_id" : 1,
"host" : "192.168.56.105:27018"
},
{
"_id" : 2,
"host" : "192.168.56.106:27018"
}
]
}
> rs.initiate(cfg)
{ "ok" : 1 }
4)创建root用户
rs01:PRIMARY> db.createUser({user:'root',pwd:'root',roles:[{role:'root',db:'admin'}]})
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
3.3 配置mongos
在所有mongos服务器上配置mongos配置文件,以192.168.56.102为例:
1)编写mongos配置文件
[mongod@mongos01 conf]$ cat mongos.cnf
systemLog:
path: /u01/mongos/mongos.log
logAppend: true
destination: file
processManagement:
fork: true
pidFilePath: /u01/mongos/mongos.pid
net:
port: 27017
bindIp: localhost,192.168.56.102
security:
keyFile: /u01/conf/keyfile
sharding:
configDB: configReplSet/192.168.56.102:27019,192.168.56.103:27019,192.168.56.103:27019
2)启动mongos
[mongod@mongos01 conf]$ mongos -f mongos.cnf
about to fork child process, waiting until server is ready for connections.
forked process: 4049
child process started successfully, parent exiting
[mongod@mongos02 conf]$ mongos -f mongos.cnf
about to fork child process, waiting until server is ready for connections.
forked process: 3821
child process started successfully, parent exiting
[mongod@mongos03 conf]$ mongos -f mongos.cnf
about to fork child process, waiting until server is ready for connections.
forked process: 4406
child process started successfully, parent exiting
3)连接到mongos
[mongod@mongos01 conf]$ mongo localhost:27017/admin -uroot -p
MongoDB shell version v4.2.0
Enter password:
connecting to: mongodb://localhost:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("e8394b78-370f-45e5-a66e-42b06767bf99") }
MongoDB server version: 4.2.0
mongos>
3.4 增加分片
mongos> sh.addShard('rs01/192.168.56.102:27018')
{
"shardAdded" : "rs01",
"ok" : 1,
"operationTime" : Timestamp(1570342471, 8),
"$clusterTime" : {
"clusterTime" : Timestamp(1570342471, 8),
"signature" : {
"hash" : BinData(0,"pGfJiEZwStgckkKC2j5vtajEKDg="),
"keyId" : NumberLong("6744546634224369689")
}
}
}
mongos> sh.addShard('rs01/192.168.56.105:27018')
{
"shardAdded" : "rs01",
"ok" : 1,
"operationTime" : Timestamp(1570342484, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1570342484, 1),
"signature" : {
"hash" : BinData(0,"/pTHzPzyFHYO+9/BWg7e4i+0kO0="),
"keyId" : NumberLong("6744546634224369689")
}
}
}
mongos> sh.addShard('rs01/192.168.56.106:27018')
{
"shardAdded" : "rs01",
"ok" : 1,
"operationTime" : Timestamp(1570342484, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1570342484, 1),
"signature" : {
"hash" : BinData(0,"/pTHzPzyFHYO+9/BWg7e4i+0kO0="),
"keyId" : NumberLong("6744546634224369689")
}
}
}
3.5 激活数据库分片
mongos> sh.enableSharding('scott')
{
"ok" : 1,
"operationTime" : Timestamp(1570343575, 3),
"$clusterTime" : {
"clusterTime" : Timestamp(1570343575, 3),
"signature" : {
"hash" : BinData(0,"aGGowPNgY89oEZyKAp2owg9peCE="),
"keyId" : NumberLong("6744546634224369689")
}
}
}
3.6 查看分片
mongos> db.runCommand( { listshards : 1 } )
{
"shards" : [
{
"_id" : "rs01",
"host" : "rs01/192.168.56.102:27018,192.168.56.105:27018,192.168.56.106:27018",
"state" : 1
},
{
"_id" : "rs02",
"host" : "rs02/192.168.56.107:27018,192.168.56.108:27018",
"state" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1570344745, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1570344745, 2),
"signature" : {
"hash" : BinData(0,"ZGZJa6TrR2kGFZoSiS24u/PlgWY="),
"keyId" : NumberLong("6744546634224369689")
}
}
}
3.7 查看分片集群状态
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5d99716e39ac52865c39c815")
}
shards:
{ "_id" : "rs01", "host" : "rs01/192.168.56.102:27018,192.168.56.105:27018,192.168.56.106:27018", "state" : 1 }
{ "_id" : "rs02", "host" : "rs02/192.168.56.107:27018,192.168.56.108:27018", "state" : 1 }
active mongoses:
"4.2.0" : 2
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
rs01 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : rs01 Timestamp(1, 0)
{ "_id" : "scott", "primary" : "rs02", "partitioned" : true, "version" : { "uuid" : UUID("ae91198c-5d9b-4669-8ee3-3f11edce03a6"), "lastMod" : 1 } }
3.8 基于范围分片集合
mongos> db.emp.createIndex({id:1})
{
"raw" : {
"rs02/192.168.56.107:27018,192.168.56.108:27018" : {
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
},
"ok" : 1,
"operationTime" : Timestamp(1570374109, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1570374109, 2),
"signature" : {
"hash" : BinData(0,"JSvp1wekNPW9I0z4ZcJgcBI8Il4="),
"keyId" : NumberLong("6744546634224369689")
}
}
}
mongos> sh.shardCollection('scott.emp',{id:1})
{
"collectionsharded" : "scott.emp",
"collectionUUID" : UUID("31c59034-8748-47b5-989d-c48a55894274"),
"ok" : 1,
"operationTime" : Timestamp(1570374118, 7),
"$clusterTime" : {
"clusterTime" : Timestamp(1570374118, 7),
"signature" : {
"hash" : BinData(0,"OetKkHBvetjxA/t4Tg1X0Qw+ALA="),
"keyId" : NumberLong("6744546634224369689")
}
}
}
mongos>
至此,MongoDB数据库分片集群环境搭建成功,可以根据具体的分片类型进行数据库的分片操作。