Build a cluster
Cluster construction on-line search has 3 programs: Replica Set/sharding/master-slaver
The third type is basically not used by very few people at this time. Main introduction Replica Set and sharding
Replica Set
The deployment Chinese translates to a replica set, which is a primary node, n secondary node, and an arbiter node, when the primary node is hung, the quorum node is moderated by which secondary node is promoted to the primary node.
Continue to run, not because the primary node hangs and affects the entire database. When the primary node resumes, as the secondary node continues to run, the quorum node does not store data and is not connected to the application, but is used only as a quorum. The quorum node cannot be missing, and the secondary node cannot be promoted to the primary node after it is missing. Causes the system to not function properly.
Local impersonation, created only one primary node (MONGODB1), one secondary node (MONGODB2), and one quorum node (MONGODB3).
Under each folder, create a data folder and log Logs folder,
Create a Master store master.config file under the Mongodb1\data folder
Create slaver to store slaver.config files under the Mongodb2\data folder
Create arbiter to store arbiter.config files under the Mongodb3\data folder
Master.config configuration file
#master. Configdbpath=d:/test/mongodb1/data/master #数据存放目录logpath =d:/test/mongodb1/log/master.log # Log storage directory Pidfilepath=d:/test/mongodb1/master.pid #进程文件, convenient to stop mongodbdirectoryperdb=true #为每一个数据库按照数据库名建立文件夹存放lo Gappend=true #以追加的方式记录日志replSet =testrs #副本集名称port =27017 #端口oplogSize = 10000 #mongodb操作日志文件的最大大小. MB, default to 5% of the hard disk's remaining space
Bind_ip:mongodb #所绑定的ip地址 deployed on different computers can be used to deploy multiple ports separately to remove the error otherwise
Fork=true #以后台方式运行进程 This is not a problem under Linux, it's wrong to return under Windows
Slaver.config and Aribiter.config and this directory and port inconsistencies are the same, not listed
Create a startup batch file under the test folder
Master Service installation. bat
@echo OFFD:CD mongodb1\binmongod-config d:\test\mongodb1\data\master\master.config--install--servicename " Mongodbmaster "--servicedisplayname" Mongodbmaster "--journal start=" Auto "pause
Installing the. bat from the service
@echo OFFD:CD mongodb2\binmongod-config d:\test\mongodb2\data\slaver\slaver.config--install--servicename " Mongodbslaver "--servicedisplayname" Mongodbslaver " --journal start=" Auto "pause
Quorum service installation. bat
@echo OFFD:CD mongodb3\binmongod-config d:\test\mongodb3\data\arbiter\arbiter.config--install--servicename " Mongodbarbiter "-servicedisplayname" Mongodbarbiter " --journal start=" Auto "pause
Configuration file
MONGO 127.0.0.1:27017use admin#cfg={_id: "Testrs", Members:[{_id:0,host: ' 127.0.0.1:27017 ', Priority:2},{_id:1,host: ' 127.0.0.1:27018 ', priority:1},{_id:2,host: ' 127.0.0.1:27019 ', arbiteronly:true}]};rs.initiate (CFG) rs.add (" 127.0.0.1:27018 "); Rs.addarb (" 1270.0.01:27019 "); Rs.status ()
CFG can be arbitrary name, of course, it is best not to be mongodb keyword, conf,config can. The outermost _id represents the name of the replica set, and the members contain the address and priority of all nodes. The highest priority becomes the master node, the 127.0.0.1:27017 here. It is particularly important to note that there is a special configuration--arbiteronly:true for the quorum node. This must not be less, or the main standby mode will not be effective.
The configuration is complete and can be viewed via rs.status ().
It can be seen that the IP is the configured Primary (Master node), the secondary node (secondary), and the Quorum node (arbiter)
When the primary node inserts data, the secondary node can be queried, and if the primary node is stopped, the quorum node will elevate the secondary node to the primary node. If the far master node recovers, it becomes a secondary node after recovery.
Data is also automatically changed to the same data as the primary node. Node queries can be viewed through rs.status ().
First knowledge of MongoDB (vii)