文章目录
- 1,安装mongodb服务
- 二、配置分片集群
- 三、启动各种服务
- 4,配置mongos连接各分片
-
- configsvr
- shard
- mongos
1,安装mongodb服务
参考:https://www.mongodb.com/docs/manual/tutorial/deploy-shard-cluster/
#安装软件包 [root@c7 mongodb]# rpm -qf /usr/bin/mongo mongodb-org-shell-5.0.7-1.el7.x86_64 [root@c7 mongodb]# rpm -qf /usr/bin/mongod mongodb-org-server-5.0.7-1.el7.x86_64 [root@c7 mongodb]# rpm -qf /usr/bin/mongos mongodb-org-mongos-5.0.7-1.el7.x86_64 #初始数据目录、日志目录、文件目录配置 [root@c7 mongodb]# mkdir -pv /export/server/mongodb/{log,conf,pid,shard1,shard2,shard3,mongos,configServer}
二、配置分片集群
- 192.168.56.7 c7 mongo1.example.net mongos.example.net
主机 | 角色 | 端口 |
---|---|---|
192.168.56.7 | configsrv * 1 | 27018 |
192.168.56.7 | shard * 3 | 27027, 27028, 27029 |
192.168.56.7 | mongos * 1 | 27017 |
[root@c7 mongodb]# ll total 44 drwxr-xr-x. 2 root root 125 Apr 2 17:17 conf drwxr-xr-x. 4 root root 4096 Apr 2 17:34 configServer drwxr-xr-x. 3 root root 132 Apr 2 17:19 log -rw-------. 1 root root 1166 Apr 2 17:19 nohup.out drwxr-xr-x. 2 root root 84 Apr 2 17:18 pid drwxr-xr-x. 4 root root 4096 Apr 2 17:34 shard1 drwxr-xr-x. 4 root root 4096 Apr 2 17:34 shard2 drwxr-xr-x. 4 root root 4096 Apr 2 17:34 shard3 -rwxr-xr-x. 1 root root 145 Apr 1 22:13 start_configSrv.sh -rwxr-xr-x. 1 root root 95 Apr 1 20:02 start_mongos.sh -rwxr-xr-x. 1 root root 94 Apr 1 19:08 start_shar1.sh -rwxr-xr-x. 1 root root 94 Apr 1 19:14 start_shar2.sh -rwxr-xr-x. 1 root root 94 Apr 1 19:53 start_shard3.sh [root@c7 mongodb]# tail -n 100 conf/*
==> conf/configSrv.conf <==
systemLog:
destination: file
path: /export/server/mongodb/log/configServer.log
logAppend: true
storage:
dbPath: /export/server/mongodb/configServer
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
processManagement:
pidFilePath: /export/server/mongodb/pid/configServer.pid
net:
port: 27018
bindIp: mongo1.example.net
maxIncomingConnections: 10000
replication:
replSetName: cs0
sharding:
clusterRole: configsvr
#security:
# authorization: enabled
# keyFile: /export/server/mongodb/conf/mongo.keyfile
==> conf/mongo.keyfile <==
58/40wjopamqNIECltm/8c225gVD58pIpAWFAkXFDTClTS2FxD2xISFIMqRVHOqF
HDCIlj33XQdgWI3xXhlRQhg11WQf3ECCDEp+OfX8I96dWPmAlKM/UkWp
==> conf/mongos.conf <==
systemLog:
destination: file
path: "/export/server/mongodb/log/mongos.log"
logAppend: true
processManagement:
fork: true
net:
port: 27017
bindIp: mongos.example.net
sharding:
configDB: "cs0/mongo1.example.net:27018"
#security:
# keyFile: "/export/server/mongodb/conf/mongo.keyfile"
# clusterAuthMode: "keyFile"
==> conf/shard1.conf <==
systemLog:
destination: file
path: /export/server/mongodb/log/shard1.log
logAppend: true
storage:
dbPath: /export/server/mongodb/shard1
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
processManagement:
pidFilePath: /export/server/mongodb/pid/shard1.pid
net:
port: 27027
bindIp: mongo1.example.net
maxIncomingConnections: 10000
replication:
replSetName: "shard1"
sharding:
clusterRole: shardsvr
#security:
# authorization: enabled
# keyFile: /export/server/mongodb/conf/mongo.keyfile
==> conf/shard2.conf <==
systemLog:
destination: file
path: /export/server/mongodb/log/shard2.log
logAppend: true
storage:
dbPath: /export/server/mongodb/shard2
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
processManagement:
pidFilePath: /export/server/mongodb/pid/shard2.pid
net:
port: 27028
bindIp: mongo1.example.net
maxIncomingConnections: 10000
replication:
replSetName: "shard2"
sharding:
clusterRole: shardsvr
#security:
# authorization: enabled
# keyFile: /export/server/mongodb/conf/mongo.keyfile
==> conf/shard3.conf <==
systemLog:
destination: file
path: /export/server/mongodb/log/shard3.log
logAppend: true
storage:
dbPath: /export/server/mongodb/shard3
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
processManagement:
pidFilePath: /export/server/mongodb/pid/shard3.pid
net:
port: 27029
bindIp: mongo1.example.net
maxIncomingConnections: 10000
replication:
replSetName: "shard3"
sharding:
clusterRole: shardsvr
#security:
# authorization: enabled
# keyFile: /export/server/mongodb/conf/mongo.keyfile
3,启动各服务
[root@c7 mongodb]# tail -n 100 *.sh
==> start_configSrv.sh <==
#!/bin/bash
conf_path=/export/server/mongodb/conf
#nohup mongod -f ${conf_path}/configSrv.conf &
setsid mongod -f ${conf_path}/configSrv.conf &
==> start_mongos.sh <==
#!/bin/bash
conf_path=/export/server/mongodb/conf
nohup mongos -f ${conf_path}/mongos.conf &
==> start_shar1.sh <==
#!/bin/bash
conf_path=/export/server/mongodb/conf
nohup mongod -f ${conf_path}/shard1.conf &
==> start_shar2.sh <==
#!/bin/bash
conf_path=/export/server/mongodb/conf
nohup mongod -f ${conf_path}/shard2.conf &
==> start_shard3.sh <==
#!/bin/bash
conf_path=/export/server/mongodb/conf
nohup mongod -f ${conf_path}/shard3.conf &
#启动所有服务
[root@c7 mongodb]# chmod +x *.sh
[root@c7 mongodb]# find . -name '*.sh' |xargs -n 1 |sh
[root@c7 mongodb]# ps -ef |grep mongo
root 7284 1 0 19:58 pts/0 00:00:28 mongod -f /export/server/mongodb/conf/shard1.conf
root 7287 1 0 19:58 pts/0 00:00:27 mongod -f /export/server/mongodb/conf/shard2.conf
root 7290 1 0 19:58 pts/0 00:00:26 mongod -f /export/server/mongodb/conf/shard3.conf
root 7294 1 0 19:58 ? 00:00:40 mongod -f /export/server/mongodb/conf/configSrv.conf
root 7297 1 0 19:58 ? 00:00:09 mongos -f /export/server/mongodb/conf/mongos.conf
4,配置mongos连接各分片
configsvr
## configsvr * 1
[root@c7 mongodb]# mongo --host mongo1.example.net --port 27018
MongoDB shell version v5.0.7
connecting to: mongodb://mongo1.example.net:27018/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session {
"id" : UUID("66c7a310-4f27-423d-9720-51c55bb116ed") }
MongoDB server version: 5.0.7
---
The server generated these startup warnings when booting:
2022-04-02T17:18:03.466+00:00: The configured WiredTiger cache size is more than 80% of available RAM. See http://dochub.mongodb.org/core/faq-memory-diagnostics-wt
2022-04-02T17:18:05.932+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2022-04-02T17:18:05.932+00:00: You are running this process as the root user, which is not recommended
2022-04-02T17:18:05.932+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
2022-04-02T17:18:05.932+00:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
---
> cfg={
_id:"cs0",configsvr: true, members:[ {
_id:0,host:"mongo1.example.net:27018"} ]} ;
{
"_id" : "cs0",
"configsvr" : true,
"members" : [
{
"_id" : 0,
"host" : "mongo1.example.net:27018"
}
]
}
> rs.initiate(cfg)
{
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(1648919973, 1),
"electionId" : ObjectId("000000000000000000000000")
},
"lastCommittedOpTime" : Timestamp(1648919973, 1)
}
cs0:SECONDARY> use admin
switched to db admin
cs0:PRIMARY> db.createUser({
user:"root",pwd:"123456",roles:[{
role:"userAdminAnyDatabase",db:"admin"}, "readWriteAnyDatabase"]})
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
},
"readWriteAnyDatabase"
]
}
cs0:PRIMARY>
shard
cfg={ _id:"shard3", members:[ {_id:0,host:"mongo1.example.net:27029",priority:1 }, {_id:1,host:"mongo2.example.net:27029",arbiterOnly:true }] };
## shard * 3
[root@c7 mongodb]# mongo --host mongo1.example.net --port 27027
MongoDB shell version v5.0.7
connecting to: mongodb://mongo1.example.net:27027/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session {
"id" : UUID("121b3fae-664a-468e-bb1a-6c3f346f2682") }
MongoDB server version: 5.0.7
> mongo --host mongo1.example.net --port 27027
uncaught exception: SyntaxError: unexpected token: identifier :
@(shell):1:8
> cfg={
_id:"shard1", members:[ {
_id:0,host:"mongo1.example.net:27027" }] };
{
"_id" : "shard1",
"members" : [
{
"_id" : 0,
"host" : "mongo1.example.net:27027"
}
]
}
> rs.initiate(cfg)
{
"ok" : 1 }
shard1:SECONDARY> use admin
switched to db admin
shard1:SECONDARY> db.createUser({
user:"root",pwd:"123456",roles:[{
role:"userAdminAnyDatabase",db:"admin"}, "readWriteAnyDatabase"]})
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
},
"readWriteAnyDatabase"
]
}
shard1:PRIMARY>
mongos
## mongos * 1
[root@c7 mongodb]# mongo --host mongos.example.net --port 27017 -u root --authenticationDatabase "admin" -p '123456'
MongoDB shell version v5.0.7
connecting to: mongodb://mongos.example.net:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session {
"id" : UUID("8b7e0129-ab73-492a-8df0-837d64f51345") }
MongoDB server version: 5.0.7
mongos> sh.addShard("shard1/mongo1.example.net:27027")
{
"shardAdded" : "shard1",
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1648920248, 6),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1648920248, 6)
}
mongos> sh.addShard("shard2/mongo1.example.net:27028")
{
"shardAdded" : "shard2",
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1648920251, 5),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1648920251, 5)
}
mongos> sh.addShard("shard3/mongo1.example.net:27029")
{
"shardAdded" : "shard3",
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1648920254, 5),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1648920254, 5)
}
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("624885a547d2892cc1ef377f")
}
shards:
{
"_id" : "shard1", "host" : "shard1/mongo1.example.net:27027", "state" : 1, "topologyTime" : Timestamp(1648920248, 3) }
{
"_id" : "shard2", "host" : "shard2/mongo1.example.net:27028", "state" : 1, "topologyTime" : Timestamp(1648920251, 3) }
{
"_id" : "shard3", "host" : "shard3/mongo1.example.net:27029", "state" : 1, "topologyTime" : Timestamp(1648920254, 3) }
active mongoses:
"5.0.7" : 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 }
mongos>