#下载 wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.0.4.tgz #解压 tar zxvf mongodb-linux-x86_64-rhel62-3.0.4.tgz mv /root/mongodb-linux-x86_64-rhel62-3.0.4 /usr/local/mongodb #创建目录和日志文件 mkdir /usr/local/mongodb/data touch /usr/local/mongodb/logs #用脚本启动: /etc/init.d/mongodb start|stop|restart #测试 cd /usr/local/mongodb/bin ./mongo use test; #开机启动 echo "/etc/init.d/mongodb start" >> /etc/rc.d/rc.local
主从复制配置
主从复制是mongodb最常用的复制方式,这种方式很灵活.可用于备份,故障恢复,读扩展等. 最基本的设置方式就是建立一个主节点和一个或多个从节点,每个从节点要知道主节点的地址. 这里我们用一主一从实现mongodb的复制 1.主机 mongodb-master 10.48.255.244 master mongodb-slave 10.48.255.243 slave
2.把以上安装过程应用于mongodb这两个主机,配置文件稍加改动 在mongodb-master上,配置文件增加 master=true oplogSize=2048 #类似于mysql的日志滚动,单位m
在mongodb-slave上,配置文件增加: slave=true source=10.48.100.1:27017 #指定主mongodb server slavedelay=10 #延迟复制,单位为秒 autoresync=true #当发现从服务器的数据不是最新时,向主服务器请求同步数据
三、测试主从可用性 在两主机上启动mongodb: mongod -f /usr/local/mongodb/conf/mongod.conf 1.看日志信息 主上日志:
tail /var/log/mongodb/mongodb.log
Thu Aug 16 17:59:44 [initandlisten] connection accepted from 10.48.255.243:38034 #1 Thu Aug 16 17:59:57 [conn1] end connection 10.48.255.243:38034 Thu Aug 16 18:00:08 [initandlisten] connection accepted from 10.48.255.243:38035 #2 Thu Aug 16 18:00:43 [clientcursormon] mem (MB) res:30 virt:8749 mapped:4302
从上日志
tail /var/log/mongodb/mongodb.log
Thu Aug 16 18:00:28 [replslave] repl: from host:10.48.255.244:27017 Thu Aug 16 18:00:28 [replslave] repl: applied 1 operations Thu Aug 16 18:00:28 [replslave] repl: syncedTo: Aug 16 18:00:17 502d3531:1 Thu Aug 16 18:00:28 [replslave] waiting until: 1345140038 to continue Thu Aug 16 18:00:28 [replslave] repl: sleep 10 sec before next pass Thu Aug 16 18:00:38 [replslave] repl: from host:10.48.255.244:27017 Thu Aug 16 18:00:38 [replslave] repl: applied 1 operations Thu Aug 16 18:00:38 [replslave] repl: syncedTo: Aug 16 18:00:27 502d353b:1 Thu Aug 16 18:00:38 [replslave] waiting until: 1345140048 to continue Thu Aug 16 18:00:38 [replslave] repl: sleep 10 sec before next pass
由上述信息知道主从可以建立通信了
2.下面我们在主上创建数据库,并插入集合文档,看其是否同步 在主服务器上:
mongo
MongoDB shell version: 2.0.7 connecting to: test
show dbs local 4.201171875GB use xin switched to db xin db.test.save({title:"just test"}) db.test.find() { "_id" : ObjectId("502d3643c5664ca66103a7cf"), "title" : "just test" } show dbs local 4.201171875GB xin 0.203125GB
注:xin是数据库名,test是集合名,{title:"just test"}是文档 mongodb中使用use即可创建一个数据库当然也可以切换数据库,和mysql有很大区别吧
让我们来看看日志吧 主 server上的日志: Thu Aug 16 18:04:51 [FileAllocator] creating directory /mongo/db/_tmp Thu Aug 16 18:04:51 [FileAllocator] done allocating datafile /mongo/db/xin.ns, size: 16MB, took 0.001 secs Thu 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 secs Thu 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 secs Thu 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 secs Thu Aug 16 18:04:53 [conn3] insert xin.test 2892ms Thu Aug 16 18:05:07 [initandlisten] connection accepted from 10.48.255.243:60173 #4 Thu Aug 16 18:05:08 [conn4] end connection 10.48.255.243:60173
从 server上的日志: Thu Aug 16 18:05:04 [replslave] repl: from host:10.48.255.244:27017 Thu Aug 16 18:05:04 [replslave] resync: dropping database xin Thu Aug 16 18:05:04 [replslave] removeJournalFiles Thu Aug 16 18:05:04 [replslave] resync: cloning database xin to get an initial copy Thu 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/_tmp Thu Aug 16 18:05:04 [FileAllocator] done allocating datafile /mongo/db/xin.ns, size: 16MB, took 0 secs Thu Aug 16 18:05:04 [FileAllocator] allocating new datafile /mongo/db/xin.0, filling with zeroes... Thu Aug 16 18:05:04 [FileAllocator] done allocating datafile /mongo/db/xin.0, size: 64MB, took 0 secs Thu 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 secs Thu 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 secs Thu Aug 16 18:05:04 [replslave] resync: done with initial clone for db: xin Thu Aug 16 18:05:04 [replslave] repl: applied 1 operations
由上述日志可以看出xin数据库已经同步到从 server上了
3.我们进入从 server 的mongodb shell,确认一下
mongo
MongoDB shell version: 2.0.7 connecting to: test
show dbs local 0.203125GB xin 0.203125GB
use xin switched to db xin db.test.find() { "_id" : ObjectId("502d3643c5664ca66103a7cf"), "title" : "just test" }
我们创建的数据库数据并不多,为什么会有0.2G呢,这是因为mongodb预分配数据库的空间,这使下次向数据库中插入数据更快了
4.在 从server 上查看Collection(集合)状态
db.printCollectionStats() system.indexes { "ns" : "xin.system.indexes", "count" : 1, "size" : 64, "avgObjSize" : 64, "storageSize" : 4096, "numExtents" : 1, "nindexes" : 0, "lastExtentSize" : 4096, "paddingFactor" : 1, "flags" : 0, "totalIndexSize" : 0, "indexSizes" : {
},
"ok" : 1
}
test { "ns" : "xin.test", "count" : 1, "size" : 44, "avgObjSize" : 44, "storageSize" : 8192, "numExtents" : 1, "nindexes" : 1, "lastExtentSize" : 8192, "paddingFactor" : 1, "flags" : 1, "totalIndexSize" : 8176, "indexSizes" : { "id" : 8176 }, "ok" : 1 }
在 从server 上查看主从复制的状态
db.printReplicationInfo() this is a slave, printing slave replication info. source: 10.48.255.244:27017 syncedTo: Thu Aug 16 2012 18:33:27 GMT+0000 (UTC) = 18 secs ago (0.01hrs)
db.printSlaveReplicationInfo() source: 10.48.255.244:27017 syncedTo: Thu Aug 16 2012 18:34:37 GMT+0000 (UTC) = 20 secs ago (0.01hrs)
ok,mongodb的主从复制搞定了!
标签:主从复制,04,Aug,16,MongoDB,Thu,配置,mongodb,18 From: https://blog.51cto.com/u_13360482/6992607