MongoDB cluster - replica set

1. Environment preparation: cluster mode of replica set, at least 3 servers are required

 

        It can be seen from the figure that the client is connected to the entire replica set, and does not care which machine is down. The master server is responsible for reading and writing the entire replica set, and the replica set regularly synchronizes data backups. Once the master node hangs and the replica node detects it through the heartbeat mechanism, it will initiate the election mechanism of the master node in the cluster to automatically elect a new one. main server.

 

2. Download mongodb and install it on 3 servers respectively

 

3. In the mongodb installation directory (such as D:/mongodb), create a new empty file data, create a new log/mongodb.log file, and create a new conf/config.properties file.

 

4. Write the conf/config.properties file, the configuration information is as follows:
dbpath=D:\mongodb\data

logpath=D:\mongodb\log\mongodb.log

port=27017 #mongodb port

logappend=

journal=

jsonp =

serviceName=MongoDB Server #mongodb service name

serviceDisplayName=MongoDB Server replSet #mongodb display name

auth=false #auth: Whether authentication is required; if authentication is required, change to true

storageEngine=wiredTiger

wiredTigerCacheSizeGB=2

#replica set name

replSet=zdvictory

 

5. Install and start the service command

#install service

D:\mongodb\bin>mongod.exe -config D:\mongodb\conf\config.properties --install

#Start the service (you can open the service and start it manually)

D:\mongodb\bin>mongod.exe --dbpath D:\mongodb\data

 

6. In each machine, start the MongoDB service and choose any machine to log in to MongoDB (note that none of the three mongodbs must be used, that is, there is no data, otherwise the clustering will not be possible)

Open cmd command window

D:\mongodb\bin>mongo --port 27017

> use admin

switched to db admin

> config = {

     "_id":"zdvictory",

     "members":[

         {"_id":0,"host":"192.168.5.161:27017"},

         {"_id":1,"host":"192.168.15.250:27017"},

         {"_id":2,"host":"192.168.88.9:27017"}

     ]

 }

> rs.initiate(config)
{ "ok" : 1 }

 

7. Common commands of mongodb cluster

D:\mongodb\bin>mongo --port 27017

#View status
zdvictory:PRIMARY>rs.status()

# Online delete
zdvictory:PRIMARY> rs.remove("192.168.15.250:27017")

#Add a node online
zdvictory:PRIMARY> rs.add("192.168.15.250:27017")

#View configuration
zdvictory:PRIMARY> rs.conf()

 

8. Last note: When there are only 2/3 mongodb clusters, the master node can be automatically switched (can be used normally), and when there is only one mongodb cluster, it cannot be automatically switched to the master node (can not be used normally)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325444167&siteId=291194637