MongoDB集群1

予早 2026-04-30 23:53:28
Categories: Tags:

MongoShell - 简介. MongoShell是一个互动的JavaScript接口的

主从分片

mongodb
  sharded_cluster
    shard_rs1_27118
      log
      data
        db
    shard_rs1_27218
      log
      data
        db
    shard_rs1_27318
      log
      data
        db
        
    shard_rs2_27418
      log
      data
        db
    shard_rs2_27518
      log
      data
        db
    shard_rs2_27618
      log
      data
        db
    config_rs_27119
      log
      data
        db
    config_rs_27219
      log
      data
        db
    config_rs_27319
      log
      data
        db
        
    mongos_27117
    mongos_27217
# 配置节点
mkdir -p ./mongodb/sharded_cluster/mongos_27117/log \ &
mkdir -p ./mongodb/sharded_cluster/mongos_27217/data/db \ &

# 分片副本集1
mkdir -p ./mongodb/sharded_cluster/shard_rs1_27118/log \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs1_27118/data/db \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs1_27218/log \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs1_27318/data/db \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs1_27318/log \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs1_27318/data/db \ &

# 分片副本集2
mkdir -p ./mongodb/sharded_cluster/shard_rs2_27418/log \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs2_27418/data/db \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs2_27518/log \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs2_27518/data/db \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs2_27618/log \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs2_27618/data/db \ &

# 配置
mkdir -p ./mongodb/sharded_cluster/config_rs_27119/log \ &
mkdir -p ./mongodb/sharded_cluster/config_rs_27119/data/db \ &
mkdir -p ./mongodb/sharded_cluster/config_rs_27219/log \ &
mkdir -p ./mongodb/sharded_cluster/config_rs_27219/data/db \ &
mkdir -p ./mongodb/sharded_cluster/config_rs_27319/log \ &
mkdir -p ./mongodb/sharded_cluster/config_rs_27319/data/db \ &

分片副本集1

vim ./mongodb/sharded_cluster/shard_rs1_27118/mongod.yaml

配置样例

systemLog:
  #MongoDB发送所有日志输出的目标指定为文件
  destination: file #mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径
  path: "/mongodb/sharded_cluster/myshardrs01_27118/log/mongod.log"
  #当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾。
  logAppend: true
storage:
  #mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。
  dbPath: "/mongodb/sharded_cluster/myshardrs01_27118/data/db"
  journal:
    #启用或禁用持久性日志以确保数据文件保持有效和可恢复。
    enabled: true
processManagement:
  #启用在后台运行mongos或mongod进程的守护进程模式。
  fork: true
  #指定用于保存mongos或mongod进程的进程ID的文件位置,其中mongos或mongod将写入其PID
  pidFilePath: "/mongodb/sharded_cluster/myshardrs01_27118/log/mongod.pid"
net:
  #服务实例绑定所有IP
  #bindIpAll: true
  #服务实例绑定的IP
  bindIp: localhost,192.168.0.2
  #绑定的端口
  port: 27118
replication:
  replSetName: myshardrs01
sharding:
  clusterRole: shardsvr

1

创建集群内部认证文件

openssl rand -base64 90 -out /usr/local/mongodb/mongo.keyfile
chmod 400 /usr/local/mongodb/mongo.keyfile
ll /usr/local/mongodb/mongo.keyfile

分片副本集1

创建文件夹

mkdir -p ./mongodb/sharded_cluster/shard_rs1_27118/log \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs1_27118/data/db \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs1_27218/log \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs1_27218/data/db \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs1_27318/log \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs1_27318/data/db

配置认证文件

echo '/usr/local/mongodb/sharded_cluster/shard_rs1_27118/mongo.keyfile 
/usr/local/mongodb/sharded_cluster/shard_rs1_27218/mongo.keyfile 
/usr/local/mongodb/sharded_cluster/shard_rs1_27318/mongo.keyfile' | xargs -n 1 cp -v /usr/local/mongodb/mongo.keyfile

