以下是 MongoDB 分片副本集群部署脚本方案的一个示例:

  1. 安装 MongoDB

在所有节点上安装 MongoDB。可以使用以下命令在 Ubuntu 上安装:

sudo apt-get install mongodb

或者在 CentOS 上安装:

sudo yum install mongodb
  1. 配置 MongoDB

在主节点上创建一个配置文件 'mongod.conf',内容如下:

systemLog:
  destination: file
  path: /var/log/mongodb/mongod.log
  logAppend: true

storage:
  journal:
    enabled: true
  dbPath: /data/db

replication:
  replSetName: 'rs0'

sharding:
  clusterRole: 'configsvr'

net:
  bindIp: <主节点 IP>
  port: 27017

在备份节点上创建一个配置文件 'mongod.conf',内容如下:

systemLog:
  destination: file
  path: /var/log/mongodb/mongod.log
  logAppend: true

storage:
  journal:
    enabled: true
  dbPath: /data/db

replication:
  replSetName: 'rs0'

net:
  bindIp: <备份节点 IP>
  port: 27017

在路由节点上创建一个配置文件 'mongos.conf',内容如下:

systemLog:
  destination: file
  path: /var/log/mongodb/mongos.log
  logAppend: true

sharding:
  configDB: <主节点 IP>:27017,<备份节点 IP>:27017
  autoSplit: true

net:
  bindIp: <路由节点 IP>
  port: 27017
  1. 启动 MongoDB

在主节点上启动 MongoDB:

mongod --config /path/to/mongod.conf

在备份节点上启动 MongoDB:

mongod --config /path/to/mongod.conf

在路由节点上启动 MongoDB:

mongos --config /path/to/mongos.conf
  1. 配置副本集

在主节点上进入 MongoDB shell:

mongo

切换到 'admin' 数据库:

use admin

初始化副本集:

rs.initiate()

添加备份节点:

rs.add('<备份节点 IP>:27017')

查看副本集状态:

rs.status()
  1. 配置分片集群

在路由节点上进入 MongoDB shell:

mongo

添加分片:

sh.addShard('<主节点 IP>:27017')
sh.addShard('<备份节点 IP>:27017')

创建分片集合:

use test
sh.enableSharding('test')
sh.shardCollection('test.users', {'_id': 'hashed'})
  1. 完成

现在,MongoDB 分片副本集群已经成功部署。可以使用 'mongo' 命令行工具或者 MongoDB 客户端连接到路由节点并开始使用分片集合。

MongoDB 分片副本集群部署脚本方案详解

原文地址: https://www.cveoy.top/t/topic/nDE8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录