mongodb-4.x shard cluster 搭建-复制集节点为单个节点-适合开发环境

2020-06-15 11:59:47 浏览数 (1)

0

仅供学习,未经允许严禁转载。

在搭建集群之前,先将各个节点机的ip配置为简单域名,如下所示

代码语言:javascript复制
192.168.5.114 vm11 vm11
192.168.5.107 vm2 vm2
192.168.4.37 vm3 vm3

1. 下载mongodb安装包

mongodb-linux-x86_64-4.0.13.tgz

2. 解压安装包

设置环境变量

代码语言:javascript复制
MONGODB_HOME=/usr/local/mongodb
PATH=$PATH:$MONGODB_HOME/bin

3. 创建数据、配置文件、日志目录

代码语言:javascript复制
#在vm11执行命令
mkdir -p /data/cluster/data/conf
mkdir -p /data/cluster/data/shard1
mkdir -p /data/cluster/conf
mkdir -p /data/cluster/log
touch /data/cluster/log/config.log
touch /data/cluster/log/shard1.log
touch /data/cluster/log/mongos.log
代码语言:javascript复制
#在vm2执行命令
mkdir -p /data/cluster/data/conf
mkdir -p /data/cluster/data/shard2
mkdir -p /data/cluster/conf
mkdir -p /data/cluster/log
touch /data/cluster/log/config.log
touch /data/cluster/log/shard2.log
touch /data/cluster/log/mongos.log
代码语言:javascript复制
#在vm3执行命令
mkdir -p /data/cluster/data/conf
mkdir -p /data/cluster/data/shard3
mkdir -p /data/cluster/conf
mkdir -p /data/cluster/log
touch /data/cluster/log/config.log
touch /data/cluster/log/shard3.log
touch /data/cluster/log/mongos.log

4. 创建配置文件

a. 在vm11 vm2 vm3机器分别创建配置文件

/data/cluster/data/conf/config.conf

代码语言:javascript复制
systemLog:
   destination: file
   path: "/data/cluster/log/config.log"
   logAppend: true
storage:
   dbPath: "/data/cluster/data/conf_auth"
processManagement:
   fork: true
net:
   bindIp: 0.0.0.0
   port: 27100
sharding:
   clusterRole: configsvr
replication:
   replSetName: configs

b. 在vm11创建配置文件 /data/cluster/data/conf/shard1.conf

代码语言:javascript复制
systemLog:
   destination: file
   path: "/data/cluster/log/shard1.log"
   logAppend: true
storage:
   dbPath: "/data/cluster/data/shard1"
   journal:
      enabled: true
   wiredTiger:
      engineConfig:
         cacheSizeGB: 1
processManagement:
   fork: true
net:
   bindIp: 0.0.0.0
   port: 27001
sharding:
   clusterRole: shardsvr
replication:
   replSetName: shard1

c. 在vm2创建配置文件 /data/cluster/data/conf/shard2.conf

代码语言:javascript复制
systemLog:
   destination: file
   path: "/data/cluster/log/shard2.log"
   logAppend: true
storage:
   dbPath: "/data/cluster/data/shard2"
   journal:
      enabled: true
   wiredTiger:
      engineConfig:
         cacheSizeGB: 1
processManagement:
   fork: true
net:
   bindIp: 0.0.0.0
   port: 27002
sharding:
   clusterRole: shardsvr
replication:
   replSetName: shard2

d. 在vm3创建配置文件 /data/cluster/data/conf/shard3.conf

代码语言:javascript复制
systemLog:
   destination: file
   path: "/data/cluster/log/shard3.log"
   logAppend: true
storage:
   dbPath: "/data/cluster/data/shard3"
   journal:
      enabled: true
   wiredTiger:
      engineConfig:
         cacheSizeGB: 1
processManagement:
   fork: true
net:
   bindIp: 0.0.0.0
   port: 27003
sharding:
   clusterRole: shardsvr
replication:
   replSetName: shard3

e. 分别在vm11 vm2 vm3 创建配置文件 /data/cluster/data/conf/mongos.conf

代码语言:javascript复制
systemLog:
  destination: file
  logAppend: true
  path: /data/cluster/log/mongos.log
processManagement:
  fork: true
#  pidFilePath: /var/log/nginx/mongodbmongos.pid

# network interfaces
net:
  port: 27200
  bindIp: 0.0.0.0
#监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字
sharding:
   configDB: configs/vm11:27100,vm2:27100,vm3:27100

5.启动配置节点

vm11 vm2 vm3 分别执行 mongod -f /data/cluster/conf/config.conf

vm11链接mongo客户端mongo --port 27100

代码语言:javascript复制
> config = {
    _id : "configs",
     members : [
         {_id : 0, host : "vm11:27100" },
         {_id : 1, host : "vm2:27100" },
         {_id : 2, host : "vm3:27100" }
     ]
}
> rs.initiate(config);

6. 启动分片节点

vm11 执行mongod -f /data/cluster/conf/shard1.confvm11链接mongo客户端mongo --port 27001

代码语言:javascript复制
  >config = {
    _id : "shard1",
     members : [
         {_id : 0, host : "vm11:27001" }
     ]
 }
>rs.initiate(config);

vm2 执行mongod -f /data/cluster/conf/shard2.confvm2链接mongo客户端mongo --port 27002

代码语言:javascript复制
  >config = {
    _id : "shard2",
     members : [
         {_id : 0, host : "vm2:27002" }
     ]
 }
>rs.initiate(config);

vm3 执行mongod -f /data/cluster/conf/shard3.confvm3链接mongo客户端mongo --port 27003

代码语言:javascript复制
  >config = {
    _id : "shard3",
     members : [
         {_id : 0, host : "vm3:27003" }
     ]
 }
>rs.initiate(config);

7. 启动路由节点

vm11 vm2 vm3 分别执行mongos -f /data/cluster/conf/mongos.confvm11链接mongos mongo --port 27200

代码语言:javascript复制
>sh.addShard("shard1/vm11:27001")
>sh.addShard("shard2/vm2:27002")
>sh.addShard("shard3/vm3:27003")

8. 创建分片集合

vm11链接mongos mongo --port 27200

代码语言:javascript复制
>use cosmo;
>db.createCollection("szgp");#创建集合
>db.szgp.ensureIndex({_id:"hashed"},{background:1});#创建分片用的hashed索引
>db.szgp.ensureIndex({time:1},{background:1});#创建普通索引
>use admin;
>db.runCommand({ enablesharding:"cosmo" });#开启cosmo库的分片
>db.runCommand({ shardcollection: "cosmo.szgp", key: {_id:"hashed"}});#指定分片键
>use config;
>db.settings.save( { _id:"chunksize", value: 128 } );#设置chunks大小
>db.settings.update(
   { _id: "balancer" },
   {$set:{activeWindow:{"start":"22:00","stop":"06:00"}}},
   { upsert: true }
);#设置分裂迁移执行窗口事件
>db.settings.update({ _id : "balancer" }, { $unset : { activeWindow : true } });#取消分裂迁移执行窗口事件
>sh.status();查看分片设置

0 人点赞