mongodb|mongodb分片集群搭建

1. 环境软件版本

环境&软件 版本
虚拟机&VMware Workstation Pro VMwareworkstation15.5.6
服务器&Centos CentOS-7-x86_64-DVD-1503-01.iso
数据库&mongodb mongodb-linux-x86_64-4.1.3
远程连接&Xshell 5
远程文件传输&Xftp 5
2. 环境架构设计
机器名称 IP:port 角色
mongodb 192.168.5.136:27011 mongodb路由节点
mongodb 192.168.5.136:17011 mongodb配置节点1
mongodb 192.168.5.136:17013 mongodb配置节点2
mongodb 192.168.5.136:17015 mongodb配置节点3
mongodb 192.168.5.136:37011 shard1节点1
mongodb 192.168.5.136:37013 shard1节点2
mongodb 192.168.5.136:37015 shard1节点3
mongodb 192.168.5.136:37017 shard1仲裁节点
mongodb 192.168.5.136:47011 shard2节点1
mongodb 192.168.5.136:47013 shard2节点2
mongodb 192.168.5.136:47015 shard2节点3
mongodb 192.168.5.136:47011 shard2仲裁节点
mongodb 192.168.5.136:57011 shard3节点1
mongodb 192.168.5.136:57013 shard3节点2
mongodb 192.168.5.136:57015 shard3节点3
mongodb 192.168.5.136:57017 shard3仲裁节点
mongodb 192.168.5.136:58011 shard4节点1
mongodb 192.168.5.136:58013 shard4节点2
mongodb 192.168.5.136:58015 shard4节点3
mongodb 192.168.5.136:58017 shard4仲裁节点
3.配置节点配置 建立17011配置节点的配置信息,vi config-17011.conf
dbpath=config/config1 # 日志文件位置 logpath=config/logs/config1.log # 以追加方式写入日志 logappend=true # 是否以守护进程方式运行 fork = true bind_ip=0.0.0.0 port = 17011 # 表示是一个配置服务器 configsvr=true # 配置服务器副本集名称 replSet=configsvr #auth=true #keyFile=data/mongodb/testKeyFile.file

以上为17011端口的配置节点配置,17013,17015节点的以此相同
注意:需提前建立dbpath和logpath指定的文件夹,否则启动报错
建立3个节点后进行启动,使用mongodb下bin目录里面的命令启动,例如:./bin/mongod -f config/config-17011.conf,依次启动17011,17013,17015
【mongodb|mongodb分片集群搭建】随意进入其中一个节点,./bin/mongo --port 17011,将3个配置节点添加进集群:
use adminvar cfg ={"_id":"configsvr", "members":[ {"_id":1,"host":"192.168.5.136:17011"}, {"_id":2,"host":"192.168.5.136:17013"}, {"_id":3,"host":"192.168.5.136:17015"}] }; rs.initiate(cfg)

配置节点完成
4.配置数据节点分片shard1 建立节点37011配置文件,vi shard1-37011.conf
dbpath=shard/shard1/shard1-37011 bind_ip=0.0.0.0 port=37011 fork=true logpath=shard/shard1/logs/shard1-37011.log replSet=shard1 shardsvr=true #auth=true #keyFile=data/mongodb/testKeyFile.file

依次建立37013,37015,37017节点的配置文件
启动节点:
./bin/mongod -f shard/shard1/shard1-37011.conf ./bin/mongod -f shard/shard1/shard1-37013.conf ./bin/mongod -f shard/shard1/shard1-37015.conf ./bin/mongod -f shard/shard1/shard1-37017.conf

进入其中一个节点,将节点加入集群:
var cfg ={"_id":"lagouCluster", "protocolVersion" : 1, "members":[ {"_id":1,"host":"192.168.5.136:37011"}, {"_id":2,"host":"192.168.5.136:37013"} , {"_id":2,"host":"192.168.5.136:37015"} , {"_id":2,"host":"192.168.5.136:37013","arbiterOnly":true} ] } rs.initiate(cfg) rs.status()

“arbiterOnly”:true标识为仲裁节点
按照上面步骤,建立shard2,shard3,shard4分片集群
5.建立路由节点 为27017路由节点建立配置文件,vi route-27017.conf
port=27017 bind_ip=0.0.0.0 fork=true logpath=route/logs/route.log configdb=configsvr/192.168.5.136:17011,192.168.5.136:17013,192.168.5.136:17015 #keyFile=data/mongodb/testKeyFile.file

启动路由节点: ./bin/mongos -f route/route-27017.conf
进入路由节点,将数据分片添加进去:
sh.status() sh.addShard("shard1/192.168.5.136:37011,192.168.5.136:37013,192.168.5.136: 37015,192.168.5.136: 37017"); sh.addShard("shard2/192.168.5.136:47011,192.168.5.136:47013,192.168.5.136: 47015,192.168.5.136: 47017"); sh.addShard("shard3/192.168.5.136:57011,192.168.5.136:57013,192.168.5.136: 57015,192.168.5.136: 57017"); sh.addShard("shard1/192.168.5.136:58011,192.168.5.136:58013,192.168.5.136: 58015,192.168.5.136: 58017"); sh.status()

