七、mongos安装配置
7.1、创建目录
代码语言:javascript复制mkdir -p /work/mongo-cluster/mongos/{bin,data,conf,logs,keys}
7.2、编辑启动脚本
代码语言:javascript复制vim /work/mongo-cluster/mongos/bin/start.sh
内容
代码语言:javascript复制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
代码语言:javascript复制chmod 755 /work/mongo-cluster/mongos/bin/start.sh
7.3、编辑配置文件
代码语言:javascript复制vim /work/mongo-cluster/mongos/conf/mongod.conf
内容
代码语言:javascript复制# 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:
7.4、复制keyfile
代码语言:javascript复制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
7.5、设置目录权限
代码语言:javascript复制sudo chown 999 /work/mongo-cluster/mongos/data/ /work/mongo-cluster/mongos/logs/
7.6、启动应用
代码语言:javascript复制/work/mongo-cluster/mongos/bin/start.sh
7.7、创建管理用户
连接任意节点
代码语言:javascript复制docker exec -it mongos mongo --port 27017
创建用户,该用户在集群环境中可见(相当于单机版root用户,用于创建用户,数据库等操作)
代码语言:javascript复制db.getSiblingDB("admin").createUser(
{
"user" : "root",
"pwd" : "q1w2e3r4",
roles: [ { "role" : "root", "db" : "admin" } ]
}
)
返回值
代码语言:javascript复制Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
7.8、将分片加入集群
连接任意节点
代码语言:javascript复制docker exec -it mongos mongo --port 27017
管理用户验证,使用你的密码替换
代码语言:javascript复制use admin
db.auth("root","q1w2e3r4")
添加shard1
代码语言:javascript复制sh.addShard("shard1/172.30.3.231:27019,172.30.3.234:27019,172.30.3.238:27019");
返回值
代码语言:javascript复制{
"shardAdded" : "shard1",
"ok" : 1,
"operationTime" : Timestamp(1586591415, 6),
"$clusterTime" : {
"clusterTime" : Timestamp(1586591415, 6),
"signature" : {
"hash" : BinData(0,"4zIj3U9o/rI rvAP74pP9S5thuI="),
"keyId" : NumberLong("6814344100507025437")
}
}
}
添加shard2
代码语言:javascript复制sh.addShard("shard2/172.30.3.231:27020,172.30.3.234:27020,172.30.3.238:27020");
返回值
代码语言:javascript复制{
"shardAdded" : "shard2",
"ok" : 1,
"operationTime" : Timestamp(1586591430, 3),
"$clusterTime" : {
"clusterTime" : Timestamp(1586591430, 3),
"signature" : {
"hash" : BinData(0,"JZW45knbJ2Lc8fEEBuA AIxgAxI="),
"keyId" : NumberLong("6814344100507025437")
}
}
}
添加shard3
代码语言:javascript复制sh.addShard("shard3/172.30.3.231:27021,172.30.3.234:27021,172.30.3.238:27021");
返回值
代码语言:javascript复制{
"shardAdded" : "shard3",
"ok" : 1,
"operationTime" : Timestamp(1586591443, 5),
"$clusterTime" : {
"clusterTime" : Timestamp(1586591443, 5),
"signature" : {
"hash" : BinData(0,"/uQ/0I3Bp8/O5KqwP59Hp39UNAA="),
"keyId" : NumberLong("6814344100507025437")
}
}
}
7.9、查询状态
代码语言:javascript复制sh.status()
返回值
代码语言:javascript复制--- 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)