MongoDB The master copy configuration of the introductory tutorial _mongodb

Source: Internet
Author: User
Tags mongodb time interval server port

Starting from this article, we mainly discuss the deployment technology of MongoDB.

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

How bad it would be to run into a database outage or be destroyed.

One: master-slave copy

1: First look at the model diagram

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

<1> data Backup.

<2> data recovery.

<3> Read and write separation.

3: Here we go.

In practical applications, we must be a multiple server deployment, limited to their own lazy to install the virtual machine, on a machine on the practice.

The first step: we put the MongoDB folder in D and E disk, simulation on multiple servers.

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

Port is still the default of 27017.

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

Source represents the address of the primary database.

Copy Code code as follows:

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

Fourth step: From the red area of the figure we found a statement such as "Applied 1 operations", and the time interval between 10s, also indicates that the subordinate database per 10s

To synchronize the data to the main database, the synchronization basis is to find the main database "Oplog" log, you can find in the red area of the figure "sync_pulloplog" words.

The next thing we want to do is test, surprised to find that the data has been synchronized update, cool AH.

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

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

OK, then let's try, I copy a copy of the MongoDB running program in the F disk, CMD window a lot of ah, we don't mess up.

Look at the log above, hint that there is no main database, it does not matter, one day our conscience found, give him a later subsidy, haha, and then open a CMD window, the statement is

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

5: Read-Write separation

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

The driver provides us with a "slaveokay" to allow us to display the read subordinate database to reduce the performance pressure on the primary database, which is not demonstrated here.

Two: Replica set

This is also very cow X's master and slave cluster, but with the above cluster still has two points of difference.

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

<2>: If which primary database is down, the cluster will elect a subordinate database as the main database on the top, which has the automatic recovery function, very cow x ah.

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

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

Here the MongoDB program on the D disk is opened and 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 worry, now to open, here the e-disk MongoDB program open.

The third step: OK, look at the above log red area, it seems we have not finished, yes, log information tells us to initialize the "replica set", since the log said, then I will

To do this, you can connect to any server, 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 port 2222 has become the primary database server.

Fifth step: We know that there is a SQL Server called the quorum server, then there are MongoDB, like SQL Server, arbitration only vote, here we

Use the MongoDB of the F disk as the quorum server, and then specify any server port in the Shopex cluster, specifying 2222 here.

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

After we have appended, we use Rs.status () to view the server status in the group, we can see clearly who is the master, or from, or arbitration.

Does it mean that the cluster has automatic failure recovery? So we can try, on the 2222-port cmd Server press CTRL + C to KO the server, and immediately we found

On the top of the 3333-Port slave server, you can finally use Rs.status () to see the status of the servers in the group.

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.