mongodb分片集群搭建

mongo版本:mongodb-linux-x86_64-4.0.16.tgz
三台虚拟机
192.168.150.133 192.168.150.134 192.168.150.135
集群环境
2个分片复制集
shard1(192.168.150.133:27017 192.168.150.134:27017 192.168.150.135:27017)
shard2(192.168.150.133:27018 192.168.150.134:27018 192.168.150.135:27018)
解压/opt/mongodb目录下
tar -zxvf mongodb-linux-x86_64-4.0.16.tgz

创建/opt/mongo配置目录
添加(yidian_repl)复制集配置文件:mongo.conf (133/134/135)

#守护进程模式
fork = true
##数据目录
dbpath=/opt/mongo/data/db
##端口
port = 27017
bind_ip=0.0.0.0
##日志文件
logpath=/opt/mongo/logs/mongodb1.log
##日志追加
logappend=true
##复制集的名字
replSet=yidian_repl
smallfiles=true
#分片集群必须要的属性
shardsvr=true

添加(yidian_repl2)复制集配置文件:mongo2.conf

 (133/134/135)
#守护进程模式
fork = true
##数据目录
dbpath=/opt/mongo/data/db
##端口
port = 27018
bind_ip=0.0.0.0
##日志文件
logpath=/opt/mongo/logs/mongodb2.log
##日志追加
logappend=true
##复制集的名字
replSet=yidian_repl2
smallfiles=true
#分片集群必须要的属性
shardsvr=true

启动副本集(yidian_repl)
/opt/mongodb/mongodb-linux-x86_64-4.0.16/bin/mongod -f /opt/mongo/mongo.conf
启动副本集(yidian_repl2)
/opt/mongodb/mongodb-linux-x86_64-4.0.16/bin/mongod -f /opt/mongo/mongo2.conf

登录复制集,添加初始化配置
进入mongo客户端
配置(yidian_repl)
/opt/mongodb/mongodb-linux-x86_64-4.0.16/bin/mongo -port 27017

进入27017客户端后,执行初始化命令

var rsconf = { _id:"yidian_repl", members:[

                     {_id:1,host:"192.168.150.133:27017"},

                     {_id:2,host:"192.168.150.134:27017"},

                     {_id:3,host:"192.168.150.135:27017"}

                ]

         }
rs.initiate(rsconf);
rs.status();//查看状态

配置(yidian_rep2)
/opt/mongodb/mongodb-linux-x86_64-4.0.16/bin/mongo -port 27018

进入27018客户端后,执行初始化命令

var rsconf = { _id:"yidian_repl2", members:[

                     {_id:1,host:"192.168.150.133:27018"},

                     {_id:2,host:"192.168.150.134:27018"},

                     {_id:3,host:"192.168.150.135:27018"}

                ]

         }
rs.initiate(rsconf);

搭建config节点复制集

创建config节点配置文件:mongo-cfg.conf (133/134/135)

    systemLog:
  destination: file
  #日志存储位置
  path: "/opt/mongo/mongo-cfg/logs/mongodb.log"
  logAppend: true
storage:
  journal:
    enabled: true
  #数据文件存储位置
  dbPath: "/opt/mongo/mongo-cfg/data"
  #是否一个库一个文件夹
  directoryPerDB: true
  #WT引擎配置
  wiredTiger:
    engineConfig:
      #WT最大使用cache(根据服务器实际情况调节)
      cacheSizeGB: 1
      #是否将索引也按数据库名单独存储
      directoryForIndexes: true
    #表压缩配置
    collectionConfig:
      blockCompressor: zlib
    #索引配置
    indexConfig:
      prefixCompression: true
#端口配置
net:
  bindIp: 192.168.150.133
  port: 28018
replication:
  oplogSizeMB: 2048
  replSetName: csvr
sharding:
  clusterRole: configsvr
processManagement:
  fork: true

启动配置复制集
/opt/mongodb/mongodb-linux-x86_64-4.0.16/bin/mongod -f /opt/mongo/mongo-cfg.conf

初始化配置节点

登录(任意一台)

/opt/mongodb/mongodb-linux-x86_64-4.0.16/bin/mongo -host 192.168.133 -port 28018
初始化命令

rs.initiate( {
   _id : "csvr",
   configsvr: true,
   members: [
      { _id: 0, host: "192.168.150.133:28018" },
      { _id: 1, host: "192.168.150.134:28018" },
      { _id: 2, host: "192.168.150.135:28018" }
   ]
   
})

mongos节点配置
mongos配置文件:mongos.conf

systemLog:
  destination: file
  path: "/opt/mongo/mongos/log/mongos.log"
  logAppend: true
net:
  bindIp: 192.168.150.133
  port: 28017
# 将confige server 添加到路由
sharding:
  configDB: csvr/192.168.150.133:28018,192.168.150.134:28018,192.168.150.135:28018
processManagement:
  fork: true

启动mongos

/opt/mongodb/mongodb-linux-x86_64-4.0.16/bin/mongos -config /opt/mongo/mongos/mongos.conf
登录mongos节点
/opt/mongodb/mongodb-linux-x86_64-4.0.16/bin/mongo 192.168.150.133:28017
添加集群中的分片节点
切换admin:
use admin
添加shard1复制集
db.runCommand( { addshard : "yidian_repl/192.168.150.133:27017,192.168.150.134:27017,192.168.150.135:27017",name:"shard1"} )
添加shard2复制集
db.runCommand( { addshard : "yidian_repl2/192.168.150.133:27018,192.168.150.134:27018,192.168.150.135:27018",name:"shard2"} )
mongos查看分片状态:
sh.status()

你可能感兴趣的:(mongodb分片集群搭建)