Mongo Sharding 集群配置

发布时间: 更新时间: 总字数:1120 阅读时间:3m 作者: IP上海 分享 网址

Mongo Sharding 集群安装部署配置

端口及目录说明

hostname

  • mongo-1
  • mongo-2
  • mongo-3

端口

配置sharding用的端口号

  • mongos: 27017
  • config: 27018
  • rs-1: 27019
  • rs-2: 27020
  • rs-3: 27021

目录

配置目录:

/etc/
└── mongodb
    ├── conf
    │   ├── mongod-config-svr.conf
    │   ├── mongos.conf
    │   ├── rs-1.conf
    │   ├── rs-2.conf
    │   └── rs-3.conf
    └── service
        ├── mongod-config-svr.service
        ├── mongod-rs-1.service
        ├── mongod-rs-2.service
        ├── mongod-rs-3.service
        └── mongos.service

创建脚本:

mkdir /etc/mongodb -p
mkdir /etc/mongodb/conf
mkdir /etc/mongodb/service

数据目录:

  • mongo configsvr:/data-01/mongo-conf-svr
  • mongod rs-1:/data-01/mongo-rs-1
  • mongod rs-2:/data-01/mongo-rs-2
  • mongod rs-3:/data-01/mongo-rs-3

创建脚本:

cd /data-01
mkdir mongo-conf-svr
mkdir mongo-rs-1
mkdir mongo-rs-2
mkdir mongo-rs-3

安装

配置 yum 源,安装 RPM 包:

yum install mongodb-org mongodb-server-org

配置

mongod(shard)配置

配置文件:vim /etc/mongodb/conf/rs-1.conf

systemLog:
  verbosity: 0
  quiet: false
  syslogFacility: user
  path: "/var/log/mongodb/rs-1.log"
  logAppend: true
  logRotate: rename
  destination: file
  timeStampFormat: iso8601-utc

storage:
  dbPath: "/data-01/mongo-rs-1"
  indexBuildRetry: true

  journal:
    enabled: true

  directoryPerDB: true
  syncPeriodSecs: 60
  engine: wiredTiger

  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      statisticsLogDelaySecs: 0
      journalCompressor: snappy
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: snappy
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: "/var/run/mongodb/mongod-rs-1.pid"

net:
  bindIp: 0.0.0.0
  port: 27019
  http:
    enabled: true
    RESTInterfaceEnabled: true

operationProfiling:
  slowOpThresholdMs: 800
  mode: "slowOp"

security:
  authorization: "disabled"

replication:
  oplogSizeMB: 102400
  replSetName: "rs-1"

注意:配置 rs-2/rs-3rs-1 相同,注意修改以下参数:

systemLog:
  path: "/var/log/mongodb/rs-2.log"
storage:
  dbPath: "/data-01/mongo-rs-2"
net:
  bindIp: 0.0.0.0
  port: 27020
replication:
  replSetName: "rs-2"

rs-1/rs-2/rs-3 创建完后,执行配置副本集群,以rs-1为例:

mongo 127.0.0.1:27019
cfg={_id: "rs-1", members: [{_id: 0, host: "mongo-2:27019", priority: 10}, {_id: 1, host: "mongo-3:27019", priority: 10}]}
rs.initiate(cfg)

注意:不同rs的端口是不同的。

配置 rs 启动脚本

mongod-rs-1.service

[Unit]
Description=High-performance, schema-free document-oriented database
After=syslog.target network.target

[Service]
Type=forking
User=mongod
ExecStart = /usr/bin/numactl --interleave=all -- /usr/bin/mongod --config /etc/mongodb/conf/rs-1.conf run
PrivateTmp=true
LimitNOFILE=64000
TimeoutStartSec=180

[Install]
WantedBy=multi-user.target
sudo systemctl enable /etc/mongodb/service/mongod-rs-1.service

mongo config server 配置

配置文件:vim /etc/mongodb/conf/mongod-config-svr.conf

systemLog:
  verbosity: 0
  quiet: true
  syslogFacility: user
  path: "/var/log/mongodb/config-svr.log"
  logAppend: true
  logRotate: rename
  destination: file
  timeStampFormat: iso8601-utc

storage:
  dbPath: "/data-01/mongo-conf-svr"
  indexBuildRetry: true

  journal:
    enabled: true

  directoryPerDB: true
  syncPeriodSecs: 60
  engine: wiredTiger

  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      statisticsLogDelaySecs: 0
      journalCompressor: snappy
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: snappy
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: "/var/run/mongodb/mongod-configsvr.pid"

net:
  bindIp: 0.0.0.0
  port: 27018
  http:
    enabled: true
    RESTInterfaceEnabled: true
  # maxIncomingConnections: 65536
  wireObjectCheck: true
  ipv6: false

operationProfiling:
  slowOpThresholdMs: 800
  mode: "off"

sharding:
  clusterRole: configsvr

