mongoDB单机部署集群(分片)

mongoDB单机部署集群

  • MongoDB集群
    • 批处理执行命令创建文件夹
    • 第一步
    • 第二步
    • 第三步
    • 复制集
    • 分片
    • 第四步
    • 第五步

MongoDB集群

mongoDB单机部署集群(分片)_第1张图片
在四块磁盘上面建立mongodb分片集群

批处理执行命令创建文件夹

第一步

  1. 新建bat文件,命名为MSS.bat
  2. 将下面批处理复制到bat文件里面
@echo off 
@echo ----------------------------------------------------------
set var = "C:";

::创建配置服务文件

@echo #----------create config server a------------
::创建第一个配置文件
mkdir MSS\config_server_a\data
mkdir MSS\config_server_a\log
mkdir MSS\config_server_a\pid

echo # port 10031 > \MSS\config_server_a\config.conf
echo sharding:							  				  >> \MSS\config_server_a\config.conf						
echo   clusterRole: configsvr 			  				  >> \MSS\config_server_a\config.conf
echo replication:						  				  >> \MSS\config_server_a\config.conf
echo   replSetName: configshard			  				  >> \MSS\config_server_a\config.conf
echo net:								  				  >> \MSS\config_server_a\config.conf
echo   bindIp: 127.0.0.1     			  				  >> \MSS\config_server_a\config.conf
echo   port: 10031                        				  >> \MSS\config_server_a\config.conf
echo storage:											  >> \MSS\config_server_a\config.conf
echo    dbPath: \MSS\config_server_a\data  	  			  >> \MSS\config_server_a\config.conf
echo    journal:                            			  >> \MSS\config_server_a\config.conf
echo       enabled: true                      			  >> \MSS\config_server_a\config.conf
echo    directoryPerDB: true                              >> \MSS\config_server_a\config.conf
echo systemLog:                                    		  >> \MSS\config_server_a\config.conf
echo    destination: file                         		  >> \MSS\config_server_a\config.conf
echo    path: \MSS\config_server_a\log\config.log  	      >> \MSS\config_server_a\config.conf
echo    logAppend: true							  		  >> \MSS\config_server_a\config.conf						
echo 
echo processManagement:								      >> \MSS\config_server_a\config.conf
echo    pidFilePath: \MSS\config_server_a\pid\config.pid  >> \MSS\config_server_a\config.conf


@echo #----------create config server b------------   				

::创建第二个配置文件
mkdir MSS\config_server_b\data
mkdir MSS\config_server_b\log
mkdir MSS\config_server_b\pid


echo # port 10032 > \MSS\config_server_b\config.conf
echo sharding:							  				  >> \MSS\config_server_b\config.conf						
echo   clusterRole: configsvr 			  				  >> \MSS\config_server_b\config.conf
echo replication:						  				  >> \MSS\config_server_b\config.conf
echo   replSetName: configshard			  				  >> \MSS\config_server_b\config.conf
echo net:								  				  >> \MSS\config_server_b\config.conf
echo   bindIp: 127.0.0.1     			  				  >> \MSS\config_server_b\config.conf
echo   port: 10032                        				  >> \MSS\config_server_b\config.conf
echo storage:											  >> \MSS\config_server_b\config.conf
echo    dbPath: \MSS\config_server_b\data  	  			  >> \MSS\config_server_b\config.conf
echo    journal:                            			  >> \MSS\config_server_b\config.conf
echo       enabled: true                      			  >> \MSS\config_server_b\config.conf
echo    directoryPerDB: true                              >> \MSS\config_server_b\config.conf
echo systemLog:                                    		  >> \MSS\config_server_b\config.conf
echo    destination: file                         		  >> \MSS\config_server_b\config.conf
echo    path: \MSS\config_server_b\log\config.log  	  	  >> \MSS\config_server_b\config.conf
echo    logAppend: true							  		  >> \MSS\config_server_b\config.conf						
echo 
echo processManagement:								      >> \MSS\config_server_b\config.conf
echo    pidFilePath: \MSS\config_server_b\pid\config.pid  >> \MSS\config_server_b\config.conf


@echo #----------create config server c------------

::创建第三个配置文件
mkdir MSS\config_server_c\data
mkdir MSS\config_server_c\log
mkdir MSS\config_server_c\pid


echo # port 10033 > \MSS\config_server_c\config.conf

