本文共 2658 字,大约阅读时间需要 8 分钟。
文档始创于2012-11-16
版权 © 2010, 2011, 2012, 2013, 2014, 2015 Netkiller(Neo Chan). All rights reserved.
版权声明
转载请与作者联系,转载时请务必标明文章原始出处和作者信息及本声明。
|
| | 扫描二维码进入 Netkiller 微信订阅号 群:128659835 请注明“读者” |
很多教程上面采用手工配置主从复制,我不建议你这样启动,请采用修改/etc/mongod.conf配置文件的方案。
创建主:mongod –port 27017 –dbpath /var/lib/mongodb –master 创建从:mongod –port 27017 –dbpath /var/lib/mongodb –slave –source master_ip_address:27017
sed -i "s/#master = true/master = true/" /etc/mongod.confsystemctl restart mongod
sed -i "s/#slave = true/slave = true/" /etc/mongod.confsed -i "s/#source = arg/source = mongodb.master.example.com/" /etc/mongod.confsystemctl restart mongod
进入 Master
[root@localhost ~]# mongoMongoDB shell version: 2.6.11connecting to: testWelcome to the MongoDB shell.For interactive help, type "help".For more comprehensive documentation, see http://docs.mongodb.org/Questions? Try the support group http://groups.google.com/group/mongodb-userServer has startup warnings:2015-11-14T15:51:21.215+0800 [initandlisten]2015-11-14T15:51:21.215+0800 [initandlisten] ** WARNING: Readahead for /var/lib/mongodb is set to 4096KB2015-11-14T15:51:21.215+0800 [initandlisten] ** We suggest setting it to 256KB (512 sectors) or less2015-11-14T15:51:21.215+0800 [initandlisten] ** http://dochub.mongodb.org/core/readahead>>> db.foo.save({'name':'neo','address':{'city':'shenzhen','post':518000},'phone':[13113668890,13322993040]})WriteResult({ "nInserted" : 1 })> db.foo.find();{ "_id" : ObjectId("5646e881a11081d5998bf70c"), "name" : "neo", "address" : { "city" : "shenzhen", "post" : 518000 }, "phone" : [ 13113668890, 13322993040 ] }>
进入 Slave
[root@localhost ~]# mongoMongoDB shell version: 2.6.11connecting to: testWelcome to the MongoDB shell.For interactive help, type "help".For more comprehensive documentation, see http://docs.mongodb.org/Questions? Try the support group http://groups.google.com/group/mongodb-userServer has startup warnings:2015-11-14T15:51:23.668+0800 [initandlisten]2015-11-14T15:51:23.668+0800 [initandlisten] ** WARNING: Readahead for /var/lib/mongodb is set to 4096KB2015-11-14T15:51:23.668+0800 [initandlisten] ** We suggest setting it to 256KB (512 sectors) or less2015-11-14T15:51:23.668+0800 [initandlisten] ** http://dochub.mongodb.org/core/readahead> db.foo.find();{ "_id" : ObjectId("5646e881a11081d5998bf70c"), "name" : "neo", "address" : { "city" : "shenzhen", "post" : 518000 }, "phone" : [ 13113668890, 13322993040 ] }>