环境
- Windows10
- MongoDB Enterprise 3.2.0(官网地址)
目标
由于没有其他机器,也没开虚拟机,所以本集群将会在一条电脑上搭建
host | port | clusterRole | replSetName |
---|---|---|---|
localhost | 32011 | shardsvr | fang-s-a |
localhost | 32012 | shardsvr | fang-s-a |
localhost | 32021 | shardsvr | fang-s-b |
localhost | 32022 | shardsvr | fang-s-b |
localhost | 32101 | configsvr | fang-cs |
localhost | 32102 | configsvr | fang-cs |
搭建步骤
将再好的文件解压至任意目录,并设置环境变量,把解压目录下的bin目录添加到path路径下,这样可以方便以后启动、连接MongoDB
在解压根路径下添加配置文件d11-config.yml,,内容如下
# yml
# mongod config
# 日志位置
systemLog:
destination: file
logAppend: true
path: D:/tools/mongodb_server/mongodb-shard/d11/log/mongodb.log
# Where and how to store data.
# db存储位置
storage:
dbPath: D:/tools/mongodb_server/mongodb-shard/d11/db
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
# fork : fork and run in background 后台运行
# pidFilePath:location of pidfile
# processManagement:
# fork: true
# pidFilePath: D:/tools/mongodb_server/mongodb-shard/d11/mongod.pid
# network interfaces
# Listen to local interface only, comment to listen on all interfaces.
net:
port: 32011
bindIp: 0.0.0.0
#security: enabled disabled
#security:
# keyFile: D:/tools/mongodb_server/mongodb-shard/d11/keyfile
# clusterAuthMode: keyFile
#operationProfiling:
operationProfiling:
slowOpThresholdMs: 1000
mode: slowOp
#replication:
replication:
replSetName: fang-s-a
#sharding:
sharding:
clusterRole: shardsvr
## Enterprise-Only Options
# 审计日志设置
# auditLog:
# destination: file
# format: JSON
# path: D:/tools/mongodb_server/mongodb-shard/d11/audit.log
#snmp:
根据上面配置文件,复制五份,并修改其中参数即可带到其他分片配置文件。
比如创建d21-config.yml配置文件时把上面数据路径、日志路径及replSetName修改成d21对应的即可
比如创建s1-config.yml配置文件时把上面数据路径、日志路径及replSetName、clusterRole、port修改成s1对应的即可,具体数值可参照上面表格。
配置完成文件如下
注:每个数据库文件夹下存在子文件夹db、log(没有会无法启动)
将上面配置文件全部创建好后就可以启动服务了,由于启动的实例太多就不都设为服务了,在这里使用命令行启动,命令如下:
mongod -f D:/tools/mongodb_server/mongodb-shard/d11-config.yml
mongod -f D:/tools/mongodb_server/mongodb-shard/d12-config.yml
mongod -f D:/tools/mongodb_server/mongodb-shard/d21-config.yml
mongod -f D:/tools/mongodb_server/mongodb-shard/d22-config.yml
mongod -f D:/tools/mongodb_server/mongodb-shard/s1-config.yml
mongod -f D:/tools/mongodb_server/mongodb-shard/s2-config.yml
上面命令要分窗口启动,启动成功后不能关闭窗口。
创建replicat set
启动成功后,再打开一个命令行窗口。
登录32011端口服务,执行下列命令
C:\Users\kjojhoojk>mongo localhost:32011
MongoDB shell version: 3.2.22
connecting to: localhost:32011/test
MongoDB Enterprise > use admin
switched to db admin
MongoDB Enterprise > rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "DESKTOP-P91QLIC:32011",
"ok" : 1
}
MongoDB Enterprise fang-s-a:OTHER> rs.add("DESKTOP-P91QLIC:32012")
{ "ok" : 1 }
MongoDB Enterprise fang-s-a:PRIMARY> rs.conf()
{
"_id" : "fang-s-a",
"version" : 2,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "DESKTOP-P91QLIC:32011",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "DESKTOP-P91QLIC:32012",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5de9f50c1f4cc2f61a03f2fe")
}
}
执行完成后,退出,并分别登录32021、32101端口的服务执行上述命令。
启动MongoDB路由服务
在控制台输入命令:
mongos --port 32000 --configdb fang-cs/DESKTOP-P91QLIC:32101,DESKTOP-P91QLIC:32102 --logpath D:/tools/mongodb_server/mongodb-shard/ms/mongos.log --bind_ip 0.0.0.0
添加分片:
C:\Users\kjojhoojk>mongo localhost:32000
MongoDB shell version: 3.2.22
connecting to: localhost:32000/test
MongoDB Enterprise mongos> use admin
switched to db admin
MongoDB Enterprise mongos> sh.addShard("fang-s-a/DESKTOP-P91QLIC:32011,DESKTOP-P91QLIC:32012")
{ "shardAdded" : "fang-s-a", "ok" : 1 }
MongoDB Enterprise mongos> sh.addShard("fang-s-b/DESKTOP-P91QLIC:32021,DESKTOP-P91QLIC:32022")
{ "shardAdded" : "fang-s-b", "ok" : 1 }
至此MongoDB分片集群已设置完毕,但这不代表你创建的数据库会自动使用分片。想要你的数据库使用分片还要对具体的数据库和表进行设置,具体操作如下:
MongoDB Enterprise mongos> use admin
switched to db admin
MongoDB Enterprise mongos> db.runCommand( { "enablesharding" :"test"})
{ "ok" : 1 }
MongoDB Enterprise mongos> sh.shardCollection( "test.user",{_id:"hashed"}))
{ "collectionsharded" : "test.user", "ok" : 1 }
MongoDB Enterprise mongos> use test )
switched to db test
MongoDB Enterprise mongos> db.user.stats()
{
"sharded" : true,
"capped" : false,
"ns" : "test.user",
"count" : 0,
"size" : 0,
"storageSize" : 8192,
"totalIndexSize" : 16384,
"indexSizes" : {
"_id_" : 8192,
"_id_hashed" : 8192
},
"avgObjSize" : 0,
"nindexes" : 2,
"nchunks" : 2,
"shards" : {
现在test数据库的user表就可以使用分片存储数据了。
注:在对表设置分片时若表不为空,要对分片键设置索引,否则无法设置分片
MongoDB设置开启权限认证功能
权限认证有很多方式,这里只讲fileKey模式
在启用认证之前我们要先建立一些用户,具体命令如下:
MongoDB Enterprise mongos> use admin
switched to db admin
MongoDB Enterprise mongos> db.createUser({user:"admin",pwd:"123456",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
MongoDB Enterprise mongos> db.createUser({user:"ttt",pwd:"123456",roles:[{role:"readWrite",db:"test"}]})
Successfully added user: {
"user" : "ttt",
"roles" : [
{
"role" : "readWrite",
"db" : "test"
}
]
}
admin 用户是一个用户管理员,他只能管理用户
ttt 用户对数据库test有读写权限
关闭所有MongoDB服务,
创建keyFile文件,这个文件我们直接创建一个txt文件修改下名字即可,在里面随便输一些字符保存
修改配置文件,在上面我们的配置文件里有一些注释的代码,把他们取消注释,并将上面创建的keyFile文件复制到该路径下即可,取消注释代码如下:
security:
keyFile: D:/tools/mongodb_server/mongodb-shard/d11/keyfile
clusterAuthMode: keyFile
启动上面配置的6个MongoDB服务,然后在启动一个路由服务:
mongos --port 32000 --keyFile D:/tools/mongodb_server/mongodb-shard/keyfile --configdb fang-cs/DESKTOP-P91QLIC:32101,DESKTOP-P91QLIC:32102 --logpath D:/tools/mongodb_server/mongodb-shard/ms/mongos.log --bind_ip 0.0.0.0
注:别忘了路由服务也需要一个keyFile文件
另起一个控制台,就可以使用上面创建的账号登陆了
C:\Users\kjojhoojk>mongo localhost:32000/admin -uadmin -p123456
MongoDB shell version: 3.2.22
connecting to: localhost:32000/admin
MongoDB Enterprise mongos> show dbs
admin 0.000GB
config 0.001GB
test 0.001GB
现在MongoDB的权限认证就配置好了
配置MongoDB审计日志
只有企业版MongoDB才支持审计日志,社区版是不支持的
配置审计日志,只需把上面的配置文件中的部分解注释即可
auditLog:
destination: file
format: JSON
path: D:/tools/mongodb_server/mongodb-shard/d21/audit.json
这个也要为每一个分区服务创建一个审计日志文件
设置好配置文件后重启服务,在启动路由服务是再加上审计日志的配置即可,由于路由服务的配置太多建议添加一个路由配置文件
logpath = D:/tools/mongodb_server/mongodb-shard/ms/mongos.log
port = 32000
keyFile = D:/tools/mongodb_server/mongodb-shard/keyfile
configdb = fang-cs/DESKTOP-P91QLIC:32101,DESKTOP-P91QLIC:32102
bind_ip = 0.0.0.0
auditDestination = file
auditFormat = JSON
auditPath = D:/tools/mongodb_server/mongodb-shard/auditLog.json
启动命令:
mongos -f D:/tools/mongodb_server/mongodb-shard/ms.conf
至此MongoDB的审计日志设置完毕。