Mango knowledge supplement (2) MongoDB master-slave synchronization preliminary Learning

Source: Internet
Author: User

Primary learning of MongoDB master-slave Synchronization

MongoDB master-slave Synchronization

MongoDB master-slave synchronization is very simple. Host startup declares that you are the host. when starting the slave, you only need to declare that you are the slave,
Enter the Host IP address and port to start.

Data Synchronization achieves read/write splitting. when the pressure is high, you can set too many slave machines to separate the read pressure. Master-slave mode
Data Replication and replica pairs Mode Server
Automatically take over from the opportunity in case of power failure or damage, and upgrade to the master server. After the host is repaired, it becomes a slave. When the slave fails
The system automatically switches to the previous host. There is also an authentication mechanism for master-slave authentication. You can create user information.

A) In mongod, the master and slave parameters are as follows:
-- Master Host Mode
-- Slave mode
-- Source Arg specifies the host information on the slave machine <server: Port>
-- Only Arg can specify a database on the slave machine for replication.
-- Slavedelay Arg synchronization delay time, in seconds
-- Autoresync automatically synchronizes all documents on the master server.

Test Environment
The Master/Slave server storage path is/data/DB
Master Server IP Address: 192.168.1.51
Slave Server IP Address: 192.168.1.52

B) Start the experiment.

Note that the time of the two servers must be set to the same. It is best to set the scheduled synchronization time.
1. Start Master/Slave servers respectively
Start the master server and use the default port 27017
./Mongod-dbpath/data/DB -- master
Start slave server port 10002
./Mongod-dbpath/data/DB -- source 192.168.1.51: 27017 -- slave -- Port 27017 -- autoresync -- slavedelay 1
If the connection from the master server is successful after startup, you can view the copied content.
When the data on the slave server is not the latest, the autoresync and slavedelay parameters are used.
When the synchronization system is started, a large amount of information is displayed. You can use ">/dev/null &" to block log information.

2. Test master-slave Synchronization
Create a database on the master server
./Mongo
> Use mytest
> DB. mytest. Save ({"context": "I am here! "})
> DB. mytest. Save ({"context": "Hello World "})
> DB. mytest. Find ()
Two pieces of data can be viewed.

View synchronized data on the server
./Mongo
MongoDB shell version: 1.8.1
Connecting to: Test
> Show DBS
Admin
Local
Test
> Use mytest
Switched to DB testdb
> DB. mytest. Find ()
Two pieces of data have been synchronized by querying.

Delete the "Hello World" record. The delete operation can only be performed on the host.
> DB. mytest. Remove ({"context": "Hello World "})
Go to the master-slave machine again and check that the record has been deleted. Data Synchronization is complete.

3. Test slave database downtime data loss
Data loss during slave database downtime requires data synchronization from the backup host to the host.

Stop the host from deleting data. (Directly go to the data directory Rm-RF)
Change the backup machine to the host for running, and then fix the host to start the host as the backup machine for running.

Operate on the slave machine and stop the slave process (mongod ). Delete local. * from the slave data directory .*. Start in Host Mode
. After the previous host is repaired, it can be started in the slave mode.

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.