Reprinted from http://www.cnblogs.com/spnt/
Since MongoDB is used, it has to use its secure backup mechanism: replica set.
What is MongoDB replica set?
In general, multiple machines are used for asynchronous synchronization of the same data, so that multiple machines have multiple copies of the same data, in addition, when the master database crashes, automatic failover is automatically performed on other backup servers without user intervention. The replica server can also be used as a read-only server to implement read/write splitting and increase load.
The index is: MongoDB master-slave replication mode is not officially recommended.
How can this problem be achieved?
I use different folders of a machine for simulation (it is best to use different servers for physical isolation in a production environment ).
My file directory is:
F: \ mongodb1 \
F: \ DB2 \
F: \ mongodb3 \
Now, start the operation.
The above is the startup command.
Parameter: Port: MongoDB listening port
Dbpath: data file storage path
Logpath: system log storage path
Replset: replica set name. I use test. Other nodes must use this name as the replica set name.
Logappend: the log write mode is append, not the default overwrite mode.
Similarly, run the preceding commands on DB2 and mongodb3 in the same directory. The port numbers are 2222,3333, as shown below:
After MongoDB is started, only one step is left. (Open a new cmd here to operate. All commands are executed here. Do not close it)
First, connect to mongodb1 instance by executing: Mongo -- Port 1111
Then define the configuration information: config_test,
Finally, start the replica set through RS. Initiate (config_test ).
So far, the configuration of the entire replica set has been completed. You can run the Rs. Status () command to view the progress.
The following parameters are described as follows:
Health: 1 // 1 indicates that the status is normal, and 0 indicates that the status is abnormal.
State: 1 // 1 indicates primary, 2 indicates slave, that is, the backup machine
The result shows that it is a master database and two backups.
What if a server crashes?
We stop mongodb1 and then run Rs. Status (), as shown below:
We can see that MongoDB has been replaced. At this time, mongodb3 becomes the master database.
After the configuration is complete, you will inevitably need to perform maintenance. For more information about maintenance, see my next article.