MONGONDB Master-slave replication

Source: Internet
Author: User
Tags mongo shell

MongoDB installation and master-slave replication

One, MongoDB installation
1. Download the Mongodblinux version (note the difference between 32-bit and 64-bit)

wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.0.7.tgz



2. Unzip

Tar xvf mongodb-linux-x86_64-2.0.7.tgzmkdir/usr/local/mongodbmv mongodb-linux-x86_64-2.0.7/*/usr/local/mongodb


3. Create a database file directory
If there is a separate partition, it is better to mount the MongoDB database file directory to a separate partition

Mkdir-p/mongo/data


Create a MongoDB log file

Mkdir/var/log/mongodbtouch/var/log/mongodb/mongodb.log


4. Create a soft connection to the MONGO command
The script file in the MONGO Bin directory can be used directly

Ln-s/usr/local/mongodb/bin/*/usr/sbin
The purpose of the tools under MongoDB Bin: Mongod: Database server, similar to mysqld, each instance starts a process, can fork for Daemon run MONGO: Client command-line tool, similar to Sqlplus/mysql, is actually a JS interpreter , Support JS Syntax Mongodump/mongorestore: Import data into a Bson format File/restore Bson file to a database, similar to xtracbackupmongoexport/ Mongoimport: Export collection to JSON/CSV format data/import data to a database, similar to mysqldump/ Mysqlimportbsondump: Dumps Bson formatted files into JSON-formatted data mongos: Shard route, if sharding function is used, The application is connected to MONGOs instead of MONGODMONGOFILES:GRIDFS management tool Mongostat: real-time monitoring tool


5 Start MongoDB (two methods)
Method one: direct command line startup

Mongod–-port 27017--fork--logpath=/var/log/mongodb/mongodb.log--logappend--dbpath=/mongo/db



Method Two: (Configuration file start: recommended) Mkdir/usr/local/mongodb/confcd/usr/local/mongodb/confvim mongod.conf port=27017 #端口号 fork=true # Runs as a daemon, creating a server process Logpath=/var/log/mongodb/mongodb.log #日志输出文件路径 logappend=true #日志输出方式 dbpath=/mongo/db #数据库路径 Shar Dsvr=true #设置是否分片 maxconns=600 #数据库的最大连接数 start: mongod-f/usr/local/mongodb/conf/mongod.conf


6. Verification
Whether ports 27017 and 28017 are turned on

NETSTAT-NULTP MONGO command into MONGO Shell


Second, MongoDB master-slave replication configuration
Master-slave replication is the most commonly used method of replication for MongoDB, which is flexible. Can be used for backup, fault recovery, read extension, etc.
The most basic way to set up is to establish a master node and one or more slave nodes, each from the node to know the address of the master node.
Here we use a master one from the implementation of MONGODB replication

1. Host distribution

Mongodb-master 10.48.255.244 mastermongodb-slave 10.48.255.243 Slave


2. Apply the above installation process to MongoDB two hosts, the configuration file changes slightly

On Mongodb-master, the configuration file increases the master=trueoplogsize=2048 #类似于mysql的日志滚动, in units of M


On Mongodb-slave, the configuration file increases:

slave=truesource=10.48.100.1:27017 #指定主mongodb serverslavedelay=10 #延迟复制, units in seconds Autoresync=true # Request synchronization of data from the primary server when the data from the server is found to be not up to date


Third, test master-Slave usability
Start the Mongodb:mongod-f/usr/local/mongodb/conf/mongod.conf on both hosts
1. Read the log message
Lord's Diary:

# tail/var/log/mongodb/mongodb.log Thu 17:59:44 [Initandlisten] connection accepted from 10.48.255.243:38034 #1Thu 17:59:57 [conn1] End connection 10.48.255.243:38034thu 18:00:08 [Initandlisten] connection accepted from 10 .48.255.243:38035 #2Thu 18:00:43 [Clientcursormon] Mem (MB) res:30 virt:8749 mapped:4302


From the previous log

# tail /var/log/mongodb/mongodb.log thu aug 16 18:00:28 [replslave]  REPL: FROM HOST:10.48.255.244:27017THU AUG 16 18:00:28 [REPLSLAVE] REPL:    APPLIED 1 OPERATIONSTHU AUG 16 18:00:28 [REPLSLAVE] REPL:    syncedto: aug 16 18:00:17 502d3531:1thu aug 16 18:00:28  [replslave] waiting until: 1345140038 to continueThu Aug 16  18:00:28 [replslave] repl: sleep 10 sec before next passthu aug  16 18:00:38 [replslave] repl: from host:10.48.255.244:27017thu aug 16  18:00:38 [replslave] repl:   applied 1 operationsthu aug 16  18:00:38 [replslave] repl:   syncedTo: Aug 16 18:00:27  502d353b:1thu aug  16 18:00:38 [replslave] waiting until: 1345140048 to continuethu aug  16 18:00:38 [replslave] repl: sleep 10 sec before next pass


The above information knows that the master and slave can establish communication

2. Below we create the database in our Lord and insert the collection document to see if it is synchronized
On the primary server:

