MongoDB学习笔记三:MongoDB集群环境搭建
标签(空格分隔): MongoDB
一、集群搭建简介
为什么使用集群架构?
主从:故障转移:无法实现,如果主机宕机,需要关闭slave并且按照master模式启动。无法解决单点故障 无法autofailover 不可以自动主从的切换
为了解决主从的问题,MongoDB3.0之后出现副本集,副本集解决了故障转移的问题,但是一个副本集中的数据是相同的,无法做到海量数据的存储。所以就需要一个架构去解决这个问题。也就是分片式集群。
一个健壮的简单的MongoDB集群的搭建需要十个服务进程(分开搭建需要十台服务器),这里在一台虚拟机上进行搭建。如下图
Router:路由。作用就是转发客户端的请求。
Config Server:配置服务器。用于记录Shard和分片的详细情况。
Shard:分片服务器。真正存储数据的地方。搭建副本集。
分片的两种模式:Range Based Sharding 和 Hash Based Sharding
两者的特点和不同点,参考官方文档。
- Range Based Sharding
- Hash Based Sharding
二、搭建过程
搭建副本集:Shard1【名称:motui】
启动服务./bin/mongod --dbpath /root/mongodb/shard1/rep1/ --port 27017 --journal --replSet motui ./bin/mongod --dbpath /root/mongodb/shard1/rep2/ --port 27018 --journal --replSet motui ./bin/mongod --dbpath /root/mongodb/shard1/rep3/ --port 27019 --journal --replSet motui
链接任意一台mongo :
./bin/mongo --port 27018 use admin var config = { _id:"motui", members:[ {_id:0,host:"192.168.0.167:27019"}, {_id:1,host:"192.168.0.167:27018"}, {_id:2,host:"192.168.0.167:27017"}] } rs.initiate(config);//初始化副本集 rs.slaveOk() //访问从机需要先执行 查看主机:在master执行rs.isMaster(); 查看副本集状态:rs.status(); 添加数据创建库
搭建副本集:Shard2【名称:motui1】
启动服务
./bin/mongod --dbpath /root/mongodb/shard2/rep1/ --port 27021 --journal --replSet motui1 ./bin/mongod --dbpath /root/mongodb/shard2/rep2/ --port 27022 --journal --replSet motui1 ./bin/mongod --dbpath /root/mongodb/shard2/rep3/ --port 27023 --journal --replSet motui1
链接任意一台mongo :
./bin/mongo --port 27021 use admin var config = { _id:"motui1", members:[ {_id:0,host:"192.168.0.167:27023"}, {_id:1,host:"192.168.0.167:27022"}, {_id:2,host:"192.168.0.167:27021"}] } rs.initiate(config);//初始化副本集 rs.slaveOk(); //访问从机需要先执行 查看主机:在master执行rs.isMaster(); 查看副本集状态:rs.status(); 添加数据创建库
启动副本集motui 和 motui1
./bin/mongod --dbpath /root/mongodb/shard1/rep1/ --port 27017 --journal --replSet motui --shardsvr ./bin/mongod --dbpath /root/mongodb/shard1/rep2/ --port 27018 --journal --replSet motui --shardsvr ./bin/mongod --dbpath /root/mongodb/shard1/rep3/ --port 27019 --journal --replSet motui --shardsvr ./bin/mongod --dbpath /root/mongodb/shard2/rep1/ --port 27021 --journal --replSet motui1 --shardsvr ./bin/mongod --dbpath /root/mongodb/shard2/rep2/ --port 27022 --journal --replSet motui1 --shardsvr ./bin/mongod --dbpath /root/mongodb/shard2/rep3/ --port 27023 --journal --replSet motui1 --shardsvr
启动配置服务器
./bin/mongod --dbpath /root/mongodb/conf/ --port 27025 --journal --configsvr ./bin/mongos --port 8000 --configdb 192.168.0.167:27025 --chunkSize 1 连接多台配置服务器命令: ./bin/mongos --port 8000 --configdb 192.168.111.129:27025,192.168.111.129:27026,192.168.111.129:27027 --chunkSize 1
连接路由器服务器
./bin/mongo --port 8000 use admin db.runCommand({addshard:"motui/192.168.0.167:27017,192.168.0.167:27018,192.168.0.167:27019",name:"Basic Shard Server1"}) db.runCommand({addshard:"motui1/192.168.0.167:27021,192.168.0.167:27022,192.168.0.167:27023",name:"Basic Shard Server2"})
对testUser数据库分片
sh.enableSharding("testUser");
对ID进行分片
使用hash based方式sh.shardCollection("testUser.t_user", {"_id": "hashed" }); sh.shardCollection("testUser.t_order", {"_id": "hashed" });
数据测试
use testUser for(var i=0; i<10000; i++){ db.t_user.insert({name:"user"+i, age:i, email:"[email protected]" }) } for(var i=0; i<10000; i++){ db.t_order.insert({name:"user"+i, age:i, email:"[email protected]" }) } 查看状态:db.t_user.stats(); db.t_order.stats();
附录:主从复制
启动服务
./bin/mongod --dbpath /root/mongodb/shard1/master --port 27017 --master --journal
./bin/mongod --dbpath /root/mongodb/shard1/slave1/ --port 27018 --slave --journal --source 192.168.0.167:27017
./bin/mongod --dbpath /root/mongodb/shard1/slave2/ --port 27018 --slave --journal --source 192.168.0.167:27017 --slavedelay 120
连接客户端查看状态
./bin/mongo --port 27017 --host 192.168.0.167
rs.slaveOk(); 查看从机
rs.help(); 查看集群状态
命令:
rs.status() { replSetGetStatus : 1 } checks repl set status
rs.initiate() { replSetInitiate : null } initiates set with default settings
rs.initiate(cfg) { replSetInitiate : cfg } initiates set with configuration cfg
rs.conf() get the current configuration object from local.system.replset
rs.reconfig(cfg) updates the configuration of a running replica set with cfg (disconnects)
rs.add(hostportstr) add a new member to the set with default attributes (disconnects)
rs.add(membercfgobj) add a new member to the set with extra attributes (disconnects)
rs.addArb(hostportstr) add a new member which is arbiterOnly:true (disconnects)
rs.stepDown([stepdownSecs, catchUpSecs]) step down as primary (disconnects)
rs.syncFrom(hostportstr) make a secondary sync from the given member
rs.freeze(secs) make a node ineligible to become primary for the time specified
rs.remove(hostportstr) remove a host from the replica set (disconnects)
rs.slaveOk() allow queries on secondary nodes
rs.printReplicationInfo() check oplog size and time range
rs.printSlaveReplicationInfo() check replica set members and replication lag
db.isMaster() check who is primary