replication:
  oplogSizeMB: 2048
  replSetName: "configsvr"

3台均配置好后,搭建 configsvrrs:

mongo --port 27018
cfg={_id: "configsvr", members: [{_id: 0, host: "mongo-1:27018", priority: 10}, {_id: 1, host: 'mongo-2:27018', priority: 10}, {_id: 2, host: "mongo-3:27018, priority: 10}]}
rs.initiate(cfg)

配置 config server 启动脚本

mongod-config-svr.service

[Unit]
Description=High-performance, schema-free document-oriented database
After=syslog.target network.target

[Service]
Type=forking
User=mongod
ExecStart = /usr/bin/numactl --interleave=all -- /usr/bin/mongod --config /etc/mongodb/conf/mongod-config-svr.conf run
PrivateTmp=true
LimitNOFILE=64000
TimeoutStartSec=180

[Install]
WantedBy=multi-user.target
sudo systemctl enable /etc/mongodb/service/mongod-config-svr.service

mongos 配置

配置文件:vim /etc/mongodb/conf/mongos.conf

systemLog:
  quiet: false
  path: /var/log/mongodb/mongos.log
  logAppend: false
  destination: file

processManagement:
  fork: true

net:
  bindIp: 0.0.0.0
  port: 27017
  # maxIncomingConnections: 65536
  wireObjectCheck: true
  ipv6: false

sharding:
  configDB: configsvr/mongo-1:27018,mongo-2:27018,mongo-3:27018

配置 mongos 启动脚本

mongos.service

[Unit]
Description=High-performance, schema-free document-oriented database
After=syslog.target network.target

[Service]
Type=forking
User=mongod
ExecStart=/usr/bin/mongos --quiet -f /etc/mongodb/conf/mongos.conf
PrivateTmp=true
LimitNOFILE=64000
TimeoutStartSec=180

[Install]
WantedBy=multi-user.target
sudo systemctl enable /etc/mongodb/service/mongos.service

配置sharding

mongo -port 27017
sh.help()
mongos> sh.addShard("rs-1/mongo-2:27019")
{ "shardAdded" : "rs-1", "ok" : 1 }

sh.addShard("rs-2/mongo-2:27020")
sh.addShard("rs-3/mongo-2:27021")

mongos> sh.enableSharding("ceilometer");
{ "ok" : 1 }
mongos> sh.shardCollection("ceilometer.meter",{counter_name:1,timestamp:1})
{ "collectionsharded" : "ceilometer.meter", "ok" : 1 }

查看shard:

use admin;
db.runCommand({listshards:1})

数据库 启动分区功能

mongos> sh.enableSharding("ceilometer")
{ "ok" : 1 }
mongos> sh.status()

collections 进行顺序分区

mongos> sh.shardCollection("ceilometer.meter",{"counter_name" : 1, "timestamp" : 1})
mongos> sh.status()

创建用户/密码/索引

mongo --host 127.0.0.1 --eval '
db = db.getSiblingDB("ceilometer");
db.createUser({user: "ceilometer",
pwd: "<pwd>",
roles: [ "readWrite", "dbAdmin" ]})'

添加新节点(monitor1)

mongod(shard)配置:

mongo --port 27019
rs1:PRIMARY> rs.add("mongo-1:27019")
{ "ok" : 1 }
mongo --port 27020
rs1:PRIMARY> rs.add("mongo-1:27020")
{ "ok" : 1 }
mongo --port 27021
rs1:PRIMARY> rs.add("mongo-1:27021")
{ "ok" : 1 }

新增config server:

将已有config server data scp至新节点后,在启动config服务即可。

配置chunk大小

mongo --port <mongos-port>
use config
db.settings.find()
db.settings.save( { _id:"chunksize", value: NumberLong(32) } )                # db.settings.save( { _id:"chunksize", value: 32 } )

配置日志归档

/var/log/mongodb/*.log {
    daily
    rotate 28
    copytruncate
    # delaycompress
    compress
    notifempty
    missingok
    sharedscripts
    postrotate
        /bin/kill -SIGUSR1 `cat /var/run/mongodb/mongod-configsvr.pid 2>/dev/null` 2> /dev/null
        /bin/kill -SIGUSR1 `cat /var/run/mongodb/mongod-rs-1.pid 2>/dev/null` 2> /dev/null
        /bin/kill -SIGUSR1 `cat /var/run/mongodb/mongod-rs-2.pid 2>/dev/null` 2> /dev/null
        /bin/kill -SIGUSR1 `cat /var/run/mongodb/mongod-rs-3.pid 2>/dev/null` 2> /dev/null
        sleep 3
        rm -f $(ls /var/log/mongodb/*.log.$(date +%Y-%m-)* 2>/dev/null | sort) || true
    endscript
}

参考

  1. https://github.com/ansible/ansible-examples/tree/master/mongodb
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数