MongoDB分片集群搭建

发布于:2024-05-05 ⋅ 阅读:(23) ⋅ 点赞:(0)
MongoDB分片集群搭建

如果不清楚什么是分片集群,可以看我上一篇发布的文章MongoDB分片集群详解

在这里插入图片描述

环境信息

以下是我在测试环境中虚拟机的配置,如果你的虚拟机ip不同可以对应的更改,但是要确保这三台虚拟机之间可以ping通

#操作系统:centos6.8
mongodb版本:mongodb-linux-x86_64-rhe162-4.0.6.tgz
#3台虚拟机
192.168.57.201、192.168.57.202、192.168.57.203
#2个分片复制集
分片集群1:(192.168.57.201:27017、192.168.57.202:27017、192.168.57.203:27017)
分片集群2:(192.168.57.201:27018、192.168.57.202:27018、192.168.57.203:27018)
#1个config复制集
config复制集:(192.168.57.201:28018、192.168.57.202:28018、192.168.57.203:28018)
1个mongos节点

config搭建复制集是为了保证config节点的高可用性,避免config挂了,mongos找不到数据存储的地方

搭建mongodb分片复制集

⚠️:如果配置中没有的的文件夹,需要手动创建,例如下面的/opt/mongo/data/db文件夹

  • shard1集群步骤

    • 添加复制集配置文件:mongo.conf,三台虚拟机都要加

      # 添加复制集配置文件
      fork=true
      dbpath=/opt/mongo/data/db
      port=27017
      bind_ip=0.0.0.0
      logpath=/opt/mongo/logs/mongodb.log
      logappend=true
      # 副本集名称
      replSet=goat_repl
      smallfiles=true
      # 分片集群必须要有的属性
      shardsvr=true
      
    • 添加复制集配置文件: mongo2.conf,同样三台虚拟机都要加

      # 添加复制集配置文件
      fork=true
      dbpath=/opt/mongo/data/db
      # 注意这里的port要改为27018
      port=27018
      bind_ip=0.0.0.0
      logpath=/opt/mongo/logs/mongodb.log
      logappend=true
      # 副本集2的名称要与副本集1名称不同
      replSet=goat_repl2
      smallfiles=true
      # 分片集群必须要有的属性
      shardsvr=true
      
    • 去到mongod所在目录下,启动两个副本集

      # mongo.conf替换为自己的配置文件路径
      ./mongod -f mongo.conf
      ./mongod -f mongo2.conf
      
    • 登陆副本集,添加初始化配置

      # 进入mongo客户端
      # 配置goat_repl副本集
      ./mongo -port 27017
      
      
      # 配置goat_repl2副本集
      ./mongo -port 27018
      
    • 进入shell后,执行初始化命令

      27017配置

      var rsconf = {
      	_id:'goat_repl', // 这里的id要与配置文件中指定的服务所属复制集相同
      	members: // 复制集成员
      	[
      		{
      			_id:1, // 成员id
      			host: '192.168.57.201:27017'
      		},
      		{
      			_id:2, // 成员id
      			host: '192.168.57.202:27017'
      		},
      		{
      			_id:3, // 成员id
      			host: '192.168.57.203:27017'
      		}
      	]
      }
      # 初始化配置
      rs.initiate(rsconf)
      

      27018配置

      var rsconf = {
      	_id:'goat_repl2', // 这里的id要与配置文件中指定的服务所属复制集相同
      	members: // 复制集成员
      	[
      		{
      			_id:1, // 成员id
      			host: '192.168.57.201:27018'
      		},
      		{
      			_id:2, // 成员id
      			host: '192.168.57.202:27018'
      		},
      		{
      			_id:3, // 成员id
      			host: '192.168.57.203:27018'
      		}
      	]
      }
      # 初始化配置
      rs.initiate(rsconf)
      # 状态查看
      rs.status()
      
