一。mongodb安装
    下载mongodb,http://www.mongodb.org/downloads
    安装:
    声明:本文主从配在一台机子上
 
  1. #tar -zxvf mongodb-2.2.tar.gz
  2. #mv mongodb-linux-x86_64-static-legacy-2.0.6/ /usr/local/mongodb
     建立日志文件夹和数据库存放目录:

  1. #cd /usr/local/mongodb
  2. #mkdir -p data/db_master
  3. #mkdir data/db_slave
  4. #mkdir log
    配置主从配置文件:(配置文件中各个参数不明白的,建议/usr/local/mongodb/bin/mongod -h查询

  1. #cd bin
  2. #vim mongod_master.conf
  3. dbpath = /usr/local/mongodb/data/db_master
  4. logpath = /usr/local/mongodb/log/mongodb_master.log
  5. port = 13600
  6. bind_ip = 192.168.1.102,127.0.0.1
  7. logappend = true
  8. fork = true
  9. directoryperdb = true
  10. nohttpinterface = true
  11. oplogSize = 1024
  12. master = true
  13. noauth = true
  14. only = stock

  15. #vim mongod_slave.conf
  16. dbpath = /usr/local/mongodb/data/db_slave
  17. logpath = /usr/local/mongodb/log/mongodb_slave.log
  18. port = 13700
  19. bind_ip = 192.168.1.102,127.0.0.1
  20. logappend = true
  21. fork = true
  22. directoryperdb = true
  23. nohttpinterface = true
  24. oplogSize = 1024
  25. slave = true
  26. noauth = true
  27. source = 192.168.1.102:13600

先启动主,在启动从:

  1. # /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongod_master.conf
  2. # /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongod_slave.conf
如果出现一下情况:建议删除从下面数据文件,就是删除db_slave下的所有文件
Wed Mar 27 15:37:07 ERROR: Client::~Client _context should be null but is not; client:replslave

查看主从是否启动:会看到四个mongo

  1. #netstat -tlupn | grep mongo

自此安装完毕。

二。检查主从工作

登录主,插入数据:
  1. #/usr/local/mongodb/bin/mongo --port 13600
  2. > use test
  3. > db.blog.save({title:"new test"})
  4. > exit
登录从,检查数据是否同步:

  1. #/usr/local/mongodb/bin/mongo --port 13700
  2. > use test
  3. > db.blog.find()
  4. { "_id" : ObjectId("5152bc3f25dc5817713accce"), "title" : "new test" }     ###显示这行,表示同步成功




    
    
10-28 17:13