MongoDB master-slave Replication

Source: Internet
Author: User

In the production environment, a single database can no longer meet business requirements. Master-slave database replication is commonly used in the architecture. It is used for backup, high availability, and read/write splitting to improve data processing performance and redundancy, common architecture modes include one master, one master, multiple slaves, and two masters. I will not talk much about the features of the MongoDB database, as Baidu says in more detail. Our architecture is one master and one slave.

Environment Description:

OS: CentOS6.5 _ x64

Master): 192.168.0.201

From Slave): 192.168.0.202

The Master/Slave database directory is in/usr/local/mongodb/data

Log directory/usr/local/mongodb/logs

1. Primary mongodb Configuration

[root@localhost ~]# mkdir /usr/local/mongodb/data[root@localhost ~]# mkdir /usr/local/mongodb/logs[root@localhost ~]# mkdir /usr/local/mongodb/conf

[Root @ localhost ~] # Vi/usr/local/mongodb/conf/mongod. confport = 27017 # port fork = true # Run logpath =/usr/local/mongodb/logs/mongodb as a daemon. log # log File logappend = true # log output mode dbpath =/usr/local/mongodb/data # database location maxConns = 1024 # maximum number of database connections master = true # master mode oplogSize = 2048 # log rolling, unit: M

2. Configure from Mongodb

[root@localhost ~]# mkdir /usr/local/mongodb/data[root@localhost ~]# mkdir /usr/local/mongodb/logs[root@localhost ~]# mkdir /usr/local/mongodb/conf

[Root @ localhost ~] # Vi/usr/local/mongodb/conf/mongod. confport = 27017 fork = truelogpath =/usr/local/mongodb/logs/mongodb. loglogappend = truedbpath =/usr/local/mongodb/datamaxConns = 1024 slave = true # source = 192.168.0.201: 27017 # specify the master node dbautoresync = true # automatic synchronization

3. Add environment variables and start Mongodb

[Root @ localhost ~] # Echo "PATH = $ PATH:/usr/local/mongodb/bin">/etc/profile [root @ localhost ~] # Source/etc/profile [root @ localhost ~] # Mongod-f/usr/local/mongodb/conf/mongod. conf # Start [root @ localhost ~] # Netstat-tupln | grep primary dtcp 0 0 0.0.0.0: 28017 0.0.0.0: * LISTEN 1923/primary D tcp 0 0 0.0.0.0: 27017 0.0.0.0: * LISTEN 1923/primary d

# The log information on the master database indicates that the master database allows connection of 192.168.0.202 from Port 45458 of the database.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140107/0R044N94-0.jpg "title =" qq 40105110532.jpg "alt =" wkiol1lizs7yui5zaadooud2s661.jpg "/>

# Synchronize the master database 192.168.0.201 from the log information

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140107/0R0442Q3-1.jpg "title =" qq 40105110336.jpg "alt =" wkiom1lizsfzevaac24wiijru519.jpg "/>

4. Test Mongodb master-slave Replication

# Create a database test with the Set Name test. Insert a field AGE: 18. Then, check that the data has been synchronized from the database showdbs. The syntax is very different from that of Mysql.

[root@localhost ~]# mongoMongoDB shell version:2.4.9-rc0> use test> db.test.save({AGE:18})> db.test.find(){ "_id" :ObjectId("52addd66124c02eb8b2d1a5a"), "AGE" : 18 }> show dbslocal   2.0771484375GBtest    0.203125GB>

Master log

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140107/0R04425D-2.jpg "title =" 1.png" alt = "wKiom1LIzdbh614lAADq-fpf3yk473.jpg"/>

Slave log

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140107/0R0444056-3.jpg "title =" 2.png" alt = "wKiom1LIzf_w7fOIAAClPWySRiM169.jpg"/>

After the database is created, the master and slave logs show that the master can synchronize the master data from applied 1 operations ). The Operation Records of the primary database are OpLog logs, indicating that the principle of the master-slave database is to regularly obtain oplog records from the primary server and then execute them on the local machine.

5. view master-slave replication status

# View master

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140107/0R044LF-4.jpg "title =" 3.png" alt = "wKioL1LIziuC2LXOAAGv_bRs8cA553.jpg"/>

[root@localhost ~]# mongoMongoDB shell version:2.4.9-rc0connecting to: test>db.printReplicationInfo()configured oplog size:   2048MBlog length start to end:1494secs (0.42hrs)oplog first event time:  Sun Dec 15 2013 10:55:37 GMT-0500 (EST)oplog last event time:   Sun Dec 15 2013 11:20:31 GMT-0500 (EST)now:                     Sun Dec 15 2013 11:20:33GMT-0500 (EST)>

# Viewing from

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140107/0R0445635-5.jpg "title =" 4.png" alt = "wKioL1LIzjehSOsgAAFfgtITTFE089.jpg"/>

[root@localhost ~]# mongoMongoDB shell version: 2.4.9-rc0connecting to: test>db.printReplicationInfo()this is a slave, printingslave replication info.source:   192.168.0.201:27017         syncedTo: Sun Dec 15 2013 11:18:31GMT-0500 (EST)                 = 21 secs ago (0.01hrs)>

# Disabling Mongodb Databases

[Root @ localhost ~] # Mongod-f/usr/local/mongodb/conf/mongod. conf -- shutdown

# Write a simple SysV management script

[root@localhost ~]# vim /etc/init.d/mongod#!/bin/bash#chkconfig: 35 80 20#description: Mongodb service control scriptPROG="/usr/local/mongodb/bin/mongod"CONF="/usr/local/mongodb/conf/mongod.conf"case $1 in        start)        $PROG -f $CONF &> /dev/null        echo "Starting mongodb:                 [ OK ]"        ;;        stop)        $PROG -f $CONF --shutdown &> /dev/null        echo "Shutting down mongodb:                    [ OK ]"        ;;        restart)        $0 stop        $0 start        ;;        *)        echo "Usage: $0 {start|stop|restart}"esac

[root@localhost ~]# chmod +x /etc/init.d/mongod[root@localhost ~]# chkconfig --add mongod[root@localhost ~]# chkconfig mongod on

Mongdb master-slave replication is complete. It is simpler and more flexible than Myslq.


This article is from the "Penguin" blog, please be sure to keep this http://going.blog.51cto.com/7876557/1348589

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.