如何使用单个 shell 脚本使用 multiple/three nodes/servers/machines 创建 MongoDB 集群?

How can I create a MongoDB cluster using multiple/three nodes/servers/machines using a single shell script?

  1. 如何从单个 remote/local 终端启动 3 配置服务器?
  2. 如何启动分片服务器?
  3. 如何将多台机器添加到分片集群?
  4. 如何在多节点集群中创建副本集?

这里你需要一个终端来启动分片服务器

1.每个分片的脚本 -

在每个分片上你都会有一个脚本cluster.sh,其中将包括

/mongodb-linux-x86_64-2.6.6/bin/mongod --replSet <shard name> --logpath <logpath of replicaset> --dbpath --port <port no> --shardsvr

应该为该分片中的每个副本集完成此操作

然后 使用脚本中提到的端口连接到 mongo 客户端以启动分片服务器。

config = { _id: <shard name>, members:[
          { _id : 0, host : <ip of the shard : its port no.> }]

通过此命令在此处添加所有副本集

rs.initiate(config)

2。每个配置服务器的脚本 (cfgserver.sh)

在每台将成为配置服务器的机器上

您将有一个脚本来启动配置服务器

/root/mongodb-linux-x86_64-2.6.6/bin/mongod --logpath <path to store config logs> --dbpath <path to store config data> --port <port no>  --configsvr

3。终端中的脚本来启动每个分片和配置服务器

对于每个分片或集群

ssh 10.x.x.x 'sh /mongodb-linux-x86_64-2.6.6/bin/cluster.sh'

每个配置服务器

ssh 10.x.x.x 'sh /mongodb-linux-x86_64-2.6.6/bin/cfgserver.sh'

在此之后,所有集群都将启动并具有所需数量的副本集 所有配置服务器也将是 运行.

我们剩下的就是向这个环境添加分片

所以会按照下面的方式进行

启动 mongo 客户端并为每个要添加的分片触发此命令

db.adminCommand( { addShard : "<name of the shard>/"+"ip of that shard:port number" } );

分片的名称将与我们在脚本中为每个分片定义的名称相同,即 cluster.sh