MongoDB replication mechanism instance

Source: Internet
Author: User
Tags install mongodb

MongoDB replication mechanism instance

MongoDB master-slave replication is a master node that can replicate multiple slave nodes and master nodes. Here, one master node and one slave server use one database server to provide different ports.

1. Start a MongoDB service named application and port 1234

./Mongod -- replSet application -- dbpath data/node1 -- port 1234 -- oplogSize 1024


Indicates that startup is successful.
2. Open the client
./Mongo -- port 1234

3. Start node2, node3

./Mongod -- replSet application -- dbpath data/node2 -- port 1235 -- oplogSize 1024

./Mongod -- replSet application -- dbpath data/node3 -- port 1236 -- oplogSize 1024


4. Start the client with port 1234
./Mongo -- port 1234
V. Configuration copy
Config = {_ id: "application", members: []}
Config. members. push ({_id: 0, host: "localhost: 1234 "})
config.members.push({_id:1,host:"localhost:1235"})
Config. members. push ({_ id: 2, host: "localhost: 1236", arbiterOnly: true })

1 indicates that the instance is successfully added.
Vi. rs. isMaster ()

7. Test whether master-slave replication is implemented
1. Add a record to the master server
1.1 use test
Db. user. insert ({_ id: 1, name: "maybo "})


1.2 open the slave service client
./Mongo -- port 1235

1.3 Query
Use test
Db. user. find ("{})

It is found that slaveOk = false the current node needs to specify the slave Node
1.4 rs. slaveOk ()
Show collections

It indicates that the master-slave replication is successful, but the master-slave replication takes one time.

For more MongoDB tutorials, see the following:

CentOS compilation and installation of php extensions for MongoDB and mongoDB

CentOS 6 install MongoDB and server configuration using yum

Install MongoDB2.4.3 in Ubuntu 13.04

MongoDB beginners must read (both concepts and practices)

MongoDB Installation Guide for Ubunu 14.04

MongoDB authoritative Guide (The Definitive Guide) in English [PDF]

Nagios monitoring MongoDB sharded cluster service practice

Build MongoDB Service Based on CentOS 6.5 Operating System

MongoDB details: click here
MongoDB: click here

This article permanently updates the link address:

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.