mkdir -p /work/mongo-cluster/mongos/{bin,data,conf,logs,keys}
vim /work/mongo-cluster/mongos/bin/start.sh
内容
docker run -d --name mongos \
--network=host \
--log-opt max-size=10m \
-v /etc/timezone:/etc/timezone \
-v /etc/localtime:/etc/localtime \
-v /work/mongo-cluster/mongos/data:/data \
-v /work/mongo-cluster/mongos/keys:/keys \
-v /work/mongo-cluster/mongos/logs:/var/log/mongodb \
-v /work/mongo-cluster/mongos/conf/mongod.conf:/etc/mongo/mongod.conf \
mongo:4.2.5 mongos -f /etc/mongo/mongod.conf \
chmod 755 /work/mongo-cluster/mongos/bin/start.sh
vim /work/mongo-cluster/mongos/conf/mongod.conf
内容
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongo.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
#security:
security:
keyFile: /keys/mongo-cluster.key
#operationProfiling:
#replication:
#sharding:
sharding:
configDB: configs/172.30.3.231:27018,172.30.3.234:27018,172.30.3.238:27018
## Enterprise-Only Options:
#auditLog:
#snmp:
cp ~/mongo-cluster.key /work/mongo-cluster/mongos/keys/
sudo chown 999 /work/mongo-cluster/mongos/keys/mongo-cluster.key
sudo chmod 400 /work/mongo-cluster/mongos/keys/mongo-cluster.key
sudo chown 999 /work/mongo-cluster/mongos/data/ /work/mongo-cluster/mongos/logs/
/work/mongo-cluster/mongos/bin/start.sh
连接任意节点
docker exec -it mongos mongo --port 27017
创建用户,该用户在集群环境中可见(相当于单机版root用户,用于创建用户,数据库等操作)
db.getSiblingDB("admin").createUser(
{
"user" : "root",
"pwd" : "q1w2e3r4",
roles: [ { "role" : "root", "db" : "admin" } ]
}
)
返回值
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
连接任意节点
docker exec -it mongos mongo --port 27017
管理用户验证,使用你的密码替换
use admin
db.auth("root","q1w2e3r4")
添加shard1
sh.addShard("shard1/172.30.3.231:27019,172.30.3.234:27019,172.30.3.238:27019");
返回值
{
"shardAdded" : "shard1",
"ok" : 1,
"operationTime" : Timestamp(1586591415, 6),
"$clusterTime" : {
"clusterTime" : Timestamp(1586591415, 6),
"signature" : {
"hash" : BinData(0,"4zIj3U9o/rI+rvAP74pP9S5thuI="),
"keyId" : NumberLong("6814344100507025437")
}
}
}
添加shard2
sh.addShard("shard2/172.30.3.231:27020,172.30.3.234:27020,172.30.3.238:27020");
返回值
{
"shardAdded" : "shard2",
"ok" : 1,
"operationTime" : Timestamp(1586591430, 3),
"$clusterTime" : {
"clusterTime" : Timestamp(1586591430, 3),
"signature" : {
"hash" : BinData(0,"JZW45knbJ2Lc8fEEBuA+AIxgAxI="),
"keyId" : NumberLong("6814344100507025437")
}
}
}
添加shard3
sh.addShard("shard3/172.30.3.231:27021,172.30.3.234:27021,172.30.3.238:27021");
返回值
{
"shardAdded" : "shard3",
"ok" : 1,
"operationTime" : Timestamp(1586591443, 5),
"$clusterTime" : {
"clusterTime" : Timestamp(1586591443, 5),
"signature" : {
"hash" : BinData(0,"/uQ/0I3Bp8/O5KqwP59Hp39UNAA="),
"keyId" : NumberLong("6814344100507025437")
}
}
}
sh.status()
返回值
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5e9169db98ee1533f5b5e254")
}
shards:
{ "_id" : "shard1", "host" : "shard1/172.30.3.231:27019,172.30.3.234:27019", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/172.30.3.234:27020,172.30.3.238:27020", "state" : 1 }
{ "_id" : "shard3", "host" : "shard3/172.30.3.231:27021,172.30.3.238:27021", "state" : 1 }
active mongoses:
"4.2.5" : 3
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:
shard1 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。