mongodb|mongodb分片集群搭建
文章图片

开启分片:
为数据库开启分片功能 sh.enableSharding("lagou_resume") 为指定集合开启分片功能 sh.shardCollection("lagou_resume.lagou_resume_datas",{"name":“hashd”})

6.测试分片功能 往route路由节点里插入数据:
mongodb|mongodb分片集群搭建
文章图片

查看分片数据:
mongodb|mongodb分片集群搭建
文章图片

7.开启权限认证 mongodb默认没有用户,所以需要先建立一个root用户,后续才可以进行用户权限的更改
use admin db.createUser( {user:"root",pwd:"123456", roles:[{role:"root",db:"admin"}]})

生成密钥文件,放在mongodb目录下的data/mongodb,没有需要新建
openssl rand -base64 756 > data/mongodb/testKeyFile.file #修改执行权限 chmod 600 data/mongodb/keyfile/testKeyFile.file

需要在config节点和数据分片节点增加配置
auth=true keyFile=data/mongodb/testKeyFile.file

在route节点配置中增加配置
keyFile=data/mongodb/testKeyFile.file

重启所有的mongodb实例,要按顺序启动;可使用脚本启动
./bin/mongod -f config/config-17011.conf ./bin/mongod -f config/config-17013.conf ./bin/mongod -f config/config-17015.conf ./bin/mongod -f shard/shard1/shard1-37011.conf ./bin/mongod -f shard/shard1/shard1-37013.conf ./bin/mongod -f shard/shard1/shard1-37015.conf ./bin/mongod -f shard/shard1/shard1-37017.conf./bin/mongod -f shard/shard2/shard2-47011.conf ./bin/mongod -f shard/shard2/shard2-47013.conf ./bin/mongod -f shard/shard2/shard2-47015.conf ./bin/mongod -f shard/shard2/shard2-47017.conf./bin/mongod -f shard/shard3/shard3-57011.conf ./bin/mongod -f shard/shard3/shard3-57013.conf ./bin/mongod -f shard/shard3/shard3-57015.conf ./bin/mongod -f shard/shard3/shard3-57017.conf./bin/mongod -f shard/shard4/shard4-58011.conf ./bin/mongod -f shard/shard4/shard4-58013.conf ./bin/mongod -f shard/shard4/shard4-58015.conf ./bin/mongod -f shard/shard4/shard4-58017.conf./bin/mongos -f route/route-27017.conf

认证用户,要进入对应数据库;认证完之后才可以操作
use admin db.auth("root","123456")

8.集成springboot 添加依赖
org.springframework.boot spring-boot-starter-data-mongodb 2.2.2.RELEASE

创建实体
@Document("lg_resume_datas") public class Resume {....}

创建dao接口和实现类
@Service("resumeDao") public class ResumeDAOImpl implements ResumeDAO { @Autowired private MongoTemplate mongoTemplate; @Override public void insertResume(Resume resume) { //mongoTemplate.insert(resume); mongoTemplate.insert(resume,"lg_resume_datas"); }@Override public Resume findByName(String name) { Query query = new Query(); query.addCriteria(Criteria.where("name").is(name)); List datas = mongoTemplate.find(query,Resume.class,"lg_resume_datas"); returndatas.isEmpty()?null:datas.get(0); }@Override public List findList(String name) { Query query = new Query(); query.addCriteria(Criteria.where("name").is(name)); List datas = mongoTemplate.find(query,Resume.class,"lg_resume_datas"); returndatas; }@Override public List findListByNameAndExpectSalary(String name, double expectSalary) { Query query = new Query(); //query.addCriteria(Criteria.where("name").is(name).andOperator(Criteria.where("expectSalary").is(expectSalary))); query.addCriteria(Criteria.where("name").is(name).andOperator(Criteria.where("expectSalary").is(expectSalary))); returnmongoTemplate.find(query,Resume.class,"lg_resume_datas"); } }

测试用例
@SpringBootApplication public class DemoApplication { public static void main(String[] args) { ApplicationContext applicationContext= SpringApplication.run(DemoApplication.class,args); ResumeDAO resumeDao = applicationContext.getBean("resumeDao",ResumeDAO.class); testInsert(resumeDao); testQuery(resumeDao); }private static void testQuery(ResumeDAO resumeDao) { Resumeresume2=resumeDao.findByName("lisi22"); System.out.println(resume2); }private static void testInsert(ResumeDAO resumeDao) { Resume resume= new Resume(); resume.setName("赵六"); resume.setCity("北京"); Date date = null; StringdateStr = "yyyy-MM-dd hh:mm:ss"; SimpleDateFormat simpleDateFormat= new SimpleDateFormat(dateStr); try { date = simpleDateFormat.parse("2003-11-02 11:13:14"); } catch (ParseException e) { e.printStackTrace(); } resume.setBirthday(date); resume.setExpectSalary(28000); resumeDao.insertResume(resume); System.out.println("resume="+resume); } }

效果展示
mongodb|mongodb分片集群搭建
文章图片

mongodb|mongodb分片集群搭建
文章图片

    推荐阅读