创建yaml配置文件,shard_rs1_27118

vim ./mongodb/sharded_cluster/shard_rs1_27118/mongod.yaml
security:
  keyFile: "/usr/local/mongodb/sharded_cluster/shard_rs1_27118/mongo.keyfile"
  authorization: enabled
systemLog:
  destination: file
  path: "/usr/local/mongodb/sharded_cluster/shard_rs1_27118/log/mongod.log"
  logAppend: true
storage:
  dbPath: "/usr/local/mongodb/sharded_cluster/shard_rs1_27118/data/db"
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: "/usr/local/mongodb/sharded_cluster/shard_rs1_27118/log/mongod.pid"
net:
  bindIp: localhost, 192.168.0.4
  port: 27118
replication:
  replSetName: shard_rs1
sharding:
  clusterRole: shardsvr

创建yaml配置文件,shard_rs1_27218

vim ./mongodb/sharded_cluster/shard_rs1_27218/mongod.yaml
security:
  keyFile: "/usr/local/mongodb/sharded_cluster/shard_rs1_27218/mongo.keyfile"
  authorization: enabled
systemLog:
  destination: file
  path: "/usr/local/mongodb/sharded_cluster/shard_rs1_27218/log/mongod.log"
  logAppend: true
storage:
  dbPath: "/usr/local/mongodb/sharded_cluster/shard_rs1_27218/data/db"
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: "/usr/local/mongodb/sharded_cluster/shard_rs1_27218/log/mongod.pid"
net:
  bindIp: localhost, 192.168.0.4
  port: 27218
replication:
  replSetName: shard_rs1
sharding:
  clusterRole: shardsvr

创建yaml配置文件,shard_rs1_27318

vim ./mongodb/sharded_cluster/shard_rs1_27318/mongod.yaml
security:
  keyFile: "/usr/local/mongodb/sharded_cluster/shard_rs1_27318/mongo.keyfile"
  authorization: enabled
systemLog:
  destination: file
  path: "/usr/local/mongodb/sharded_cluster/shard_rs1_27318/log/mongod.log"
  logAppend: true
storage:
  dbPath: "/usr/local/mongodb/sharded_cluster/shard_rs1_27318/data/db"
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: "/usr/local/mongodb/sharded_cluster/shard_rs1_27318/log/mongod.pid"
net:
  bindIp: localhost, 192.168.0.4
  port: 27318
replication:
  replSetName: shard_rs1
sharding:
  clusterRole: shardsvr

启动mongodb服务

./mongodb/bin/mongod -f ./mongodb/sharded_cluster/shard_rs1_27118/mongod.yaml
./mongodb/bin/mongod -f ./mongodb/sharded_cluster/shard_rs1_27218/mongod.yaml
./mongodb/bin/mongod -f ./mongodb/sharded_cluster/shard_rs1_27318/mongod.yaml
# 查看是否启动
ps -ef |grep mongod

初始化副本集、构建节点联系

./mongodb/bin/mongo --host 192.168.0.4 --port 27118




