Mongodb主从配置

MongoDB 主从复制与 Mysql 类似。
在主服务器上加--master 选项启动。
[root@Mongodb bin]# /Apps/mongo/bin/mongod --dbpath=/data/db  --master --port=27018  --oplogSize 64
Tue Jul 16 19:17:44 [initandlisten] MongoDB starting : pid=30892 port=27018 dbpath=/data/db master=1 32-bit 


** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
**       see http://blog.mongodb.org/post/137788967/32-bit-limitations
**       with --dur, the limit is lower


Tue Jul 16 19:17:44 [initandlisten] db version v1.8.1, pdfile version 4.5
Tue Jul 16 19:17:44 [initandlisten] git version: a429cd4f535b2499cc4130b06ff7c26f41c00f04
Tue Jul 16 19:17:44 [initandlisten] build sys info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_37
Tue Jul 16 19:17:44 [initandlisten] waiting for connections on port 27018
Tue Jul 16 19:17:44 [initandlisten] ******
Tue Jul 16 19:17:44 [initandlisten] creating replication oplog of size: 64MB...
Tue Jul 16 19:17:44 [FileAllocator] allocating new datafile /data/db/local.ns, filling with zeroes...
Tue Jul 16 19:17:57 [FileAllocator] done allocating datafile /data/db/local.ns, size: 16MB,  took 13.032 secs
Tue Jul 16 19:17:57 [FileAllocator] allocating new datafile /data/db/local.0, filling with zeroes...
Tue Jul 16 19:18:06 [FileAllocator] done allocating datafile /data/db/local.0, size: 16MB,  took 9.22 secs
Tue Jul 16 19:18:06 [FileAllocator] allocating new datafile /data/db/local.1, filling with zeroes...
Tue Jul 16 19:18:16 [FileAllocator] done allocating datafile /data/db/local.1, size: 32MB,  took 9.628 secs
Tue Jul 16 19:18:16 [FileAllocator] allocating new datafile /data/db/local.2, filling with zeroes...
Tue Jul 16 19:18:37 [FileAllocator] done allocating datafile /data/db/local.2, size: 128MB,  took 21.691 secs
Tue Jul 16 19:18:37 [initandlisten] ******
Tue Jul 16 19:18:37 [websvr] web admin interface listening on port 28018
Tue Jul 16 19:45:16 [initandlisten] connection accepted from 192.168.155.199:58930 #1
Tue Jul 16 19:45:16 [conn1] query local.oplog.$main reslen:80 nscanned:2 { ts: { $gte: Timestamp 1373975112000|1 } }  nreturned:1 132ms
Tue Jul 16 19:45:17 [slaveTracking] building new index on { _id: 1 } for local.slaves
Tue Jul 16 19:45:17 [slaveTracking] done for 0 records 0.001secs
--oplogSize 指定操作的日志,指定小一点 64 M。
从服务器上加 --slave选项启动并指定 master 的地址。  
注意:防火墙是否关闭等原因导致连接不上主库;
/etc/init.d/iptables status  --查看状态;
/etc/init.d/iptables stop    --关闭防火墙;

