Mongodb主从配置

一:使用命令行方式配置mongodb主从


  1. [[email protected] ~]#  /usr/local/mongodb/bin/mongod --bind_ip 192.168.1.112 -port 3306 --dbpath /data/mongodb/db1/ --logpath /usr/local/mongodb/logs/server1.log  --rest --master & 
  2. [1] 14449 
  3.   
  4. [[email protected] ~]#  /usr/local/mongodb/bin/mongod --bind_ip 192.168.1.113 --port 3306 --dbpath /data/mongodb/db2/ --logpath /usr/local/mongodb/logs/server2.log  --rest --slave --source 192.168.1.112:3306 & 
  5. [1] 16853 
 二:连接测试数据同步情况 

  1. [[email protected] ~]# /usr/local/mongodb/bin/mongo 192.168.1.112:3306 
  2. MongoDB shell version: 2.2.2 
  3. connecting to: 192.168.1.112:3306/test 
  4. > use test 
  5. switched to db test 
  6. > db.test.save({b:2}) 
  7. > db.test.find() 
  8. { "_id" : ObjectId("50fcb7f05712bfb21c866dc6"), "a" : 1 } 
  9. { "_id" : ObjectId("50fcdccf252ef3433457646f"), "b" : 2 } 
  10.  
  11. [[email protected] ~]# /usr/local/mongodb/bin/mongo 192.168.1.113:3306 
  12. MongoDB shell version: 2.2.2 
  13. connecting to: 192.168.1.113:3306/test 
  14. > use test 
  15. switched to db test 
  16. > db.test.find() 
  17. { "_id" : ObjectId("50fcb7f05712bfb21c866dc6"), "a" : 1 } 
  18. { "_id" : ObjectId("50fcdccf252ef3433457646f"), "b" : 2 } 
Mongodb主从配置

 

Mongodb主从配置

三:创建repl用户,主要用于后续的安全认证同步

  1. [[email protected] ~]#  /usr/local/mongodb/bin/mongo 192.168.1.112:3306 
  2. > use local 
  3. switched to db local 
  4. > db.addUser('repl','replication') 
  5.         "user" : "repl", 
  6.         "readOnly" : false, 
  7.         "pwd" : "418b80a28664aeaeb1ec8bf792ea3052", 
  8.         "_id" : ObjectId("50fce98cc4553449b56c6e9f") 
  9.  
  10. [[email protected] ~]#  /usr/local/mongodb/bin/mongo 192.168.1.113:3306 
  11. > use local 
  12. switched to db local 
  13. > db.addUser('repl','replication') 
  14.         "user" : "repl", 
  15.         "readOnly" : false, 
  16.         "pwd" : "418b80a28664aeaeb1ec8bf792ea3052", 
  17.         "_id" : ObjectId("50fce98cc4553449b56c6e9f") 

四:创建普通用户yang,master端创建即可


  1. > use admin 
  2. switched to db admin 
  3. > db.addUser('yang','123') 
  4. > show users 
  5.         "_id" : ObjectId("50fce0bd15861bedf081584a"), 
  6.         "user" : "yang", 
  7.         "readOnly" : false, 
  8.         "pwd" : "c26040a2869fb7579c83e85c54faaffa" 
  9.  
  10. > db.system.users.find() 
  11. { "_id" : ObjectId("50fce0bd15861bedf081584a"), "user" : "yang", "readOnly" : false, "pwd" : "c26040a2869fb7579c83e85c54faaffa" } 

五:重启mongodb主从实例,以auth方式启动,用户登录测试


  1. [[email protected] ~]# /usr/local/mongodb/bin/mongod --bind_ip 192.168.1.112 --auth --port 3306 --dbpath /data/mongodb/db1/ --logpath /usr/local/mongodb/logs/server1.log  
  2.  
  3.  --rest --master & 
  4. [[email protected] ~]# /usr/local/mongodb/bin/mongod --bind_ip 192.168.1.113 --auth --port 3306 --dbpath /data/mongodb/db2/ --logpath /usr/local/mongodb/logs/server2.log  
  5.  
  6.  --rest --slave --source 192.168.1.112:3306 & 
  7. [[email protected] ~]# /usr/local/mongodb/bin/mongo 192.168.1.112:3306 
  8. MongoDB shell version: 2.2.2 
  9. connecting to: 192.168.1.112:3306/test 
  10. > use admin 
  11. switched to db admin 
  12.  
  13. > show users 
  14. Mon Jan 21 14:42:47 uncaught exception: error: { 
  15.         "$err" : "unauthorized db:test ns:test.system.users lock type:1 client:192.168.1.112", 
  16.         "code" : 10057 
  17.  
  18. > db.auth('yang','123') 
  19.  
  20. > show users 
  21.         "_id" : ObjectId("50fce0bd15861bedf081584a"), 
  22.         "user" : "yang", 
  23.         "readOnly" : false, 
  24.         "pwd" : "c26040a2869fb7579c83e85c54faaffa" 

再次登录web界面需要输入用户名和密码!

Mongodb主从配置

五:使用配置文件管理mongodb


  1. [[email protected] ~]# cat /etc/mongodb.conf  
  2. fork = true 
  3. quiet = true 
  4. bind_ip = 192.168.1.112 
  5. port = 3306 
  6. dbpath = /data/mongodb/db1 
  7. logpath = /usr/local/mongodb/logs/server1.log 
  8. logappend = true 
  9. journal = true 
  10. rest = true 
  11. mastertrue 
  12. auth = true 
  13.  
  14. [[email protected] ~]# /usr/local/mongodb/bin/mongod -f /etc/mongodb.conf  
  15. all output going to: /usr/local/mongodb/logs/server1.log 
  16. forked process: 5831 
  17. child process started successfully, parent exiting 
  18.  
  19. [[email protected] ~]# netstat -ntpl |grep mongo 
  20. tcp        0      0 192.168.1.112:3306          0.0.0.0:*                   LISTEN      5831/mongod          
  21. tcp        0      0 192.168.1.112:4306          0.0.0.0:*                   LISTEN      5831/mongod 
  22.  
  23. [[email protected] ~]# cat /etc/mongodb.conf  
  24. fork = true 
  25. quiet = true 
  26. bind_ip = 192.168.1.113 
  27. port = 3306 
  28. dbpath = /data/mongodb/db2 
  29. logpath = /usr/local/mongodb/logs/server2.log 
  30. logappend = true 
  31. journal = true 
  32. rest = true 
  33. slave = true 
  34. source = 192.168.1.112:3306 
  35. auth = true 
  36.  
  37. [[email protected] ~]# /usr/local/mongodb/bin/mongod -f /etc/mongodb.conf  
  38. all output going to: /usr/local/mongodb/logs/server2.log 
  39. forked process: 3064 
  40. child process started successfully, parent exiting 
  41.  
  42. [[email protected] ~]# /usr/local/mongodb/bin/mongod -f /etc/mongodb.conf  --shutdown 
  43. [[email protected] ~]# /usr/local/mongodb/bin/mongod -f /etc/mongodb.conf  --shutdown 

参考文章:http://docs.mongodb.org/manual/administration/master-slave/

本文转自斩月博客51CTO博客,原文链接http://blog.51cto.com/ylw6006/1123826如需转载请自行联系原作者


ylw6006