rs.initiate()
{
        "info2" : "no configuration specified. Using a default configuration for the set",
        "me" : "192.168.0.4:27118",
        "ok" : 1,
        "operationTime" : Timestamp(1663399663, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1663399663, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}




rs.add("192.168.0.4:27218")
{
        "ok" : 1,
        "operationTime" : Timestamp(1663399722, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1663399722, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
rs.addArb("192.168.0.4:27318")
{
        "ok" : 1,
        "operationTime" : Timestamp(1663399739, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1663399739, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}



rs.status() # 查看副本集状态
rs.conf() # 查看副本集配置


./mongodb/bin/mongo --host 192.168.0.4 --port 27218
rs.slaveOk()
./mongodb/bin/mongo --host 192.168.0.4 --port 27318
rs.slaveOk()

分片副本集2

创建文件夹

mkdir -p ./mongodb/sharded_cluster/shard_rs2_27418/log \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs2_27418/data/db \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs2_27518/log \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs2_27518/data/db \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs2_27618/log \ &
mkdir -p ./mongodb/sharded_cluster/shard_rs2_27618/data/db \ &

配置认证文件

echo '/usr/local/mongodb/sharded_cluster/shard_rs2_27418/mongo.keyfile 
/usr/local/mongodb/sharded_cluster/shard_rs2_27518/mongo.keyfile 
/usr/local/mongodb/sharded_cluster/shard_rs2_27618/mongo.keyfile' | xargs -n 1 cp -v /usr/local/mongodb/mongo.keyfile

创建yaml配置文件,shard_rs1_27418

vim ./mongodb/sharded_cluster/shard_rs2_27418/mongod.yaml
security:
  keyFile: "/usr/local/mongodb/sharded_cluster/shard_rs2_27418/mongo.keyfile"
  authorization: enabled
systemLog:
  destination: file
  path: "/usr/local/mongodb/sharded_cluster/shard_rs2_27418/log/mongod.log"
  logAppend: true
storage:
  dbPath: "/usr/local/mongodb/sharded_cluster/shard_rs2_27418/data/db"
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: "/usr/local/mongodb/sharded_cluster/shard_rs2_27418/log/mongod.pid"
net:
  bindIp: localhost, 192.168.0.4
  port: 27418
replication:
  replSetName: shard_rs2
sharding:
  clusterRole: shardsvr

创建yaml配置文件,shard_rs2_27518

vim ./mongodb/sharded_cluster/shard_rs2_27518/mongod.yaml
security:
  keyFile: "/usr/local/mongodb/sharded_cluster/shard_rs2_27518/mongo.keyfile"
  authorization: enabled
systemLog:
  destination: file
  path: "/usr/local/mongodb/sharded_cluster/shard_rs2_27518/log/mongod.log"
  logAppend: true
storage:
  dbPath: "/usr/local/mongodb/sharded_cluster/shard_rs2_27518/data/db"
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: "/usr/local/mongodb/sharded_cluster/shard_rs2_27518/log/mongod.pid"
net:
  bindIp: localhost, 192.168.0.4
  port: 27518
replication:
  replSetName: shard_rs2
sharding:
  clusterRole: shardsvr

创建yaml配置文件,shard_rs2_27618

vim ./mongodb/sharded_cluster/shard_rs2_27618/mongod.yaml
security:
  keyFile: "/usr/local/mongodb/sharded_cluster/shard_rs2_27618/mongo.keyfile"
  authorization: enabled
systemLog:
  destination: file
  path: "/usr/local/mongodb/sharded_cluster/shard_rs2_27618/log/mongod.log"
  logAppend: true
storage:
  dbPath: "/usr/local/mongodb/sharded_cluster/shard_rs2_27618/data/db"
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: "/usr/local/mongodb/sharded_cluster/shard_rs2_27618/log/mongod.pid"
net:
  bindIp: localhost, 192.168.0.4
  port: 27618
replication:
  replSetName: shard_rs2
sharding:
  clusterRole: shardsvr

启动mongodb服务

./mongodb/bin/mongod -f ./mongodb/sharded_cluster/shard_rs2_27418/mongod.yaml
./mongodb/bin/mongod -f ./mongodb/sharded_cluster/shard_rs2_27518/mongod.yaml
./mongodb/bin/mongod -f ./mongodb/sharded_cluster/shard_rs2_27618/mongod.yaml
# 查看是否启动
ps -ef | grep mongod

初始化副本集、构建节点联系

./mongodb/bin/mongo --host 192.168.0.4 --port 27418
rs.initiate()
rs.add("192.168.0.4:27518")
rs.addArb("192.168.0.4:27618")

rs.status() # 查看副本集状态
rs.conf() # 查看副本集配置

./mongodb/bin/mongo --host 192.168.0.4 --port 27518
rs.slaveOk()
./mongodb/bin/mongo --host 192.168.0.4 --port 27618
rs.slaveOk()

配置副本集

创建文件夹

mkdir -p ./mongodb/sharded_cluster/config_rs_27119/log \ &
mkdir -p ./mongodb/sharded_cluster/config_rs_27119/data/db \ &
mkdir -p ./mongodb/sharded_cluster/config_rs_27219/log \ &
mkdir -p ./mongodb/sharded_cluster/config_rs_27219/data/db \ &
mkdir -p ./mongodb/sharded_cluster/config_rs_27319/log \ &
mkdir -p ./mongodb/sharded_cluster/config_rs_27319/data/db \ &

配置认证文件

echo '/usr/local/mongodb/sharded_cluster/config_rs_27119/mongo.keyfile
/usr/local/mongodb/sharded_cluster/config_rs_27219/mongo.keyfile
/usr/local/mongodb/sharded_cluster/config_rs_27319/mongo.keyfile' | xargs -n 1 cp -v /usr/local/mongodb/mongo.keyfile

创建yaml配置文件,config_rs_27119

vim ./mongodb/sharded_cluster/config_rs_27119/mongod.yaml
security:
  keyFile: "/usr/local/mongodb/sharded_cluster/config_rs_27119/mongo.keyfile"
  authorization: enabled
systemLog:
  destination: file
  path: "/usr/local/mongodb/sharded_cluster/config_rs_27119/log/mongod.log"
  logAppend: true
storage:
  dbPath: "/usr/local/mongodb/sharded_cluster/config_rs_27119/data/db"
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: "/usr/local/mongodb/sharded_cluster/config_rs_27119/log/mongod.pid"
net:
  bindIp: localhost, 192.168.0.4
  port: 27119
replication:
  replSetName: config_rs
sharding:
  clusterRole: configsvr

创建yaml配置文件,config_rs_27219

vim ./mongodb/sharded_cluster/config_rs_27219/mongod.yaml
security:
  keyFile: "/usr/local/mongodb/sharded_cluster/config_rs_27219/mongo.keyfile"
  authorization: enabled
systemLog:
  destination: file
  path: "/usr/local/mongodb/sharded_cluster/config_rs_27219/log/mongod.log"
  logAppend: true
storage:
  dbPath: "/usr/local/mongodb/sharded_cluster/config_rs_27219/data/db"
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: "/usr/local/mongodb/sharded_cluster/config_rs_27219/log/mongod.pid"
net:
  bindIp: localhost, 192.168.0.4
  port: 27219
replication:
  replSetName: config_rs
sharding:
  clusterRole: configsvr

创建yaml配置文件,config_rs_27319

vim ./mongodb/sharded_cluster/config_rs_27319/mongod.yaml
security:
  keyFile: "/usr/local/mongodb/sharded_cluster/config_rs_27319/mongo.keyfile"
  authorization: enabled
systemLog:
  destination: file
  path: "/usr/local/mongodb/sharded_cluster/config_rs_27319/log/mongod.log"
  logAppend: true
storage:
  dbPath: "/usr/local/mongodb/sharded_cluster/config_rs_27319/data/db"
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: "/usr/local/mongodb/sharded_cluster/config_rs_27319/log/mongod.pid"
net:
  bindIp: localhost, 192.168.0.4
  port: 27319
replication:
  replSetName: config_rs
sharding:
  clusterRole: configsvr

启动mongodb服务

./mongodb/bin/mongod -f ./mongodb/sharded_cluster/config_rs_27119/mongod.yaml
./mongodb/bin/mongod -f ./mongodb/sharded_cluster/config_rs_27219/mongod.yaml
./mongodb/bin/mongod -f ./mongodb/sharded_cluster/config_rs_27319/mongod.yaml
# 查看是否启动
ps -ef | grep mongod

初始化副本集、构建节点联系

./mongodb/bin/mongo --host 192.168.0.4 --port 27119
rs.initiate()
rs.add("192.168.0.4:27219")
rs.addArb("192.168.0.4:27319")

rs.status() # 查看副本集状态
rs.conf() # 查看副本集配置

./mongodb/bin/mongo --host 192.168.0.4 --port 27219
rs.slaveOk()
./mongodb/bin/mongo --host 192.168.0.4 --port 27319
rs.slaveOk()

路由结点1

mkdir -p ./mongodb/sharded_cluster/mongos_27117/log

配置认证文件

cp /usr/local/mongodb/mongo.keyfile /usr/local/mongodb/sharded_cluster/mongos_27117/mongo.keyfile
vim ./mongodb/sharded_cluster/mongos_27117/mongos.yaml
security:
  keyFile: "/usr/local/mongodb/sharded_cluster/mongos_27117/mongo.keyfile"
  # authorization: enabled # mongos配置中无该配置
systemLog:
  destination: file
  path: "/usr/local/mongodb/sharded_cluster/mongos_27117/log/mongod.log"
  logAppend: true
processManagement:
  fork: true
  pidFilePath: "/usr/local/mongodb/sharded_cluster/mongos_27117/log/mongod.pid"
net:
  bindIp: localhost, 192.168.0.4
  port: 27117
sharding:
  configDB: config_rs/192.168.0.4:27119,192.168.0.4:27219,192.168.0.4:27319
  # 注意上述多个IP:PORT别有空格
./mongodb/bin/mongos -f ./mongodb/sharded_cluster/mongos_27117/mongos.yaml
./mongodb/bin/mongo --host 192.168.0.4 --port 27117
sh.addShard("shard_rs1/192.168.0.4:27118,192.168.0.4:27218,192.168.0.4:27318")
sh.status()
sh.addShard("shard_rs2/192.168.0.4:27418,192.168.0.4:27518,192.168.0.4:27618")
sh.status()

路由结点2

mkdir -p ./mongodb/sharded_cluster/mongos_27217/log

配置认证文件

cp /usr/local/mongodb/mongo.keyfile /usr/local/mongodb/sharded_cluster/mongos_27217/mongo.keyfile
vim ./mongodb/sharded_cluster/mongos_27217/mongos.yaml
security:
  keyFile: "/usr/local/mongodb/sharded_cluster/mongos_27217/mongo.keyfile"
  # authorization: enabled # mongos配置中无该配置
systemLog:
  destination: file
  path: "/usr/local/mongodb/sharded_cluster/mongos_27217/log/mongod.log"
  logAppend: true
processManagement:
  fork: true
  pidFilePath: "/usr/local/mongodb/sharded_cluster/mongos_27217/log/mongod.pid"
net:
  bindIp: localhost, 192.168.0.4
  port: 27217
sharding:
  configDB: config_rs/192.168.0.4:27119,192.168.0.4:27219,192.168.0.4:27319
  # 注意上述多个IP:PORT别有空格
./mongodb/bin/mongos -f ./mongodb/sharded_cluster/mongos_27217/mongos.yaml
./mongodb/bin/mongo --host 192.168.0.4 --port 27217
sh.status()
// 第二个路由节点会自动从配置副本集同步配置,无需再次添加分片配置

启用分片

对某个库启用分片并设置某个集合的分片键及其分片规则

# 库分片
sh.enableSharding("articledb")
sh.enableSharding("库名")
sh.status()
# 集合分片
sh.shardCollection(namespace, key, unique)
# 分片键策略
# 哈希策略
sh.shardCollection("articledb.comment",{"nickname":"hashed"})
# 范围策略
sh.shardCollection("articledb.author",{"age":1})

基于范围的分片方式与基于哈希的分片方式性能对比: 基于范围的分片方式提供了更高效的范围查询,给定一个片键的范围,分发路由可以很简单地确定哪个数 据块存储了请求需要的数据,并将请求转发到相应的分片中. 不过,基于范围的分片会导致数据在不同分片上的不均衡,有时候,带来的消极作用会大于查询性能的积极 作用.比如,如果片键所在的字段是线性增长的,一定时间内的所有请求都会落到某个固定的数据块中,最终 导致分布在同一个分片中.在这种情况下,一小部分分片承载了集群大部分的数据,系统并不能很好地进行 扩展. 与此相比,基于哈希的分片方式以范围查询性能的损失为代价,保证了集群中数据的均衡.哈希值的随机性 使数据随机分布在每个数据块中,因此也随机分布在不同分片中.但是也正由于随机性,一个范围查询很难 确定应该请求哪些分片,通常为了返回需要的结果,需要请求所有分片. 如无特殊情况,一般推荐使用 Hash Sharding。 而使用 _id 作为片键是一个不错的选择,因为它是必有的,你可以使用数据文档 _id 的哈希作为片键。 这个方案能够是的读和写都能够平均分布,并且它能够保证每个文档都有不同的片键所以数据块能够很 精细。 似乎还是不够完美,因为这样的话对多个文档的查询必将命中所有的分片。虽说如此,这也是一种比较 好的方案了。

# 显示集群的详细信息:
db.printShardingStatus()

# 查看均衡器是否工作(需要重新均衡时系统才会自动启动,不用管它):
sh.isBalancerRunning()

# 查看当前Balancer状态
sh.getBalancerState()

提示:js的语法,因为mongo的shell是一个JavaScript的shell。

关闭集群

#!/usr/bash
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/sharded_cluster/shard_rs1_27118/mongod.yaml --shutdown &
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/sharded_cluster/shard_rs1_27218/mongod.yaml --shutdown &
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/sharded_cluster/shard_rs1_27318/mongod.yaml --shutdown &
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/sharded_cluster/shard_rs2_27418/mongod.yaml --shutdown &
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/sharded_cluster/shard_rs2_27518/mongod.yaml --shutdown &
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/sharded_cluster/shard_rs2_27618/mongod.yaml --shutdown &
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/sharded_cluster/config_rs_27119/mongod.yaml --shutdown &
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/sharded_cluster/config_rs_27219/mongod.yaml --shutdown &
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/sharded_cluster/config_rs_27319/mongod.yaml --shutdown &
/usr/local/mongodb/bin/mongo localhost:27117 --eval 'db.adminCommand({ "shutdown" : 1 })' &
/usr/local/mongodb/bin/mongo localhost:27217 --eval 'db.adminCommand({ "shutdown" : 1 })' &
echo 'Done'

启动集群

#!/usr/bash
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/sharded_cluster/shard_rs1_27118/mongod.yaml &
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/sharded_cluster/shard_rs1_27218/mongod.yaml &
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/sharded_cluster/shard_rs1_27318/mongod.yaml &
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/sharded_cluster/shard_rs2_27418/mongod.yaml &
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/sharded_cluster/shard_rs2_27518/mongod.yaml &
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/sharded_cluster/shard_rs2_27618/mongod.yaml &
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/sharded_cluster/config_rs_27119/mongod.yaml &
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/sharded_cluster/config_rs_27219/mongod.yaml &
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/sharded_cluster/config_rs_27319/mongod.yaml &
/usr/local/mongodb/bin/mongos -f /usr/local/mongodb/sharded_cluster/mongos_27117/mongos.yaml &
/usr/local/mongodb/bin/mongos -f /usr/local/mongodb/sharded_cluster/mongos_27217/mongos.yaml &
echo 'Done'
use admin
db.createUser(
  {
    user: "admin",
    pwd: passwordPrompt(), // or cleartext password
    roles: [
      { role: "userAdminAnyDatabase", db: "admin" },
      { role: "readWriteAnyDatabase", db: "admin" }
    ]
  }
)

db.createUser(
  {
    user: "admin",
    pwd: "mongd", // or cleartext password
    roles: [
      { role: "userAdminAnyDatabase", db: "admin" },
      { role: "readWriteAnyDatabase", db: "admin" }
    ]
  }
)