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, the common architecture mode is master-slave

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, the common architecture mode is master-slave

[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 listen dtcp0 0 0.0.0.0: 28017 0.0.0.0: * LISTEN1923/mongod tcp0 0 0.0.0.0: 27017 0.0.0.0: * LISTEN1923/mongod

[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.0771484375 GBtest 0.203125 GB>

Master log

Slave log

After the database is created, the master and slave logs show that the master can synchronize the master data (applied 1 operations) from the slave connections ). 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

[Root @ localhost ~] # MongoMongoDB shell version: 2.4.9-rc0connecting to: test> db. printReplicationInfo () configured oplog size: 2048 MBlog length start to end: 1494 secs (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

[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 instart) $ PROG-f $ CONF &>/dev/nullecho" Starting mongodb: [OK] "; stop) $ PROG-f $ CONF -- shutdown &>/dev/nullecho "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 keep this source

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.