MongoDB Master-Slave Mode Chapter

Source: Internet
Author: User
Tags mongodb server

Master-Slave Mode architecture diagram:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/75/79/wKioL1Y6AzPAzanDAABxoQZ_Mk4841.jpg "title=" 1.png " alt= "Wkiol1y6azpazandaabxoqz_mk4841.jpg"/>

Master-Slave Configuration installation:

10.10.20.160 (Master)-->10.10.20.161 (slave)

Master :

Tar zxvf mongodb-linux-x86_64-2.2.2.tgz

Ln-s mongodb-linux-x86_64-2.2.2 MongoDB

Mkdir-p/u01/mongodata/db/geomaster/

Mkdir-p/u01/mongodata/log/

/usr/local/mongodb/bin/mongod--fork--port40000--dbpath/u01/mongodata/db/geomaster/--logpath/u01/mongodata/log/ Geomaster.log--logappend–master

Slave :

Tar zxvf mongodb-linux-x86_64-2.2.2.tgz

Ln-s mongodb-linux-x86_64-2.2.2 MongoDB

Mkdir-p/u01/mongodata/db/geomaster/

Mkdir-p/u01/mongodata/log/

/usr/local/mongodb/bin/mongod--fork--port40000--dbpath/u01/mongodata/db/geoslave/--logpath/u01/mongodata/log/ Geosalve.log–logappend--slave--source 10.10.20.160:40000

Parameter description:

Mongod for the MongoDB Server program, the startup parameters used mainly have the following several

--fork Fork out a server -side daemon process

--port Server Listener Port

--dbpath data File directory

--logpath log file path

The--logappend log is appended to the log file instead of overwriting the write

Mongod parameters that can be set under the master and slave conditions are:

--master Master Mode

--salve Salve Mode

--source specified master<server:port> in Salve mode

--only Copy only one database

--salvedelay synchronizing delays from the library from the main library

--autoresync If the data from the library is not synchronized automatically

After the master-slave configuration is complete test:

Master :

/usr/local/mongodb/bin/mongo-port 40000

Use test;

Db.createcollection ("table1");

Db.test.table1.insert ({tag: "Test"});

Db.test.table1.find ();

slave :

/usr/local/mongodb/bin/mongo-port 40000

Db.test.table1.find ();

There is data indicating that synchronization was successful

Slave Threads can also be seen with db.currentop () on master after synchronization

Master-Slave related commands:

Db.ismaster (): Master and slave can execute, use this command to determine whether master

Db.getreplicationinfo (): The Lord executes, obtains the master-slave information

Db.printreplicationinfo (): Master and slave can execute, get master-slave information

Db.printslavereplicationinfo (); : Execute from Library, view master/slave delay

Synchronization Failure Handling method:

If the synchronization fails, you can synchronize all data using the following command:

There is a problem with synchronization, the log will have the following record

Tuefeb 16:31:45 [Replslave] all sources dead:data too stale haltedreplication, sleeping for 5 seconds

is every 5 seconds retry, has been retrying, then if you want to solve can only execute the following command all synchronized

Use admin

Db.runcommand ({"Resync": 1})

can also be added when starting from the library--autoresync parameter

Synchronization principle:

synchronization is the master to record changes to the data in Oplog, and then slave crawl master oplog execution. From this point of view Oplog function and mysql mysql-bin. functions similarly. the Mysql-bin exists in the form of a binary log, but Oplog is present as a mongodb table in the local library table named Oplog. $main, the table is a circular write shape, so it does not have to be cleaned up regularly.

Modify oplog Size:

We first launch Mongodb when the service is not set Oplog 5% oplogsize has already existed, so the modified size start error, Tue 15:43:19[initandlisten] cmdline oplogsize (ten) different than existing (1779) /em> mongodb data directory local.* Delete, see: http://api.mongodb.org/wiki/current/Halted%20Replication.html

because after deletion Oplog is rebuilt, Slave oplog The start point is small, So you can only do full synchronization from the library to continue syncing. But there is a problem, if the main library data is very much, do a full synchronization is a very time-consuming thing, moreover, the data is not lost. So here's a tip:

    1. Close slave 's MONGO service

    2. Master on after we delete local.* , do not rush to start the service, first use the Linux date command to change the system time, so that the time is less than the last time slave on the synchronization , that is, slave execution db.printslavereplicationinfo () See the time

    3. Then start the main library and from the library to continue to synchronize

This article is from the "Zhangdh Open Space" blog, so be sure to keep this source http://linuxblind.blog.51cto.com/7616603/1709782

MongoDB Master-Slave Mode Chapter

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.