MongoDB Master-Slave switching (excellent)

Source: Internet
Author: User
Tags mkdir mongodb mysql in
Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://elain.blog.51cto.com/3339379/760266

Master-Slave synchronization:

MD01 10.0.0.11 Master
MD02 10.0.0.12 Slave
MD03 10.0.0.14 Slave

#建立数据库目录

Mkdir-p/elain/apps/mongodb/mkdir-p/elain/data/mongodb/db/mkdir-p/elain/logs/mongodb/

Note:
1, do not need to copy the complete data like MySQL in the past, Concou boot will be brake copy master library full data.
2, Concou automatically configured as read-only.
3, MongoDB first boot time is longer, need to pay attention to wait.
4, most of the management command in the Admin library to execute the use admin

Related parameters:./mongod--help
--autoresync when the data from the server is found to be not up to date, the request to synchronize data from the master server begins
--slavedelay synchronization delay, Unit: SEC

Master-From
Start as master server

/elain/apps/mongodb/bin/mongod--fork--master--oplogsize=4096--port 27001--dbpath/elain/data/mongodb/db--logpath /elain/logs/mongodb/mongodb.log

As starting from the server and indicating the primary server address. --autoresync to force all data to be synchronized from the primary server

/elain/apps/mongodb/bin/mongod--fork--slave--source 10.0.0.11:27001--port 27001--dbpath/elain/data/mongodb/db-- Logpath/elain/logs/mongodb/mongodb.log

Note: You can add the following common parameters when starting from
--slavedelay #延时复制 units of seconds
--autoresync #自动重新同步
--only #复制指定的数据库, all libraries are copied by default
--oplogsize #主节点的oplog日志大小, the unit is m, it is recommended to set a large point (change oplog size, just stop the main library, delete local.*, and then add--oplogsize=* reboot, * represents the size)

If you find that the master never synchronizes, manually sync from the

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

Status Query

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

#主还是从

Querying the main library address on the Plex library

> Use local;  Switched to DB local > Db.sources.find (); {"_id": ObjectId ("4e9105515d704346c8796407"), "host": "10.0.0.11:27001", "source": "Main", "Syncedto": {"T": 131815 5992000, "I": 1}}

#查看各Collection状态

Db.printcollectionstats ();

#查看主从复制状态

Db.printreplicationinfo ();

Test Master
Create a new database on the primary server

MONGO--port 27001 >show dbs >use elaindb >db.blog.save ({title: "new Article"})

Viewing synchronized data from a server

MONGO--port 27001

#在从库插入数据的操作 will prompt not master

> Use elaindb;  Switched to DB elaindb > Db.blog.find (); {"_id": ObjectId ("4e9174b48443c8ef12b30c56"), "title": "New Article"}

#查看主从同步信息:

> Db.printreplicationinfo ();  This is a slave, printing slave replication info. source:10.0.0.11:27001 Syncedto:sun Oct 18:37:12 gmt+0800 (CST) = secs ago (0.0 1hrs)
>db.printslavereplicationinfo ();

#此为同步过来的数据, Test success

Additional:

Add and remove Sources:
You can specify the master node with--source when you start the slave node, or you can configure the source in the shell.

Start a slave with a port of 27002

/elain/apps/mongodb/bin/mongod--fork--slave--port 27002--dbpath/elain/data/mongodb/testdb--logpath/elain/logs/ Mongodb/mongodb.log

Login:

MONGO--port 27002 [root@md03 ~]# MONGO--port 27002 MongoDB Shell version:2.0.0 connecting To:127.0.0.1:27002/test > Show dbs local (empty)

Add to slave node

>use Local >db.sources.insert ({"Host": "10.0.0.11:27001"});

#查看显示正在同步

> Db.sources.find (); {"_id": ObjectId ("4e9111e1c29bbd9b9cd31d4e"), "host": "10.0.0.11:27001", "source": "Main", "Syncedto": {"T": 131815 6782000, "I": 246}, "Dbsnextpass": {"TestDB": True, "TESTDB1": True}}

#同步完成后

> Db.sources.find (); {"_id": ObjectId ("4e9111e1c29bbd9b9cd31d4e"), "host": "10.0.0.11:27001", "source": "Main", "Syncedto": {"T": 131815 6782000, "I": 256}}

Change Source: (assuming 10.0.0.12 is also the master library)

Db.sources.insert ({"Host": "10.0.0.12:27001"}); Db.sources.remove ({"Host": "10.0.0.11:27001"});

If authentication is used in replication, you need to add the REPL user to the local library to copy

>user Local; > Db.adduser ("Repl", "elain123");


Appendix I, Slave replacement Master
If the primary server 10.0.0.11 down,
You need to replace the master service with a 10.0.0.12 machine.

The steps are as follows:
#停止 10.0.0.12 Process (Mongod)

Kill-2 ' Ps-ef|grep mongod|grep-v Grep|awk ' {print $} '

#删除 local.* in the 10.0.0.12 data Catalog

Rm-rf/elain/data/mongodb/db/local.*

#以--master mode start 10.0.0.12

/elain/apps/mongodb/bin/mongod--fork--master--oplogsize=4096--port 27001--dbpath/elain/data/mongodb/db--logpath /elain/logs/mongodb/mongodb.log

Appendix II, Switching Master/slave roles
Switch between the main library 10.0.0.11 and the role from the library 10.0.0.12

The steps are as follows: (Command slightly)

Pauses the write operation on the main library with the Fsync command.
To close a service from the library
Emptying the local.* file from the library
Restart from library service with-MASTER option
Performs a write operation from the library, initializes the Oplog, and obtains a synchronous start point
Close from the library service, at which point the new local.* file is already available from the library
Close the main library service and replace the local.* file on the main library with a new local.* file from the library (it is recommended to compress and copy first)
Restart from library service with-MASTER option
Restart the main library service by adding a-fastsync option on the option to start slave

reproduced Please specify: Reproduced from http://www.elain.org

This article link address: MongoDB Combat Series III: MongoDB's master-slave deployment

This article is from the Elain blog, so be sure to keep this source http://elain.blog.51cto.com/3339379/760266

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.