echo sharding:							  				  >> \MSS\config_server_c\config.conf						
echo   clusterRole: configsvr 			  				  >> \MSS\config_server_c\config.conf
echo replication:						  				  >> \MSS\config_server_c\config.conf
echo   replSetName: configshard			  				  >> \MSS\config_server_c\config.conf
echo net:								  				  >> \MSS\config_server_c\config.conf
echo   bindIp: 127.0.0.1     			  				  >> \MSS\config_server_c\config.conf
echo   port: 10033                        				  >> \MSS\config_server_c\config.conf
echo storage:											  >> \MSS\config_server_c\config.conf
echo    dbPath: \MSS\config_server_c\data  	  			  >> \MSS\config_server_c\config.conf
echo    journal:                            			  >> \MSS\config_server_c\config.conf
echo       enabled: true                      			  >> \MSS\config_server_c\config.conf
echo    directoryPerDB: true                              >> \MSS\config_server_c\config.conf
echo systemLog:                                    		  >> \MSS\config_server_c\config.conf
echo    destination: file                         		  >> \MSS\config_server_c\config.conf
echo    path: \MSS\config_server_c\log\config.log  	  	  >> \MSS\config_server_c\config.conf
echo    logAppend: true							  		  >> \MSS\config_server_c\config.conf						
echo 
echo processManagement:								      >> \MSS\config_server_c\config.conf
echo    pidFilePath: \MSS\config_server_c\pid\config.pid  >> \MSS\config_server_c\config.conf

::创建mongodb route文件夹

@echo #----------create route------------
mkdir MSS\route\log

echo # port 20000 > \MSS\route\config.conf
::如果配置服务的IP发生变化,在此处修改配置服务的IP即可
::端口号为:20000
echo sharding:                                                                	>> \MSS\route\config.conf
echo    configDB: configshard/127.0.0.1:10031,127.0.0.1:10032,127.0.0.1:10033 	>> \MSS\route\config.conf
echo net:           														  	>> \MSS\route\config.conf
echo    bindIp: 127.0.0.1 													  	>> \MSS\route\config.conf
echo    port: 20000															  	>> \MSS\route\config.conf
echo systemLog:																  	>> \MSS\route\config.conf
echo    destination: file                                                     	>> \MSS\route\config.conf
echo    path: \MSS\route\log\route.log                                		    >> \MSS\route\config.conf
echo    logAppend: true                                                       	>> \MSS\route\config.conf

::创建第一个shard

@echo #----------create shard-a------------

mkdir MSS\shard-a
mkdir MSS\shard-a\pid
mkdir MSS\shard-a\log

echo # port 10011 > \MSS\shard-a\shard.conf
echo sharding:									     >> \MSS\shard-a\shard.conf
echo   clusterRole: shardsvr						 >> \MSS\shard-a\shard.conf
echo replication:									 >> \MSS\shard-a\shard.conf
echo   replSetName: shard1                        	 >> \MSS\shard-a\shard.conf
echo net:                                            >> \MSS\shard-a\shard.conf
echo   bindIp: 127.0.0.1                             >> \MSS\shard-a\shard.conf
echo   port: 10011                                   >> \MSS\shard-a\shard.conf
echo storage:                                        >> \MSS\shard-a\shard.conf
echo    dbPath: \shard\data             			 >> \MSS\shard-a\shard.conf
echo    journal:                                     >> \MSS\shard-a\shard.conf
echo       enabled: true                             >> \MSS\shard-a\shard.conf
echo    directoryPerDB: true                         >> \MSS\shard-a\shard.conf
echo systemLog:                                      >> \MSS\shard-a\shard.conf
echo    destination: file                            >> \MSS\shard-a\shard.conf
echo    path: \MSS\shard-a\log\shard.log   			 >> \MSS\shard-a\shard.conf
echo    logAppend: true                              >> \MSS\shard-a\shard.conf
echo 
echo processManagement:                              >> \MSS\shard-a\shard.conf
echo    pidFilePath: \MSS\shard-a\pid\shard.pid 	 >> \MSS\shard-a\shard.conf
   

::创建第二个shard
@echo #----------create shard-b------------
mkdir MSS\shard-b
mkdir MSS\shard-b\pid
mkdir MSS\shard-b\log