配置mongodb分片配置集
  • 搭建config节点复制集

    • 创建config节点配置文件:mongo-cfg.conf(同样3台虚拟机都要配置)

      systemLog:
      	destination: file
      	path: /opt/mongo/mongo-cfg/logs/mongodb.log
      	logAppend: true
      storage:
      	joural:
      		enable: true
      	# 数据存储位置
      	dbPath: /opt/mongo/mongo-cfg/data
      	# 是否一个库一个文件夹
      	directoryPerDB: true
      	wiredTiger:
      		engineConfig:
      			#最大使用的cache,根据需求调节
      			cacheSizeGB: 1
      			#是否将索引也按照数据库名,单独存储
      			directoryForIndexes: true
      		collectionConfig:
      			#表压缩配置
      			blockCompressor: zlib
      		indexConfig:
      			prefixCompression: true
      net:
      	# ip地址
      	bindIp: 192.168.57.201
      	# 端口
      	port: 28018
      replication:
      	oplogSizeMB: 2048
      	# 配置节点的复制集名称
      	replSetName: configReplSet
      sharding:
      	clusterRole: configsvr
      processManageMent:
      	fork: true
      
      systemLog:
      	destination: file
      	path: /opt/mongo/mongo-cfg/logs/mongodb.log
      	logAppend: true
      storage:
      	joural:
      		enable: true
      	# 数据存储位置
      	dbPath: /opt/mongo/mongo-cfg/data
      	# 是否一个库一个文件夹
      	directoryPerDB: true
      	wiredTiger:
      		engineConfig:
      			#最大使用的cache,根据需求调节
      			cacheSizeGB: 1
      			#是否将索引也按照数据库名,单独存储
      			directoryForIndexes: true
      		collectionConfig:
      			#表压缩配置
      			blockCompressor: zlib
      		indexConfig:
      			prefixCompression: true
      net:
      	# ip地址
      	bindIp: 192.168.57.202
      	# 端口
      	port: 28018
      replication:
      	oplogSizeMB: 2048
      	# 配置节点的复制集名称
      	replSetName: configReplSet
      sharding:
      	clusterRole: configsvr
      processManageMent:
      	fork: true
      
      systemLog:
      	destination: file
      	path: /opt/mongo/mongo-cfg/logs/mongodb.log
      	logAppend: true
      storage:
      	joural:
      		enable: true
      	# 数据存储位置
      	dbPath: /opt/mongo/mongo-cfg/data
      	# 是否一个库一个文件夹
      	directoryPerDB: true
      	wiredTiger:
      		engineConfig:
      			#最大使用的cache,根据需求调节
      			cacheSizeGB: 1
      			#是否将索引也按照数据库名,单独存储
      			directoryForIndexes: true
      		collectionConfig:
      			#表压缩配置
      			blockCompressor: zlib
      		indexConfig:
      			prefixCompression: true
      net:
      	# ip地址
      	bindIp: 192.168.57.203
      	# 端口
      	port: 28018
      replication:
      	oplogSizeMB: 2048
      	# 配置节点的复制集名称
      	replSetName: configReplSet
      sharding:
      	clusterRole: configsvr
      processManageMent:
      	fork: true
      
    • 启动配置复制集(三台)

      ./mongod -f /opt/mongo/mongo-cfg.conf
      
    • 初始化配置节点(三台)

      # 登陆
      ./mongo -host 192.168.57.201 -port 28018
      
    • 初始化命令

      var rsconf = {
      	_id:'configReplSet', // 这里的id要与配置文件中指定的服务所属复制集相同
      	members: // 复制集成员
      	[
      		{
      			_id:1, // 成员id
      			host: '192.168.57.201:28018'
      		},
      		{
      			_id:2, // 成员id
      			host: '192.168.57.202:28018'
      		},
      		{
      			_id:3, // 成员id
      			host: '192.168.57.203:28018'
      		}
      	]
      }
      # 初始化配置
      rs.initiate(rsconf)
      
配置mongos节点
  • mongos配置文件

    systemLog:
    	destination: file
    	path: /opt/mongo/mongos/log/mongos.log
    	logAppend: true
    net:
    	bindIp: 192.168.57.201
    	port: 28017
    sharding:
    	configDB: configReplSet/192.168.57.201:28018,192.168.57.202:28018,192.168.57.201:28018
    processManagement:
    	fork: true
    
  • 启动mongos

    ./mongos -confjg /opt/mongo/mongos/mongos.conf
    
添加集群中的分片节点
  • 切换到admin数据库

    use admin
    
  • 添加shard1复制集

    db.runCommand({
    	addshard: "goat_repl/192.168.57.201:27017,192.168.57.202:27017,192.168.57.203:27017",
    	name: "shard1"
    })
    
  • 添加shard2复制集

    db.runCommand({
    	addshard: "goat_repl2/192.168.57.201:27018,192.168.57.202:27018,192.168.57.203:27018",
    	name: "shard1"
    })
    
  • 查看分片

    # 列出分片
    db.runCommand({listshards: 1})
    # 查看分片集群的状态
    sh.status()
    
测试分片集群
  • 先创建一个数据库,开启分片
db.runCommand({ enablesharding: "testdb" })
  • 创建分片的键
db.runCommand({ shardcollection: "testdb.users", key: {id: 1}})
  • 创建索引(如果不是空集合,不是第一次操作)
use testdb
db.users.ensureIndex({id: 1})
  • 添加测试数据
var arr=[];
for(var i = 0; i < 1500000;i++) {
	var uid = i;
	var name = "name" + i;
	arr.push({"id":uid, "name":name});
}
db.users.insertMany(arr);

其他分片集群的命令

# 删除分片
db.runCommand({removeShard: "shard2"})

分片集群部署的常见错误

  • 分片不会默认生成,需要先在数据库中启动分片(sh.enableSharding(“DBName”)),然后再设置集合分片(sh.shardCollection(“Collection”{片键}))

网站公告

今日签到

点亮在社区的每一天
去签到