# Mongomongodb Shell version:2.0.7connecting to:test> show dbslocal 4.201171875gb> use xinswitched to DB Xin> ; Db.test.save ({title: "Just Test"}) > Db.test.find () {"_id": ObjectId ("502D3643C5664CA66103A7CF"), "title": "Just Test "}> Show dbslocal 4.201171875GBxin 0.203125gb>

Note: Xin is the database name, test is the collection name, {title: "Just Test"} is a document
MongoDB using use to create a database of course, you can switch the database, and MySQL is a big difference.

Let's take a look at the log.
Logs on the primary server:

Thu aug 16 18:04:51 [fileallocator] creating directory /mongo/db/_tmpthu  aug 16 18:04:51 [fileallocator] done allocating datafile /mongo/db/ xin.ns, size: 16mb,  took 0.001 secsthu aug 16 18:04:53 [ Fileallocator] allocating new datafile /mongo/db/xin.0, filling with zeroes ... thu aug 16 18:04:53 [fileallocator] done allocating datafile /mongo/db /xin.0, size: 64mb,  took 0.034 secsthu aug 16 18:04:53 [ Fileallocator] allocating new datafile /mongo/db/xin.1, filling with zeroes ... thu aug 16 18:04:53 [fileallocator] done allocating datafile /mongo/db /xin.1, size: 128mb,  took 0.04 secsthu aug 16 18:04:53 [ conn3] build index xin.test { _id: 1 }thu aug 16 18:04:53 [conn3]  build index done 0 records 0.012 secsthu aug 16 18:04:53 [ conn3] insert xin.test 2892msthu aug 16 18:05:07 [initandlisten]  connection accepted from 10.48.255.243:60173  #4Thu  aug 16 18:05:08 [ conn4] end connection 10.48.255.243:60173

 
Log from the server:

Thu aug 16 18:05:04 [replslave] repl: from host:10.48.255.244:27017thu  aug 16 18:05:04 [replslave] resync: dropping database xinthu aug  16 18:05:04 [replslave] removejournalfilesthu aug 16 18:05:04 [ Replslave] resync: cloning database xin to get an initial copythu  aug 16 18:05:04 [fileallocator] allocating new datafile /mongo/db/ Xin.ns, filling with zeroes ... Thu aug 16 18:05:04 [fileallocator] creating directory /mongo/db/_tmpthu  aug 16 18:05:04 [fileallocator] done allocating datafile /mongo/db/ xin.ns, size: 16mb,  took 0 secsthu aug 16 18:05:04 [ Fileallocator] allocating new datafile /mongo/db/xin.0, filling with&nbSp;zeroes ... thu aug 16 18:05:04 [fileallocator] done allocating datafile /mongo/db /xin.0, size: 64mb,  took 0 secsthu aug 16 18:05:04 [ Fileallocator] allocating new datafile /mongo/db/xin.1, filling with zeroes ... thu aug 16 18:05:04 [fileallocator] done allocating datafile /mongo/db /xin.1, size: 128mb,  took 0.001 secsthu aug 16 18:05:04 [ replslave] build index xin.test { _id: 1 }thu aug 16  18:05:04 [replslave] build index done 1 records 0 secsthu aug  16 18:05:04 [replslave] resync: done with initial clone for  db: xinthu aug 16 18:05:04 [replslave] repl:   applied  1 operations


As you can see from the above log, the Xin database has been synchronized to the server.

3. We enter the MongoDB shell from the server and confirm

# Mongomongodb Shell version:2.0.7connecting to:test> show dbslocal 0.203125GBxin 0.203125gb>> use Xinswi tched to DB xin> Db.test.find () {"_id": ObjectId ("502D3643C5664CA66103A7CF"), "title": "Just Test"}



We do not create much database data, why there is 0.2G, this is because MongoDB pre-allocated the database space, which makes the next time to insert data into the database faster

4. Viewing the collection (collection) status from the server

> db.printcollectionstats () system.indexes{"ns": "Xin.system.indexes", "Count": 1, "size": +, "avgobjsize": 64, " Storagesize ": 4096," numextents ": 1," nindexes ": 0," lastextentsize ": 4096," Paddingfactor ": 1," Flags ": 0," Totalindexsiz E ": 0," indexsizes ": {}," OK ": 1}---test{" ns ":" Xin.test "," Count ": 1," size ":," avgobjsize ":," storagesize ": 8192 , "numextents": 1, "nindexes": 1, "lastextentsize": 8192, "Paddingfactor": 1, "Flags": 1, "totalindexsize": 8176, "Indexsiz Es ": {" _id_ ": 8176}," OK ": 1}---

To view the status of Master-slave replication from the server

> Db.printreplicationinfo () This is a slave, printing slave replication Info.source:10.48.255.244:27017syncedto:thu 18:33:27 gmt+0000 (UTC) = secs ago (0.01hrs) > > Db.printslavereplicationinfo () source:10.48.255.244: 27017syncedto:thu 18:34:37 gmt+0000 (UTC) = secs ago (0.01hrs)



Ok,mongodb's master-slave copy is done!


This article from "Born for Technology" blog, reproduced please contact the author!

MONGONDB Master-slave replication

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.