MongoDB master-Slave Model building Example

Source: Internet
Author: User

For the database, in order to ensure the security of data and high availability, often use the master-slave architecture model, for MongoDB is also the case, this article will explain how to build a simple MongoDB master-slave architecture model.

In the following example, because there are no multiple servers, experiments were performed on a single server, and different port numbers were used to differentiate between master and slave.

The first thing we need is to download the official version of MongoDB, here i download the Linux version of 2.4.1, and unzip under Linux, and then create two folders master and slave, respectively, to hold the master MongoDB instance and from the MongoDB instance.


We then first start the Master MongoDB instance in master, as follows:

We see that MongoDB is already waiting for the client to connect on port 27017, and from "Master:true" see the current MongoDB instance is started as master.

Next, we start from the MongoDB instance, and specify that from MongoDB is the slave instance of that master MongoDB,

Mongod--port 27018--dbpath=/lingxi/local/mongodb/slave/mongodb/mongodbdata/-slave-source 172.31.4.15:27017

Here we will start from the MongoDB instance on port 27018 and act as the slave instance of the MongoDB instance on the original 27017 port. The startup code is as follows:


From the above red can be seen, MongoDB from the instance has been started up. The next step is to validate the master-slave mode. Here, we only do a simple test, we write a piece of data to the main MongoDB to see if MongoDB will have data from the instance.


Above, we first create a new collection, insert a new test collection, insert a document {"Caller": "18326149379"}, and then we insert a document again, Let's see if MongoDB can get the new data in time from the instance in the insert process, as follows:

From the results, from the MongoDB instance can be informed of the main MongoDB instance data changes and synchronization, so a simple MongoDB master-slave mode is set up.

It should be explained that, because the master-slave mode also has some problems, for these problems, MongoDB has other methods can be better resolved, so MongoDB official recommendation to try not to use MongoDB Master never provide high-availability scenarios, instead of using replica set and sharding technology, For the explanation of this problem, go back and finish an article to explain, thank you.

MongoDB master-Slave Model building Example

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.