echo # port 10012 > \MSS\shard-b\shard.conf
echo sharding:									     >> \MSS\shard-b\shard.conf
echo   clusterRole: shardsvr						 >> \MSS\shard-b\shard.conf
echo replication:									 >> \MSS\shard-b\shard.conf
echo   replSetName: shard2                        	 >> \MSS\shard-b\shard.conf
echo net:                                            >> \MSS\shard-b\shard.conf
echo   bindIp: 127.0.0.1                             >> \MSS\shard-b\shard.conf
echo   port: 10012                                   >> \MSS\shard-b\shard.conf
echo storage:                                        >> \MSS\shard-b\shard.conf
echo    dbPath: \shard\data             			 >> \MSS\shard-b\shard.conf
echo    journal:                                     >> \MSS\shard-b\shard.conf
echo       enabled: true                             >> \MSS\shard-b\shard.conf
echo    directoryPerDB: true                         >> \MSS\shard-b\shard.conf
echo systemLog:                                      >> \MSS\shard-b\shard.conf
echo    destination: file                            >> \MSS\shard-b\shard.conf
echo    path: \MSS\shard-b\log\shard.log    		 >> \MSS\shard-b\shard.conf
echo    logAppend: true                              >> \MSS\shard-b\shard.conf
echo 
echo processManagement:                              >> \MSS\shard-b\shard.conf
echo    pidFilePath: \MSS\shard-b\pid\shard.pid 	 >> \MSS\shard-b\shard.conf

   

::创建第二个shard
@echo #----------create shard-c------------
mkdir MSS\shard-c
mkdir MSS\shard-c\pid
mkdir MSS\shard-c\log

echo # port 10013 > \MSS\shard-c\shard.conf
echo sharding:									     >> \MSS\shard-c\shard.conf
echo   clusterRole: shardsvr						 >> \MSS\shard-c\shard.conf
echo replication:									 >> \MSS\shard-c\shard.conf
echo   replSetName: shard3                        	 >> \MSS\shard-c\shard.conf
echo net:                                            >> \MSS\shard-c\shard.conf
echo   bindIp: 127.0.0.1                             >> \MSS\shard-c\shard.conf
echo   port: 10013                                   >> \MSS\shard-c\shard.conf
echo storage:                                        >> \MSS\shard-c\shard.conf
echo    dbPath: \shard\data             		     >> \MSS\shard-c\shard.conf
echo    journal:                                     >> \MSS\shard-c\shard.conf
echo       enabled: true                             >> \MSS\shard-c\shard.conf
echo    directoryPerDB: true                         >> \MSS\shard-c\shard.conf
echo systemLog:                                      >> \MSS\shard-c\shard.conf
echo    destination: file                            >> \MSS\shard-c\shard.conf
echo    path: \MSS\shard-c\log\shard.log    		 >> \MSS\shard-c\shard.conf
echo    logAppend: true                              >> \MSS\shard-c\shard.conf
echo 
echo processManagement:                              >> \MSS\shard-c\shard.conf
echo    pidFilePath: \MSS\shard-c\pid\shard.pid 	 >> \MSS\shard-c\shard.conf   


::创建第三个shard
@echo #----------create shard-d------------
mkdir MSS\shard-d
mkdir MSS\shard-d\pid
mkdir MSS\shard-d\log

echo # port 10014 > \MSS\shard-d\shard.conf
echo sharding:									     >> \MSS\shard-d\shard.conf
echo   clusterRole: shardsvr						 >> \MSS\shard-d\shard.conf
echo replication:									 >> \MSS\shard-d\shard.conf
echo   replSetName: shard4                        	 >> \MSS\shard-d\shard.conf
echo net:                                            >> \MSS\shard-d\shard.conf
echo   bindIp: 127.0.0.1                             >> \MSS\shard-d\shard.conf
echo   port: 10014                                   >> \MSS\shard-d\shard.conf
echo storage:                                        >> \MSS\shard-d\shard.conf
echo    dbPath: \shard\data            		         >> \MSS\shard-d\shard.conf
echo    journal:                                     >> \MSS\shard-d\shard.conf
echo       enabled: true                             >> \MSS\shard-d\shard.conf
echo    directoryPerDB: true                         >> \MSS\shard-d\shard.conf
echo systemLog:                                      >> \MSS\shard-d\shard.conf
echo    destination: file                            >> \MSS\shard-d\shard.conf
echo    path: \MSS\shard-d\log\shard.log    		 >> \MSS\shard-d\shard.conf
echo    logAppend: true                              >> \MSS\shard-d\shard.conf
echo 
echo processManagement:                              >> \MSS\shard-d\shard.conf
echo    pidFilePath: \MSS\shard-d\pid\shard.pid 	 >> \MSS\shard-d\shard.conf 
											

::创建批处理文件
@echo #----------shard1------------
echo mongod -f ./shard-a/shard.conf >> \MSS\shard-1.bat
@echo #----------shard2------------
echo mongod -f ./shard-b/shard.conf >> \MSS\shard-2.bat
@echo #----------shard3------------
echo mongod -f ./shard-c/shard.conf >> \MSS\shard-3.bat
@echo #----------shard4------------
echo mongod -f ./shard-d/shard.conf >> \MSS\shard-4.bat


@echo #----------config-server-a------------
echo mongod -f ./config_server_a/config.conf >> \MSS\config-server-a.bat

