安装包下载地址MongoDB Community Download | MongoDB,下载完成后上传服务器;
集群模式是多台服务器共同组成的,需要将安装包上传到每个服务器上;
集群规划,我这里有三台服务器,集群角色有三个mongos路由、Config Server配置服务器、分片服务器;
具体划分如下:
Mongos路由:192.168.100.101:27021
配置服务器2台:192.168.100.101:28018,192.168.100.102:28018, 192.168.100.103:28018
分片服务器2台(2个分片+3个复制集):
分片1:192.168.100.101:27017,192.168.100.102:27017,192.168.100.103:27017
分片2:192.168.100.101:27018,192.168.100.102:27018,192.168.100.103:27018
0. 软件包目录:/opt/softpackage,安装目录:/opt/software/ mongodb-4.2.14
1. 解压安装包(3台服务器执行):tar -zxvf mongodb-linux-x86_64-rhel62-4.2.14.tgz
2. 移动到安装目录/opt/software/,修改目录名为mongodb-4.2.14(3台服务器执行)
3. 分片集群安装;
安装路径:
分片1:
192.168.100.101:27017 /opt/software/mongodb-4.2.14/sharding/cluster/sharding1/rs1
192.168.100.102:27017 /opt/software/mongodb-4.2.14/sharding/cluster/sharding1/rs2
192.168.100.103:27017 /opt/software/mongodb-4.2.14/sharding/cluster/sharding1/rs3
分片2:
192.168.100.101:27018 /opt/software/mongodb-4.2.14/sharding/cluster/sharding2/rs1
192.168.100.102:27018 /opt/software/mongodb-4.2.14/sharding/cluster/sharding2/rs2
192.168.100.103:27018 /opt/software/mongodb-4.2.14/sharding/cluster/sharding2/rs3
(1) 在上面安装路径下分别创建data和log目录,并在log目录下创建mongdb.log文件;
(2) 在上面安装路径下分别创建一个mongodb.conf,列举分片1和分片2下rs1的的配置文件如下(其他两个slave配置文件只有数据目录、日志目录不同外,其他配置相同);
分片1:
fork=true
dbpath=/opt/software/mongodb-4.2.14/sharding/cluster/sharding1/rs1/data
port=27017
bind_ip=0.0.0.0
logpath=/opt/software/mongodb-4.2.14/sharding/cluster/sharding1/rs1/log/mongodb.log
logappend=true
replSet=hy_repl1
shardsvr=true
分片2:
fork=true
dbpath=/opt/software/mongodb-4.2.14/sharding/cluster/sharding2/rs1/data
port=27018
bind_ip=0.0.0.0
logpath=/opt/software/mongodb-4.2.14/sharding/cluster/sharding2/rs1/log/mongodb.log
logappend=true
replSet=hy_repl2
shardsvr=true
(3) 启动分片集群:
启动命令:
/opt/software/mongodb-4.2.14/bin/mongod -f /opt/software/mongodb-4.2.14/sharding/cluster/sharding1/rs1/mongodb.conf
/opt/software/mongodb-4.2.14/bin/mongod -f /opt/software/mongodb-4.2.14/sharding/cluster/sharding1/rs2/mongodb.conf
/opt/software/mongodb-4.2.14/bin/mongod -f /opt/software/mongodb-4.2.14/sharding/cluster/sharding1/rs3/mongodb.conf
/opt/software/mongodb-4.2.14/bin/mongod -f /opt/software/mongodb-4.2.14/sharding/cluster/sharding2/rs1/mongodb.conf
/opt/software/mongodb-4.2.14/bin/mongod -f /opt/software/mongodb-4.2.14/sharding/cluster/sharding2/rs2/mongodb.conf
/opt/software/mongodb-4.2.14/bin/mongod -f /opt/software/mongodb-4.2.14/sharding/cluster/sharding2/rs3/mongodb.conf
登录复制集,添加初始化配置:
/opt/software/mongodb-4.2.14/bin/mongo --port 27017
/opt/software/mongodb-4.2.14/bin/mongo --port 27018
进入客户端后,执行初始化命令(这里演示分片1,分片2修改一下_id(副本集名称)和host即可)
var rsconf = {
_id:'hy_repl1', //这里的_id要与配置文件离指定的服务所属的复制集相同
members:
[
{
_id:1,
host:'192.168.100.101:27017' //成员所属节点的ip以及该成员服务启动时所占的端口
},
{
_id:2,
host:'192.168.100.102:27017'
},
{
_id:3,
host:'192.168.100.103:27017'
}
]
}
初始化配置(加载rsconf配置文件)
rs.initiate(rsconf);
状态查看
rs.status();
结果如下:
{
"set" : "hy_repl1",
"date" : ISODate("2021-05-28T02:50:46.098Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1622170239, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2021-05-28T02:50:39.118Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1622170239, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2021-05-28T02:50:39.118Z"),
"appliedOpTime" : {
"ts" : Timestamp(1622170239, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1622170239, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2021-05-28T02:50:39.118Z"),
"lastDurableWallTime" : ISODate("2021-05-28T02:50:39.118Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1622170219, 1),
"lastStableCheckpointTimestamp" : Timestamp(1622170219, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2021-05-28T02:43:18.912Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1622169788, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2021-05-28T02:43:18.920Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2021-05-28T02:43:19.721Z")
},
"members" : [
{
"_id" : 1,
"name" : "192.168.100.101:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 618,
"optime" : {
"ts" : Timestamp(1622170239, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-05-28T02:50:39Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1622169798, 1),
"electionDate" : ISODate("2021-05-28T02:43:18Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "192.168.100.102:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 457,
"optime" : {
"ts" : Timestamp(1622170239, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1622170239, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-05-28T02:50:39Z"),
"optimeDurableDate" : ISODate("2021-05-28T02:50:39Z"),
"lastHeartbeat" : ISODate("2021-05-28T02:50:45.060Z"),
"lastHeartbeatRecv" : ISODate("2021-05-28T02:50:45.913Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.100.101:27017",
"syncSourceHost" : "192.168.100.101:27017",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 3,
"name" : "192.168.100.103:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 457,
"optime" : {
"ts" : Timestamp(1622170239, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1622170239, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-05-28T02:50:39Z"),
"optimeDurableDate" : ISODate("2021-05-28T02:50:39Z"),
"lastHeartbeat" : ISODate("2021-05-28T02:50:45.059Z"),
"lastHeartbeatRecv" : ISODate("2021-05-28T02:50:45.932Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.100.101:27017",
"syncSourceHost" : "192.168.100.101:27017",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1622170239, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1622170239, 1)
}
4. Config Server配置服务器安装:
(1) 安装路径:
192.168.100.101:28018 /opt/software/mongodb-4.2.14/sharding/config/node1
192.168.100.102:28018 /opt/software/mongodb-4.2.14/sharding/config/node2
192.168.100.103:27018 /opt/software/mongodb-4.2.14/sharding/config/node3
(2) 在node1、node2、node3目录下分别创建data和log目录,并在log目录下新建mongodb.log文件
(3) 分别在node1、node2、node3目录下添加mongodb.conf文件,例举node1的配置文件mongodb.conf 如下(node2、node3的配置文件只有IP、数据目录、日志目录不同外,其他配置相同);
storage:
dbPath: /opt/software/mongodb-4.2.14/sharding/config/node1/data
journal:
enabled: true
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: false
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
systemLog:
destination: file
path: "/opt/software/mongodb-4.2.14/sharding/config/node1/log/mongodb.log"
logAppend: true
net:
bindIp: 192.168.100.101
port: 28018
processManagement:
fork: true
replication:
replSetName: shardingConfig
oplogSizeMB: 2048
sharding:
clusterRole: configsvr
注意:
因为这三台是可复制集,所以配置文件要加上:
因为这三台是配置服务器,所以配置文件要加上:
配置启动流程:
启动命令:
/opt/software/mongodb-4.2.14/bin/mongod -f /opt/software/mongodb-4.2.14/sharding/config/node1/mongodb.conf
/opt/software/mongodb-4.2.14/bin/mongod -f /opt/software/mongodb-4.2.14/sharding/config/node2/mongodb.conf
/opt/software/mongodb-4.2.14/bin/mongod -f /opt/software/mongodb-4.2.14/sharding/config/node3/mongodb.conf
登录配置节点:
/opt/software/mongodb-4.2.14/bin/mongo -host 192.168.100.101 -port 28018
初始化命令:
var rsconf = {
_id:'shardingConfig',
members:
[
{
_id:1,
host:'192.168.100.101:28018'
},
{
_id:2,
host:'192.168.100.102:28018'
},
{
_id:3,
host:'192.168.100.103:28018'
}
]
}
rs.initiate(rsconf)
状态查看:
rs.status()
结果如下:
{
"set" : "shardingConfig",
"date" : ISODate("2021-05-28T03:25:36.144Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"configsvr" : true,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastCommittedWallTime" : ISODate("1970-01-01T00:00:00Z"),
"appliedOpTime" : {
"ts" : Timestamp(1622172335, 31),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1622172335, 31),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2021-05-28T03:25:35.725Z"),
"lastDurableWallTime" : ISODate("2021-05-28T03:25:35.725Z")
},
"lastStableRecoveryTimestamp" : Timestamp(0, 0),
"lastStableCheckpointTimestamp" : Timestamp(0, 0),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2021-05-28T03:25:35.368Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1622172325, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2021-05-28T03:25:35.402Z")
},
"members" : [
{
"_id" : 1,
"name" : "192.168.100.101:28018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 402,
"optime" : {
"ts" : Timestamp(1622172335, 31),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-05-28T03:25:35Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1622172335, 1),
"electionDate" : ISODate("2021-05-28T03:25:35Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "192.168.100.102:28018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 10,
"optime" : {
"ts" : Timestamp(1622172325, 1),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(1622172325, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2021-05-28T03:25:25Z"),
"optimeDurableDate" : ISODate("2021-05-28T03:25:25Z"),
"lastHeartbeat" : ISODate("2021-05-28T03:25:35.385Z"),
"lastHeartbeatRecv" : ISODate("2021-05-28T03:25:35.777Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 3,
"name" : "192.168.100.103:28018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 10,
"optime" : {
"ts" : Timestamp(1622172325, 1),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(1622172325, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2021-05-28T03:25:25Z"),
"optimeDurableDate" : ISODate("2021-05-28T03:25:25Z"),
"lastHeartbeat" : ISODate("2021-05-28T03:25:35.384Z"),
"lastHeartbeatRecv" : ISODate("2021-05-28T03:25:35.776Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(1622172325, 1),
"electionId" : ObjectId("7fffffff0000000000000001")
},
"lastCommittedOpTime" : Timestamp(0, 0),
"$clusterTime" : {
"clusterTime" : Timestamp(1622172335, 31),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1622172335, 31)
}
5. Mongos路由安装:
安装路径:
192.168.100.101:27021 /opt/software/mongodb-4.2.14/sharding/mongos
(1) 在上面安装目录,新建mongos目录;
(2) 在mongos目录下新建data和log目录,并在log目录下创建mongodb.log文件;
(3) 在mongos目录下添加mongodb.conf文件,内容如下:
systemLog:
destination: file
path: "/opt/software/mongodb-4.2.14/sharding/mongos/log/mongodb.log"
logAppend: true
net:
bindIp: 192.168.100.101
port: 27021
sharding:
configDB: shardingConfig/192.168.100.101:28018,192.168.100.102:28018,192.168.100.103:28018
processManagement:
fork: true
启动mongos
/opt/software/mongodb-4.2.14/bin/mongos -config /opt/software/mongodb-4.2.14/sharding/mongos/mongodb.conf
登录mongos节点
/opt/software/mongodb-4.2.14/bin/mongo 192.168.100.101:27021
添加集群中的分片节点:
切换admin:use admin
添加shard1复制集
db.runCommand(
{
addshard: "hy_repl1/192.168.100.101:27017,192.168.100.102:27017,192.168.100.103:27017",
name: "shard1"
}
)
添加shard2复制集
db.runCommand(
{
addshard: "hy_repl2/192.168.100.101:27018,192.168.100.102:27018,192.168.100.103:27018",
name: "shard2"
}
)
查看分片
db.runCommand({listshards: 1})
结果如下:
{
"shards" : [
{
"_id" : "shard1",
"host" : "hy_repl1/192.168.100.101:27017,192.168.100.102:27017,192.168.100.103:27017" ,
"state" : 1
},
{
"_id" : "shard2",
"host" : "hy_repl2/192.168.100.101:27018,192.168.100.102:27018,192.168.100.103:27018" ,
"state" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1622181077, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1622181077, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
查看分片状态
sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("60b062af78c3a214abd8c13e")
}
shards:
{ "_id" : "shard1", "host" : "hy_repl1/192.168.100.101:27017,192.168.100.102:27017,192.168.100.103: 27017", "state" : 1 }
{ "_id" : "shard2", "host" : "hy_repl2/192.168.100.101:27018,192.168.100.102:27018,192.168.100.103: 27018", "state" : 1 }
active mongoses:
"4.2.14" : 1
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 }
开启数据库分片配置
在admin库下执行以下命令
db.runCommand({enablesharding:"testdb"})
创建分片的键(id)
db.runCommand({shardcollection:"testdb.users",key:{id:1}})
创建索引(如果不是空集合,不是第一次操作)
use testdb
db.users.ensureIndex({id:1})
添加测试数据:
use testdb
var arr=[];
for(var i=0;i<2000000;i++){
var uid = i;
var name = "name" + i;
arr.push({"id":uid,"name":name});
}
db.users.insertMany(arr)
查看分区状态:
sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("60b062af78c3a214abd8c13e")
}
shards:
{ "_id" : "shard1", "host" : "hy_repl1/192.168.100.101:27017,192.168.100.102:27017,192.168.100.103: 27017", "state" : 1 }
{ "_id" : "shard2", "host" : "hy_repl2/192.168.100.101:27018,192.168.100.102:27018,192.168.100.103: 27018", "state" : 1 }
active mongoses:
"4.2.14" : 1
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:
516 : Success
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
shard1 512
shard2 512
too many chunks to print, use verbose if you want to force print
{ "_id" : "testdb", "primary" : "shard2", "partitioned" : true, "version" : { "uuid" : UUI D("b44938b5-1487-4b41-9274-d0b13029d643"), "lastMod" : 1 } }
testdb.users
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
shard1 4
shard2 4
{ "id" : { "$minKey" : 1 } } -->> { "id" : 1 } on : shard1 Timestamp(2, 0)
{ "id" : 1 } -->> { "id" : 500001 } on : shard1 Timestamp(3, 0)
{ "id" : 500001 } -->> { "id" : 750002 } on : shard1 Timestamp(5, 0)
{ "id" : 750002 } -->> { "id" : 1000003 } on : shard2 Timestamp(4, 1)
{ "id" : 1000003 } -->> { "id" : 1250004 } on : shard2 Timestamp(5, 1)
{ "id" : 1250004 } -->> { "id" : 1500004 } on : shard2 Timestamp(3, 2)
{ "id" : 1500004 } -->> { "id" : 1974999 } on : shard2 Timestamp(3, 3)
{ "id" : 1974999 } -->> { "id" : { "$maxKey" : 1 } } on : shard1 Timestamp(4, 0 )
删除分片命令:
db.runCommand({removeShard:”shard2”})
1.关闭路由服务器
执行一下命令找到关于mongo的进程,然后kill -9杀掉:
ps aux | grep mongo
kill -9 进程id
2.关闭配置服务器
下面是一个副本脚本,其它副本只需修改副本目录即可;
/opt/software/mongodb-4.2.14/bin/mongod --shutdown -f /opt/software/mongodb-4.2.14/sharding/config/node1/mongodb.conf
完成后执行chmod u+x stop-mongodb-config.sh
指定sh stop-mongodb-config.sh命令关闭
3.关闭分片复制集
下面是分片1和分片2的第一个副本关闭命令,放到一个脚本即可,其它副本只需修改副本目录即可;
/opt/software/mongodb-4.2.14/bin/mongod --shutdown -f /opt/software/mongodb-4.2.14/sharding/cluster/sharding1/rs1/mongodb.conf
/opt/software/mongodb-4.2.14/bin/mongod --shutdown -f /opt/software/mongodb-4.2.14/sharding/cluster/sharding2/rs1/mongodb.conf
完成后执行chmod u+x stop-mongodb-shard.sh
指定sh stop-mongodb-shard.sh命令关闭
1.启动配置服务器
下面是一个副本脚本,其它副本只需修改副本目录即可;
/opt/software/mongodb-4.2.14/bin/mongod -f /opt/software/mongodb-4.2.14/sharding/config/node1/mongodb.conf
完成后执行chmod u+x start-mongodb-config.sh
指定sh start-mongodb-config.sh命令关闭2.启动分片副本集
下面是分片1和分片2的第一个副本启动命令,放到一个脚本即可,其它副本只需修改副本目录即可;
/opt/software/mongodb-4.2.14/bin/mongod --shutdown -f /opt/software/mongodb-4.2.14/sharding/cluster/sharding1/rs1/mongodb.conf
/opt/software/mongodb-4.2.14/bin/mongod --shutdown -f /opt/software/mongodb-4.2.14/sharding/cluster/sharding2/rs1/mongodb.conf
完成后执行chmod u+x start-mongodb-shard.sh
指定sh start-mongodb-shard.sh命令启动
3.启动路由服务器
脚本内容
/opt/software/mongodb-4.2.14/bin/mongos -config /opt/software/mongodb-4.2.14/sharding/mongos/mongodb.conf
完成后执行chmod u+x start-mongodb-router.sh
指定sh start-mongodb- router.sh命令启动
db.createUser( #创建集群管理员
{
user:"cluster",
pwd:"cluster",
roles:[{role:"clusterAdmin",db:"admin"},{role:"clusterManager",db:"admin"},{role:"clusterMonitor",db:"admin"}]
}
)
> use testdb #切换到testdb数据库,不用事先创建
> db.createUser( #创建特定库的特定用户
{
user:"test",
pwd:"test",
roles:[{role:"readWrite",db:"testdb"},{role:"dbAdmin",db:"testdb"},{role:"userAdmin",db:"testdb"}]
}
)
> use admin
> db.system.users.find() #查看创建的