1. Install the VM. Install the Linux OS
2. Install the mongo
(1) Download address: www.mongodb.com/download-ce…
2. Decompress tar -xzvf mongodb-*****. TGZ \ in /usr/local
Rename the decompressed package mv mongodb-**** mongodb
Mkdir -p /data/db\
Run the following command to set the environment variable vi /etc/profile: export PATH=/usr/local/mongodb/bin:$PATH\
3. Start: mongo –nodb
4. Create a replicaSet: replicaSet = new ReplSetTest({“nodes”:3}) // create three replicas to view the newly created ports
Such as, 20000, 20001, 20002
5. Replicaset.startset ()
6. Initialize the replicaSet: replicaset.initiate () //
7. Switch to another command window to start MongoDB: mongo –nodb
Conn1 = new mongo (“localhost:20000”)
9. Enter the database: primaryDB = conn1.getdb (“test”)
10. Check whether there is a primary node: primarydb.ismaster ()
{“primary” : true} // Indicates that the primary node is connected in the same way
Insert ({“name” : “HBW “}) {primarydb.coll.
13. Can read written information: primarydb.col.find ()
14. Reconnect a secondary node:
conn2 = new Mongo(“localhost:20001”)
secondaryDB = conn2.getDB(“test”)
Secondarydb.coll.find () secondarydb.coll.find ()
Conn2.setslaveok ()
You can then read the information inserted into the COLL document by the master node
Secondarydb. runCommand({“getLastError”:1}) secondarydb. runCommand({“getLastError”:1})
Note Only the primary node has the write permission, while the secondary node has the query permission
15. However, if the primary node is shutdown or becomes unavailable for some reason, a new primary node is selected according to most (more than half) of the rules. You can query the new primary node by following the steps above. 1})