@echo #----------config-server-b------------
echo mongod -f ./config_server_b/config.conf >> \MSS\config-server-b.bat

@echo #----------config-server-c------------
echo mongod -f ./config_server_c/config.conf >> \MSS\config-server-c.bat

@echo #----------Route------------
echo mongos -f ./route/config.conf >> \MSS\Route.bat

@echo #----------setup config_server------------
echo mongo --port 10031 >> \MSS\config_server.bat

@echo #----------initial file------------
echo #init file > \MSS\_init.bat
echo start config-server-a.bat           >> \MSS\_init.bat
echo start config-server-b.bat           >> \MSS\_init.bat
echo start config-server-c.bat           >> \MSS\_init.bat
echo start shard-1.bat           		 >> \MSS\_init.bat
echo start shard-2.bat           		 >> \MSS\_init.bat
echo start shard-3.bat           		 >> \MSS\_init.bat
echo start shard-4.bat           		 >> \MSS\_init.bat
echo start Route.bat           		 	 >> \MSS\_init.bat
echo pause           		 	 	     >> \MSS\_init.bat

@echo #-------------create shard data-------
mkdir \shard\data 

pause

以上批处理命令打开即可创建文件夹以及配置文件
此时批处理磁盘出现了两个文件夹 一个MSS 和 shard
MSS:里面有三个配置服务,一个路由 和 四个 shard的配置文件和一些批处理的启动文件
shard:存放数据

第二步

  1. 执行config_server_a.bat、config_server_b.bat、config_server_c.bat这三个批处理文件

  2. 重新打开CMD窗口(按住Shift键加鼠标右键)输入mongo --port 10031 (!配置服务任意端口号即可)

  3. 初始化三个配置服务

(!根据自己生成配置文件的端口号修改,只需修改端口号)

rs.initiate({_id:"configshard",configsvr:true,members:[{_id:1,host:"127.0.0.1:10031"},{_id:2,host:"127.0.0.1:10032"},{_id:3,host:"127.0.0.1:10033"}]})

查看主节点

rs.isMaster()

第三步

复制集

rs.initiate({"_id" : "sha","members" : [{"_id" : 0,"host" : "127.0.0.1:10011",priority:1000},{"_id" : 1,"host" : "127.0.0.1:10012",priority:999},{"_id" : 2,"host" : "127.0.0.1:10013",priority:998}]});

分片

	##  shard-1
  1. 执行shard-1.bat批处理文件
  2. 重新打开CMD窗口(按住Shift键加鼠标右键)输入mongo --port 10011 (!shard任意端口号即可)
  3. 初始化配置服务
rs.initiate({"_id" : "shard1","members" : [{"_id" : 0,"host" : "127.0.0.1:10011",priority:1000}]});
##  shard-2
  1. 执行shard-2.bat批处理文件
  2. 重新打开CMD窗口(按住Shift键加鼠标右键)输入mongo --port 10012 (!shard任意端口号即可)
  3. 初始化配置服务
rs.initiate({"_id" : "shard2","members" : [{"_id" : 0,"host" : "127.0.0.1:10012",priority:1000}]});
##  shard-3
  1. 执行shard-3.bat批处理文件
  2. 重新打开CMD窗口(按住Shift键加鼠标右键)输入mongo --port 10013 (!shard任意端口号即可)
  3. 初始化配置服务
rs.initiate({"_id" : "shard3","members" : [{"_id" : 0,"host" : "127.0.0.1:10013",priority:1000}]});
##  shard-4
  1. 执行shard-4.bat批处理文件
  2. 重新打开CMD窗口(按住Shift键加鼠标右键)输入mongo --port 10014 (!shard任意端口号即可)
  3. 初始化配置服务
rs.initiate({"_id" : "shard4","members" : [{"_id" : 0,"host" : "127.0.0.1:10014",priority:1000}]});

第四步

  1. 执行Route.bat批处理文件
  2. 重新打开CMD窗口(按住Shift键加鼠标右键)输入mongo–port 20000(!shard任意端口号即可)
  3. 添加分片
sh.addShard("shard1/127.0.0.1:10011");
sh.addShard("shard2/127.0.0.1:10012");
sh.addShard("shard3/127.0.0.1:10013");
sh.addShard("shard4/127.0.0.1:10014");

第五步

····················································································································

use world
sh.enableSharding("world"); //设置world为分片
sh.shardCollection("world.tiles",{name:"hashed"}); //设置分片集**
for (var i=0;i<100000;i++) db.world.insert({name:"zs"+i,age:i}); //插入数据
db.teacher.find();//查看数据

你可能感兴趣的:(GIS开发)