MongoDB replication (Replica set)

Source: Internet
Author: User

Copy (replica set) what is replication
    • Replication provides redundant backups of data, stores copies of data on multiple servers, improves data availability, and ensures data security
    • Replication also allows recovery of data from hardware failures and service outages
Why do you want to copy
    • Data backup
    • Data disaster recovery
    • Read/write separation
    • High (24* 7) data availability
    • No Downtime Maintenance
    • Replica sets are transparent to applications
How replication works
    • Replication requires a minimum of two nodes A, B ...
    • A is the master node responsible for processing client requests
    • The rest is from the node, which is responsible for replicating the data on the master node
    • The common collocation methods of nodes are: one master, one from, one main and many from
    • The master node records all operations on it, obtains these operations from the node periodically polling the master node, and then performs these operations on its own copy of the data, ensuring that the data from the node is consistent with the primary node
    • Data interaction between primary node and slave node ensures data consistency
Features of replication
    • N-node clusters
    • Any node can be used as the master node
    • All write operations are on the primary node
    • Auto Fail-Over
    • Automatic recovery
Setting Up Replication nodes
    • The next operation will need to open multiple terminal windows, and may be connected to more than one Ubuntu host, it will appear a bit messy, it is recommended to implement in Xshell
    • Step1: Create database directory T1, t2
    • Demo in Desktop directory, other directories can also, pay attention to the permissions can be
mkdir t1mkdir t2
    • Step2: Start Mongod with the following format, note that the name of Replset is consistent
mongod --bind_ip 192.168.196.128 --port 27017 --dbpath ~/Desktop/t1 --replSet rs0mongod --bind_ip 192.168.196.128 --port 27018 --dbpath ~/Desktop/t2 --replSet rs0
    • Step3: Connect to master server, set 192.168.196.128:27017 as primary server here
mongo --host 192.168.196.128 --port 27017
    • STEP4: Initialization
rs.initiate()
    • After initialization is complete, the prompt is as follows:

    • Step5: View current status
rs.status()
    • Current state such as:

    • STEP6: Adding replica Sets
rs.add(‘192.168.196.128:27018‘)
    • STEP7: After the replica set has been added successfully, the current state is as follows:

    • STEP8: Connecting a second MONGO service
mongo --host 192.168.196.128 --port 27018
    • After the connection is successful, the prompt is as follows:

    • STEP9: Inserting data into the primary server
use test1for(i=0;i<10;i++){db.t1.insert({_id:i})}db.t1.find()
    • STEP10: Inserting queries from the server
    • Note: If you are reading from a server, you need to set Rs.slaveok ()
rs.slaveOk()db.t1.find()
Other instructions
    • Remove from node
rs.remove(‘192.168.196.128:27018‘)
    • After shutting down the primary server and restarting, you will find that the original slave server has changed from the server, the newly started server (the original slave server) to the slave server

MongoDB replication (Replica set)

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.