Mongodb Master/Slave Configuration

Source: Internet
Author: User

I. Configure mongodb Master/Slave using the command line

 
 
  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 
Ii. Connection Test Data Synchronization
 
 
  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 =" "/>

3. Create a repl user for subsequent Security Authentication Synchronization
 
 
  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") 

4. Create a common user, yang, and 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" } 

5. Restart the mongodb Master/Slave instance and start it in auth mode. The user logs on to the instance for testing.

 
 
  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" 

To log on to the web page again, enter the user name and password!

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

V. Using configuration files to manage 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 

Reference: http://docs.mongodb.org/manual/administration/master-slave/

This article is from the "Bo Yue" blog and will not be reproduced!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.