MongoDB Master-Slave mode deployment

Source: Internet
Author: User
Tags mongodb client mongodb server

MongoDB deployment adopts master-slave mode, the main node is: Peiwo-turn4, from node to Peiwo-proxy

1, create deployment User and deployment path, log, data storage path

Perform the following actions on the Peiwo-proxy host

Groupadd MongoDB

Useradd-g mongodb-d/usr/local/mongodb MongoDB

Mkdir-p/data0/mongodb/logs #日志文件存放路径

Mkdir-p/data0/mongodb/data/master #数据文件存放路径

Mkdir-p/data0/mongodb/pid #pid文件存放路径

Mkdir-p/usr/local/mongodb/etc #配置文件存放路径

Log, data storage path authorization: Chown-r Mongodb.mongodb/data0/mongodb


Perform the following actions on the PEIWO-TURN4 host

Groupadd MongoDB

Useradd-g mongodb-d/usr/local/mongodb MongoDB

Mkdir-p/data1/mongodb/logs #日志文件存放路径

Mkdir-p/data1/mongodb/data/master #数据文件存放路径

Mkdir-p/data1/mongodb/pid #pid文件存放路径

Mkdir-p/usr/local/mongodb/etc #配置文件存放路径

Log, data storage path authorization: Chown-r Mongodb.mongodb/data1/mongodb


2. Download the software and unzip it

Curl-o http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-1.6.3.tgz && TAR-ZXVF mongodb-linux-x86_ 64-1.6.3.tgz-c/usr/local/mongodb


3, the new configuration file under/usr/local/mongodb/etc

The PEIWO-TURN4 host configuration file is as follows: master.conf

Dbpath=/data1/mongodb/data/master

Logpath=/data1/mongodb/logs/master.log

Pidfilepath=/data1/mongodb/pid/master.pid

Directoryperdb=true

Logappend=true

port=21000

Master = True

source=10.144.160.124:21000

oplogsize=100

Fork=true

Noprealloc=true



The peiwo-proxy host configuration file is as follows: slave.conf

Dbpath=/data0/mongodb/data/slave

Logpath=/data0/mongodb/logs/slave.log

Pidfilepath=/data0/mongodb/pid/slave '. PID

Directoryperdb=true

Logappend=true

port=21000

Slave = True

source=10.144.145.235:21000

oplogsize=100

Fork=true

Noprealloc=true

Autoresync=true


4. Start/stop/Process View

Login to MongoDB server, switch to MongoDB User: Su-mongodb

Start process:./start.sh

Stop process: To end MongoDB, you can killall Mongod, or./mongo in the console, use Admin, and then Db.shutdownserver () to end the MongoDB server. Direct kill-9 will cause the MONGO to exit abnormally and may cause data corruption problems.

View process Run Status:./ps.sh

View log:./taillog.sh

Login MongoDB client:./connect.sh





Master and Slave daily operation and Maintenance command:



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

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

2. Status Query

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


3, query the main library address on the cluster library

> Use local;

Switched to DB Local

> Db.sources.find ();

{"_id": ObjectId ("5551769ddf22026a698a271d"), "host": "10.144.145.235:21000", "source": "Main", "Syncedto": Timestamp (1431510735, 1)}


4. Check the status of each collection

Db.printcollectionstats ();


5. View master-slave replication status

Db.printreplicationinfo ();


6. Test Master and slave

Create a new database on the primary server

MONGO--port 21000

>show DBS

>use elaindb

>db.blog.save ({title: "new Article"})

Viewing synchronized data from a server

MONGO--port 21000

The operation to insert data from the library will prompt not master

> Use elaindb;

Switched to DB elaindb

> Db.blog.find ();

{"_id": ObjectId ("4e9174b48443c8ef12b30c56"), "title": "New Article"}

To view master-slave synchronization information:

> 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.01hrs)


>db.printslavereplicationinfo ();

This is the synchronized data, the test is successful.



7, Slave replacement Master

If the primary server 10.144.145.235 down,

You need to replace the master service with a 10.144.160.124 machine.


The steps are as follows:

Stop 10.144.160.124 process (Mongod)

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

#删除 local.* in the 10.144.160.124 data Catalog

Rm-rf/data1/mongodb/data/master//local.*

#以--master mode start 10.144.160.124




Appendix II, Switching Master/slave roles

Switch between the main library 10.144.145.235 and the role from the library 10.144.160.124

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



This article is from the "My Ops Time" blog, so be sure to keep this source http://aaronsa.blog.51cto.com/5157083/1741516

MongoDB Master-Slave mode deployment

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.