天天看點

mongos叢集部署

mongos 分片伺服器


1.部署的伺服器ip位址
    172.16.0.151
    172.16.0.173
    172.16.0.220

2.etcd版本
    wget  https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-5.0.4.tgz
3.三台伺服器安裝目錄 
  3個資料分片副本集,1個mongos副本集,1個config副本集
  /data  安裝目錄
  /data01到/data05
    /data01/share1  #資料分片副本集1目錄
    /data02/share2  #資料分片副本集2目錄
    /data03/share3  #資料分片副本集3目錄
    /data04/configsvr #config副本集目錄
    /data05/mongos   #mongos副本集目錄


4.每個記憶體限制0.5G;
[root@im-04 ~]# ls /data/mongodb/conf/
configsvr.conf  mongos.conf  shard1.conf  shard2.conf  shard3.conf

[root@im-04 conf]# vim shard1.conf 
systemLog:
    destination: file
    logAppend: true
    path: /data02/mongodb/logs/shard2.log
storage:
    dbPath: /data02/mongodb/data
    journal:
      enabled: true
    wiredTiger:
      engineConfig:
        configString : cache_size=500M
processManagement:
    fork: true
    pidFilePath: /data02/mongodb/shard2/shard2.pid
net:
   port: 27020
   bindIp: 0.0.0.0
replication:
   replSetName: shard2
sharding:
   clusterRole: shardsvr
   
   
[root@im-04 conf]# vim shard2.conf 
systemLog:
    destination: file
    logAppend: true
    path: /data03/mongodb/logs/shard3.log
storage:
    dbPath: /data03/mongodb/data
    journal:
       enabled: true
    wiredTiger:
      engineConfig:
        configString : cache_size=500M
processManagement:
    fork: true
    pidFilePath: /data03/mongodb/shard3/shard3.pid
net:
    port: 27021
    bindIp: 0.0.0.0
replication:
    replSetName: shard3
sharding:
    clusterRole: shardsvr
[root@im-04 conf]# vim shard1.conf     
systemLog:
    destination: file
    logAppend: true
    path: /data01/mongodb/logs/shard1.log
storage:
    dbPath: /data01/mongodb/data
    journal:
      enabled: true
    wiredTiger:
      engineConfig:
        configString : cache_size=500M
processManagement:
    fork: true
    pidFilePath: /data01/mongodb/shard1/shard1.pid
net:       
   port: 27019
   bindIp: 0.0.0.0
replication:
   replSetName: shard1
sharding:
   clusterRole: shardsvr



[root@im-04 conf]# cat configsvr.conf 
systemLog:
    destination: file
    logAppend: true
    path: /data04/mongodb/logs/configsvr.log

# Where and how to store data.
storage:
    dbPath: /data04/mongodb/data
    journal:
      enabled: true

# how the process runs
processManagement:
    fork: true  # fork and run in background
    pidFilePath: /data04/mongodb/configsvr/configsvr.pid  # location of pidfile
 
# network interfaces
net:
    port: 27018
    bindIp: 0.0.0.0
sharding:
    clusterRole: configsvr

replication:
    replSetName: config

        
[root@im-04 conf]# cat mongos.conf 
systemLog:
  destination: file
  logAppend: true
  path: /data05/mongodb/logs/mongos.log
processManagement:
  fork: true
  pidFilePath: /data05/mongodb/mongos/mongos.pid
net:
  port: 27017
  bindIp: 0.0.0.0
sharding:
  configDB: config/172.16.0.151:27018,172.16.0.173:27018,172.16.0.220:27018



5.建立mongodb啟動使用者
    groupadd mongod
    useradd -g mongod mongod

6.配置mongodb目錄權限
    chown -R mongod:mongod /data/mongodb

7.啟動
    /data/mongodb/bin/mongod -f /data/mongodb/conf/configsvr.conf
    /data/mongodb/bin/mongod   -f /data/mongodb/conf/shard1.conf
    /data/mongodb/bin/mongod   -f /data/mongodb/conf/shard2.conf
    /data/mongodb/bin/mongod   -f /data/mongodb/conf/shard3.conf
    /data/mongodb/bin/mongos -f /data/mongodb/conf/mongos.conf


8.登入任意一台配置伺服器,初始化配置副本集

    #連接配接
    mongo 172.16.0.151 --port 27018
    初始化configServer副本集
    rs.initiate(
      {
        _id: "config",
        configsvr: true,
        members: [
          { _id : 0, host : "172.16.0.151:27018" },
          { _id : 1, host : "172.16.0.173:27018" },
          { _id : 2, host : "172.16.0.220:27018" }
        ]
      }
    )
    檢視節點狀态
    rs.status()

    啟動三台伺服器的shard1 server
    /data/mongodb/bin/mongod   -f /data/mongodb/conf/shard1.conf

    mongo 172.16.0.151 --port 27020
    rs.initiate(
      {
        _id: "shard2",
        members: [
          { _id : 0, host : "172.16.0.151:27020" },
          { _id : 1, host : "172.16.0.173:27020" },
          { _id : 2, host : "172.16.0.220:27020" }
        ]
      }
    )

    mongo 172.16.0.173 --port 27021
    rs.initiate(
      {
        _id: "shard3",
        members: [
          { _id : 0, host : "172.16.0.173:27021" },
          { _id : 1, host : "172.16.0.151:27021" },
          { _id : 2, host : "172.16.0.220:27021" }
        ]
      }
    )


9、啟用分片

    mongo 172.16.0.220 --port 27017
    sh.addShard( "shard1/172.16.0.151:27019,172.16.0.173:27019,172.16.0.220:27019")
    sh.addShard( "shard2/172.16.0.151:27020,172.16.0.173:27020,172.16.0.220:27020")
    sh.addShard( "shard3/172.16.0.151:27021,172.16.0.173:27021,172.16.0.220:27021")


這個指令可以檢視MongoDB的記憶體限制情況,檢視結果如下:
 
    SECONDARY> db.hostInfo()

        wiredTiger:
          engineConfig:
            configString : cache_size=500M