MongoDB replica set

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.