[root@Mongodb-B mongo]# /Apps/mongo/bin/mongod  --port=27019  --slave  --journal  --source 192.168.155.198:27018 --dbpath=/data/db/
Tue Jul 16 19:46:51.522 [initandlisten] MongoDB starting : pid=12538 port=27019 dbpath=/data/db/ slave=1 32-bit host=Mongodb-B
Tue Jul 16 19:46:51.522 [initandlisten] 
Tue Jul 16 19:46:51.522 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Tue Jul 16 19:46:51.522 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
Tue Jul 16 19:46:51.522 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
Tue Jul 16 19:46:51.523 [initandlisten] 
Tue Jul 16 19:46:51.523 [initandlisten] db version v2.4.4
Tue Jul 16 19:46:51.523 [initandlisten] git version: 4ec1fb96702c9d4c57b1e06dd34eb73a16e407d2
Tue Jul 16 19:46:51.523 [initandlisten] build info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Tue Jul 16 19:46:51.523 [initandlisten] allocator: system
Tue Jul 16 19:46:51.523 [initandlisten] options: { dbpath: "/data/db/", journal: true, port: 27019, slave: true, source: "192.168.155.198:27018" }
Tue Jul 16 19:46:51.528 [initandlisten] journal dir=/data/db/journal
Tue Jul 16 19:46:51.528 [initandlisten] recover : no journal files present, no recovery needed
Tue Jul 16 19:46:51.648 [FileAllocator] allocating new datafile /data/db/local.ns, filling with zeroes...
Tue Jul 16 19:46:51.648 [FileAllocator] creating directory /data/db/_tmp
Tue Jul 16 19:46:51.776 [FileAllocator] done allocating datafile /data/db/local.ns, size: 16MB,  took 0.125 secs
Tue Jul 16 19:46:51.777 [FileAllocator] allocating new datafile /data/db/local.0, filling with zeroes...
Tue Jul 16 19:46:51.903 [FileAllocator] done allocating datafile /data/db/local.0, size: 16MB,  took 0.125 secs
Tue Jul 16 19:46:51.905 [initandlisten] command local.$cmd command: { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0  reslen:37 257ms
Tue Jul 16 19:46:51.906 [initandlisten] waiting for connections on port 27019
Tue Jul 16 19:46:51.907 [websvr] admin web console waiting for connections on port 28019
Tue Jul 16 19:46:52.911 [replslave] build index local.sources { _id: 1 }
Tue Jul 16 19:46:52.912 [replslave] build index done.  scanned 0 total records. 0.001 secs
Tue Jul 16 19:46:52.912 [replslave] repl: syncing from host:192.168.155.198:27018
Tue Jul 16 19:46:52.931 [replslave] build index local.me { _id: 1 }
Tue Jul 16 19:46:52.932 [replslave] build index done.  scanned 0 total records. 0 secs
Tue Jul 16 19:47:05.762 [replslave] repl:   applied 1 operations
Tue Jul 16 19:47:05.763 [replslave] repl:  end sync_pullOpLog syncedTo: Jul 16 19:45:22 51e53252:1
Tue Jul 16 19:47:05.763 [replslave] repl: sleep 1 sec before next pass
Tue Jul 16 19:47:06.766 [replslave] repl: syncing from host:192.168.155.198:27018
Tue Jul 16 19:47:15.756 [replslave] repl:   applied 1 operations
Tue Jul 16 19:47:15.757 [replslave] repl:  end sync_pullOpLog syncedTo: Jul 16 19:45:32 51e5325c:1
Tue Jul 16 19:47:15.757 [replslave] repl: syncing from host:192.168.155.198:27018
Tue Jul 16 19:47:25.755 [replslave] repl:   applied 1 operations
Tue Jul 16 19:47:25.756 [replslave] repl:  end sync_pullOpLog syncedTo: Jul 16 19:45:42 51e53266:1
Tue Jul 16 19:47:25.756 [replslave] repl: syncing from host:192.168.155.198:27018
Tue Jul 16 19:47:36.344 [replslave] repl:   applied 1 operations
Tue Jul 16 19:47:36.344 [replslave] repl:  end sync_pullOpLog syncedTo: Jul 16 19:45:52 51e53270:1
Tue Jul 16 19:47:36.345 [replslave] repl: syncing from host:192.168.155.198:27018
Tue Jul 16 19:47:45.756 [replslave] repl:   applied 1 operations
Tue Jul 16 19:47:45.756 [replslave] repl:  end sync_pullOpLog syncedTo: Jul 16 19:46:02 51e5327a:1
Tue Jul 16 19:47:45.757 [replslave] repl: syncing from host:192.168.155.198:27018
Tue Jul 16 19:47:47.924 [replslave] resync: dropping database test
Tue Jul 16 19:47:47.930 [replslave] removeJournalFiles
Tue Jul 16 19:47:47.931 [replslave] resync: cloning database test to get an initial copy
Tue Jul 16 19:47:48.235 [FileAllocator] allocating new datafile /data/db/test.ns, filling with zeroes...
Tue Jul 16 19:47:49.538 [FileAllocator] done allocating datafile /data/db/test.ns, size: 16MB,  took 1.303 secs
Tue Jul 16 19:47:49.539 [FileAllocator] allocating new datafile /data/db/test.0, filling with zeroes...
Tue Jul 16 19:47:51.056 [FileAllocator] done allocating datafile /data/db/test.0, size: 16MB,  took 1.516 secs
Tue Jul 16 19:47:51.058 [FileAllocator] allocating new datafile /data/db/test.1, filling with zeroes...
主从起来,开始复制;
[root@Mongodb-B db]# /Apps/mongo/bin/mongo --port=27019
MongoDB shell version: 2.4.4
connecting to: 127.0.0.1:27019/test
Server has startup warnings: 
Tue Jul 16 19:46:51.522 [initandlisten] 
Tue Jul 16 19:46:51.522 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Tue Jul 16 19:46:51.522 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
Tue Jul 16 19:46:51.522 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
Tue Jul 16 19:46:51.523 [initandlisten] 
> show dbs
local   0.03125GB
tes     0.0625GB
test    0.0625GB
> use test
switched to db test
> show tables;
song
system.indexes
> db.song.find()
{ "_id" : ObjectId("51e532826930261d26cc60aa"), "name" : "xixi" }

原文地址:https://www.cnblogs.com/javawebsoa/p/3194248.html