服务器规划
三台服务器: xxx.xxx.xxx.234xxx.xxx.xxx.235xxx.xxx.xxx.236
服务器 | 234 | 235 | 235 |
---|---|---|---|
组件 1 | mongos | mongos | mongos |
组件 2 | config server | config server | config server |
组件 3 | shard server1 主 | shard server1 副 | shard server1 仲裁 |
组件 4 | shard server2 仲裁 | shard server2 主 | shard server2 副 |
组件 5 | shard server3 副 | shard server3 仲裁 | shard server3 主 |
三台机器的端口配置都采用如下方式:
- mongos:20000
- config:21000
- shard1:27001
- shard2:27002
- shard3:27003
安装 mongodb
解压, 重命名
- # 解压
- tar -xzvf mongodb-linux-x86_64-3.6.3.tgz -C /root/mongo
- # 改名
- mv mongodb-linux-x86_64-3.6.3 mongodb
配置环境变量 vim /etc/profile
- # /etc/profile 添加内容
- export MONGODB_HOME=/root/mongo/mongodb
- export PATH=$MONGODB_HOME/bin:$PATH
- # 使立即生效
- source /etc/profile
分别在每台机器建立 confmongosconfigshard1shard2shard3 六个目录, 因为 mongos 不存储数据, 只需要建立日志文件目录即可
- mkdir -p /root/mongo/data/conf
- mkdir -p /root/mongo/ data /mongos/log
- mkdir -p /root/mongo/ data /config/data
- mkdir -p /root/mongo/ data /config/log
- mkdir -p /root/mongo/ data /shard1/data
- mkdir -p /root/mongo/ data /shard1/log
- mkdir -p /root/mongo/ data /shard2/data
- mkdir -p /root/mongo/ data /shard2/log
- mkdir -p /root/mongo/ data /shard3/data
- mkdir -p /root/mongo/ data /shard3/log
- config server
mongodb3.4 以后要求配置服务器也创建副本集, 不然集群搭建不成功
添加配置文件
- pidfilepath = /root/mongo/data/config/log/configsrv.pid
- dbpath = /root/mongo/data/config/data
- logpath = /root/mongo/data/config/log/congigsrv.log
- logappend = true
- bind_ip = 0.0.0.0
- port = 21000
- fork = true
- #declare this is a config db of a cluster;
- configsvr = true
- # 副本集名称
- replSet=configs
- # 设置最大连接数
- maxConns=20000
分别启动三台服务器的 config server
mongod -f /usr/local/mongodb/conf/config.conf
登录任意一台配置服务器, 初始化配置副本集
- # 连接 mongo--port 21000#config 变量 config = {..._id: "configs",
- ...members: [... {
- _id: 0,
- host: "xxx.xxx.xxx.234:21000"
- },
- ... {
- _id: 1,
- host: "xxx.xxx.xxx.235:21000"
- },
- ... {
- _id: 2,
- host: "xxx.xxx.xxx.236:21000"
- }...]...
- }# 初始化副本集 rs.initiate(config)
其中,_id : configs 应与配置文件中配置的 replicaction.replSetName 一致, members 中的 host 为三个节点的 ip 和 port
shard 分片副本集
第 1 个分片副本集
在 / root/mongo/data/conf 中新增配置文件
- vi shard1.conf
- pidfilepath = /root/mongo/data/shard1/log/shard1.pid
- dbpath = /root/mongo/data/shard1/data
- logpath = /root/mongo/data/shard1/log/shard1.log
- logappend = true
- bind_ip = 0.0.0.0
- port = 27001
- fork = true
- directoryperdb = true
- # 副本集名称
- replSet=shard1
- #declare this is a shard db of a cluster;
- shardsvr = true
- # 设置最大连接数
- maxConns=20000
启动三台服务器的 shard1 server
mongod -f /usr/local/mongodb/conf/shard1.conf
登陆任意一台服务器 (非仲裁节点), 初始化副本集
- mongo --port 27001
- # 使用 admin 数据库
- use admin
- # 定义副本集配置, 第三个节点的 "arbiterOnly":true 代表其为仲裁节点
- config = {
- ... _id : "shard1",
- ... members : [
- ... {_id : 0, host : "xxx.xxx.xxx.234:27001" },
- ... {_id : 1, host : "xxx.xxx.xxx.235:27001" },
- ... {_id : 2, host : "xxx.xxx.xxx.236:27001 , arbiterOnly: true }
- ... ]
- ... }
- # 初始化副本集配置
- rs.initiate(config);
第 2 个分片副本集
在 / root/mongo/data/conf 中新增配置文件
- vi shard2.conf
- pidfilepath = /root/mongo/data/shard2/log/shard2.pid
- dbpath = /root/mongo/data/shard2/data
- logpath = /root/mongo/data/shard2/log/shard2.log
- logappend = true
- bind_ip = 0.0.0.0
- port = 27002
- fork = true
- directoryperdb = true
- # 副本集名称
- replSet=shard2
- #declare this is a shard db of a cluster;
- shardsvr = true
- # 设置最大连接数
- maxConns=20000
分别启动三台服务器的 shard2 server
mongod -f /usr/local/mongodb/conf/shard2.conf
登陆任意一台服务器 (非仲裁节点), 初始化副本集
- mongo--port 27002# 使用 admin 数据库 use admin# 定义副本集配置 config = {..._id: "shard2",
- ...members: [... {
- _id: 0,
- host: "xxx.xxx.xxx.234:27002",
- arbiterOnly: true
- },
- ... {
- _id: 1,
- host: "xxx.xxx.xxx.235:27002"
- },
- ... {
- _id: 2,
- host: "xxx.xxx.xxx.236:27002"
- }...]...
- }# 初始化副本集配置 rs.initiate(config);
第 3 个分片副本集
在 / root/mongo/data/conf 中新增配置文件
- vi shard3.conf
- pidfilepath = /root/mongo/data/shard3/log/shard3.pid
- dbpath = /root/mongo/data/shard3/data
- logpath = /root/mongo/data/shard3/log/shard3.log
- logappend = true
- bind_ip = 0.0.0.0
- port = 27003
- fork = true
- directoryperdb = true
- # 副本集名称
- replSet=shard3
- #declare this is a shard db of a cluster;
- shardsvr = true
- # 设置最大连接数
- maxConns=20000
启动三台服务器的 shard3 server
mongod -f /usr/local/mongodb/conf/shard3.conf
登陆任意一台服务器 (非仲裁节点), 初始化副本集
- mongo--port 27003# 使用 admin 数据库 use admin# 定义副本集配置 config = {..._id: "shard3",
- ...members: [... {
- _id: 0,
- host: "xxx.xxx.xxx.234:27003"
- },
- ... {
- _id: 1,
- host: "xxx.xxx.xxx.235:27003",
- arbiterOnly: true
- },
- ... {
- _id: 2,
- host: "xxx.xxx.xxx.236:27003"
- }...]...
- }# 初始化副本集配置 rs.initiate(config);
mongos 路由服务器
先启动配置服务器和分片服务器, 后启动路由实例:(三台机器)
在 / root/mongo/data/conf 中新增配置文件
- vi mongos.conf
- pidfilepath = /root/mongo/data/mongos/log/mongos.pid
- logpath = /root/mongo/data/mongos/log/mongos.log
- logappend = true
- bind_ip = 0.0.0.0
- port = 20000
- fork = true
- # 监听的配置服务器, 只能有 1 个或者 3 个 configs 为配置服务器的副本集名字
- configdb = configs/xxx.xxx.xxx.234:21000,xxx.xxx.xxx.235:21000,xxx.xxx.xxx.236:21000
- # 设置最大连接数
- maxConns=20000
启动三台服务器的 mongos server
mongos -f /usr/local/mongodb/conf/mongos.conf
启用分片与测试
目前搭建了 mongodb 配置服务器路由服务器, 各个分片服务器, 不过应用程序连接到 mongos 路由服务器并不能使用分片机制, 还需要在程序里设置分片配置, 让分片生效
登陆任意一台 mongos
- mongo --port 20000
- # 使用 admin 数据库
- use admin
- # 串联路由服务器与分配副本集
- sh.addShard("shard1/xxx.xxx.xxx.234:27001,xxx.xxx.xxx.235:27001, xxx.xxx.xxx.236:27001")
- sh.addShard("shard2/xxx.xxx.xxx.234:27002,xxx.xxx.xxx.235:27002, xxx.xxx.xxx.236:27002")
- sh.addShard("shard3/xxx.xxx.xxx.234:27003,xxx.xxx.xxx.235:27003, xxx.xxx.xxx.236:27003")
- # 查看集群状态
- sh.status()
目前配置服务路由服务分片服务副本集服务都已经串联起来了, 但我们的目的是希望插入数据, 数据能够自动分片连接在 mongos 上, 准备让指定的数据库指定的集合分片生效
默认的 chunkSize 为 64M, 这里的数据量没有达到 64M, 可以修改一下 chunkSize 的大小为 1M, 方便测试分片效果:
- mongo --port 20000
- mongos> use config
- switched to db config
- mongos> db.settings.save( { _id:"chunksize", value: 1 } )
- WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "chunksize" })
- mongos> db.settings.find();
- { "_id" : "balancer", "stopped" : false, "mode" : "full" }
- { "_id" : "chunksize", "value" : 1 }
指定 testdb 分片生效
- # 指定 testdb 分片生效
- mongos> db.runCommand( { enablesharding :"testdb"});
- # 指定数据库里需要分片的集合和片键
- mongos> db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )
- mongos> use testdb
- switched to db testdb
使用 java 代码插入 10W 调数据测试分片效果
- // 创建客户端连接
- MongoClient mongoClient = new MongoClient( "xxx.xxx.xxx.234" , 20000 );
- MongoDatabase database = mongoClient.getDatabase("testdb");
- MongoCollection<Document> collection = database.getCollection("table1");
- // 插入文档
- for (int i=1;i<100000;i++){
- Document doc = new Document("id", i)
- .append("type", "database")
- .append("test","testval");
- System.out.println(i);
- collection.insertOne(doc);
- }
使用 db.table1.stats(); 命令查看集合分片状态, 部分无关信息省掉, 可以看到数据分到 3 个分片
参考文献
http://www.ityouknow.com/mongodb/2017/08/05/mongodb-cluster-setup.html
来源: http://www.jianshu.com/p/4bc67a39e5c5