Configuring Replica Sets Replica Sets
Keep the data consistent, clean up the database-----practice Operations
[Email protected] ~]# rm-rf/usr/local/mongodb/data/db/*
1. Enable replica set, write config file, 4.2-4.3-4.4, specify host replica set name
[Email protected] ~]# vim/usr/local/mongodb/etc/mongodb.conf
Replset=rs1
2. Start the service, if the service does not come, check the configuration file
[Email protected] ~]# mongod-f/usr/local/mongodb/etc/mongodb.conf
3. Create a cluster-------any one can, this operation--->4.2, connect themselves, on which machine to execute the command to create a cluster, it becomes the main
[Email protected] ~]#/usr/local/mongodb/bin/mongo--host 192.168.4.2--port 27050
config={
... _id: "Rs1",
... members:[
... {_id:0, Host: "192.168.4.2:27050"},
... {_id:1, Host: "192.168.4.3:27051"},
... {_id:2, Host: "192.168.4.4:27052"}
... ]
... };
The output is as follows:
{
"_id": "Rs1",
"Members": [
{
"_id": 0,
"Host": "192.168.4.2:27050"
},
{
"_id": 1,
"Host": "192.168.4.3:27051"
},
{
"_id": 2,
"Host": "192.168.4.4:27052"
}
]
}
4. Initializing the Replica sets environment
? Execute the following command
–>rs.initiate (config)
With output, OK.
Rs1:primary>
5. View replica Set information
Query status information
Rs1:primary> Rs.status ()
See if it is the master library
Rs1:primary> Rs.ismaster ()
######### #其他机器也可以执行上面命令查看
6. Verifying the replica set configuration
Client Connection master: [[email protected] ~]#/usr/local/mongodb/bin/mongo--host 192.168.4.2--port 27050
Write to document:
Rs1:primary> Db.name.save ({name: "Jerry", age:19})
Synchronize data validation------from the library to view the document
Rs1:secondary> Db.getmongo (). Setslaveok () allows viewing of data from a library
Simulate downtime and go from the library to see who is the main
–> Rs.ismaster () see if it is the main library
Connect the current master, write the document, and see if the current Master library is synchronizing data from the library
Repair the database that has been down, execute command Db.getmongo (). Setslaveok () to see if the data is synchronized and is the current master library from the library
Building a copy set of MongoDB