前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MongoDB分片集群部署(六)

MongoDB分片集群部署(六)

原创
作者头像
堕落飞鸟
发布2022-02-24 14:56:02
3450
发布2022-02-24 14:56:02
举报
文章被收录于专栏:飞鸟的专栏飞鸟的专栏
七、mongos安装配置
7.1、创建目录
mkdir -p /work/mongo-cluster/mongos/{bin,data,conf,logs,keys}
7.2、编辑启动脚本
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
7.3、编辑配置文件
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:
7.4、复制keyfile
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、设置目录权限
sudo chown 999 /work/mongo-cluster/mongos/data/ /work/mongo-cluster/mongos/logs/
7.6、启动应用
/work/mongo-cluster/mongos/bin/start.sh
7.7、创建管理用户

连接任意节点

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"
                }
        ]
}
7.8、将分片加入集群

连接任意节点

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")
                }
        }
}
7.9、查询状态
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 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 七、mongos安装配置
    • 7.1、创建目录
      • 7.2、编辑启动脚本
        • 7.3、编辑配置文件
          • 7.4、复制keyfile
            • 7.5、设置目录权限
              • 7.6、启动应用
                • 7.7、创建管理用户
                  • 7.8、将分片加入集群
                    • 7.9、查询状态
                    相关产品与服务
                    容器镜像服务
                    容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档