Mongodb主從配置

來源:互聯網
上載者:User

一:使用命令列方式配置mongodb主從

 
  1. [root@server11 ~]#  /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. [root@server12 ~]#  /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. [root@server11 ~]# /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. [root@server12 ~]# /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 } 
650) this.width=650;" src="http://img1.51cto.com/attachment/201301/112217602.jpg" border="0" alt="" />

 

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/1S3564113-1.jpg" border="0" alt="" />

三:建立repl使用者,主要用於後續的安全認證同步
 
  1. [root@server11 ~]#  /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. [root@server12 ~]#  /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. [root@server11 ~]# /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. [root@server12 ~]# /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. [root@server11 ~]# /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介面需要輸入使用者名稱和密碼!

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/1S3562H9-2.jpg" border="0" alt="" />

五:使用設定檔管理mongodb

 
  1. [root@server11 ~]# 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. master= true 
  12. auth = true 
  13.  
  14. [root@server11 ~]# /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. [root@server11 ~]# 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. [root@server12 ~]# 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. [root@server12 ~]# /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. [root@server11 ~]# /usr/local/mongodb/bin/mongod -f /etc/mongodb.conf  --shutdown 
  43. [root@server12 ~]# /usr/local/mongodb/bin/mongod -f /etc/mongodb.conf  --shutdown 

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

本文出自 “斬月” 部落格,謝絕轉載!

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.