MongoDB Learning (5) master-slave replication

Source: Internet
Author: User
Tags server port

From this beginning we mainly discuss the deployment technology of MongoDB.

We know that SQL Server can read and write separation, dual-machine hot backup and cluster deployment, of course, MongoDB can also do, in practical applications we do not want the database to use a single point of deployment,

How bad it would be if you hit a database outage or were devastated.

One: Master-slave replication

1: First look at the model diagram

2: From the graph above we can analyze the benefits of this architecture as follows:

<1> data Backup.

<2> data recovery.

<3> read/write separation.

3: Let's practice the following

In practical applications we are certainly multi-server deployments, limited to their own lazy installation of virtual machines, on a machine to practice.

The first step: we put the MongoDB folder on the D and E disk, the simulation on the multi-server.

The second step: start the D drive MongoDB, the database designated as the main database, in fact, the command is simple: >mongodb--dbpath= ' XXX '--master,

The port is still the default of 27017.

The third step: the same way to start MongoDB on the E-disk, specify that the database is a subordinate database, the command is very simple, of course, we want to change a port, such as: 8888.

Source represents the address of the primary database.

>mongod--dbpath=xxxx--port=8888--slave--source=127.0.0.1:27017

Fourth step: The red area we found a: "Applied 1 operations" such a statement, and the occurrence of the time is 10s apart, also indicates that the subordinate database every 10s

Synchronize the data to the main database, the synchronization is to find the main database "OpLog" log, you can find in the red area of the image "Sync_pulloplog" word.

What we're going to do next is test, and it's amazing that the data has been updated in a synchronized way.

4: If I want to add a subordinate database, but I do not want to specify at startup, but later specify, then MongoDB can do it? The answer must be yes.

Our master or subordinate database has a collection called local, which is primarily used for storing internal replication information.

OK, so let's try it, I'll copy another MongoDB run program in F-disk, cmd window a lot of ah, let's not mess up.

Look at the log above, the hint does not have the main database, it's OK, one day our conscience found, give him a late subsidy, haha, open a CMD window, the statement is

Add a host address in the sources and finally find the data synchronized to the 127.0.0.1:5555 slave database ....

5: Read and write separation

This approach is implemented in a larger architecture, which is very simple in MongoDB, and, by default, the subordinate database does not support the reading of the data, but it doesn't matter,

The driver provides us with a read-dependent database called "Slaveokay" that we can display to alleviate the performance stress of the primary database, which is not demonstrated here.

II: Replica Set

This is also very cow x master-slave cluster, but with the above cluster is still two points apart.

<1>: The cluster does not have a specific primary database.

<2>: If the main database is down, the cluster will elect a subordinate database to be the top of the primary database, this has the automatic recovery function, very bull x ah.

OK, let's try it now, first turn all the cmd windows off, and clear out all the files under the DB.

The first step: since we want to set up a cluster, we have to take a cluster name, here to take our company name Shopex,--replset said to let the server know Shopex there are other databases,

This will open the MongoDB program in the D disk, the port is 2222. Specifying a port of 3333 is another database server under the Shopex cluster.

The second step: since the above said 3333 is another database server, do not hurry, now to open, here the e-disk of the MongoDB program opened.

Step three: OK, look at the red area of the log above, it seems we have not finished, yes, the log information tells us to initialize the "replica set", since the log says so, then I will

To do so, connect any of the servers can be done, but be sure to enter the Admin collection.

Fourth step: After the success, we want to see who can become the primary database server, you can see that the port is 2222 has become the primary database server.

Fifth step: We know that there is a SQL Server called the Arbiter server, then there is also MongoDB, as with SQL Server, the arbitration only participate in voting, here we

Use the F-disk MongoDB as the arbiter server and specify any server port in the Shopex cluster, specifying 2222.

We then use the Rs.addarb () append in the Admin collection.

After the addition, we use Rs.status () to see the status of the servers in the cluster, and we can see clearly who is the Lord, from, or quorum.

Not that the cluster has automatic failback? Then we can try, on the 2222-port cmd Server press CTRL + C to KO off the server, immediately we found

On the 3333-Port slave server, you can also use Rs.status () to look at the status of the servers in the cluster.

Copyright NOTICE: Welcome reprint, Hope in your reprint at the same time, add the original address, thank you with

MongoDB Learning (5) master-slave replication

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.