Mongodb主從配置完美實現讀寫分離

一:使用命令行方式配置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. [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 &

  4. [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. [root@server12 ~]# /usr/local/mongodb/bin/mongo 192.168.1.113:3306

  11. MongoDB shell version: 2.2.2

  12. connecting to: 192.168.1.113:3306/test

  13. > use test

  14. switched to db test

  15. > db.test.find()

  16. { "_id" : ObjectId("50fcb7f05712bfb21c866dc6"), "a" : 1 }

  17. { "_id" : ObjectId("50fcdccf252ef3433457646f"), "b" : 2 }



三:創建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. {

  6.        "user" : "repl",

  7.        "readOnly" : false,

  8.        "pwd" : "418b80a28664aeaeb1ec8bf792ea3052",

  9.        "_id" : ObjectId("50fce98cc4553449b56c6e9f")

  10. }

  11. [root@server12 ~]#  /usr/local/mongodb/bin/mongo 192.168.1.113:3306

  12. > use local

  13. switched to db local

  14. > db.addUser('repl','replication')

  15. {

  16.        "user" : "repl",

  17.        "readOnly" : false,

  18.        "pwd" : "418b80a28664aeaeb1ec8bf792ea3052",

  19.        "_id" : ObjectId("50fce98cc4553449b56c6e9f")

  20. }

四:創建普通用戶yang,master端創建即可

  1. > use admin

  2. switched to db admin

  3. > db.addUser('yang','123')

  4. > show users

  5. {

  6.        "_id" : ObjectId("50fce0bd15861bedf081584a"),

  7.        "user" : "yang",

  8.        "readOnly" : false,

  9.        "pwd" : "c26040a2869fb7579c83e85c54faaffa"

  10. }

  11. > db.system.users.find()

  12. { "_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. --rest --master &

  3. [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  

  4. --rest --slave --source 192.168.1.112:3306 &

  5. [root@server11 ~]# /usr/local/mongodb/bin/mongo 192.168.1.112:3306

  6. MongoDB shell version: 2.2.2

  7. connecting to: 192.168.1.112:3306/test

  8. > use admin

  9. switched to db admin

  10. > show users

  11. Mon Jan 21 14:42:47 uncaught exception: error: {

  12.        "$err" : "unauthorized db:test ns:test.system.users lock type:1 client:192.168.1.112",

  13.        "code" : 10057

  14. }

  15. > db.auth('yang','123')

  16. 1

  17. > show users

  18. {

  19.        "_id" : ObjectId("50fce0bd15861bedf081584a"),

  20.        "user" : "yang",

  21.        "readOnly" : false,

  22.        "pwd" : "c26040a2869fb7579c83e85c54faaffa"

  23. }

再次登錄web界面需要輸入用戶名和密碼!

112249758.jpg

五:使用配置文件管理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. [root@server11 ~]# /usr/local/mongodb/bin/mongod -f /etc/mongodb.conf  

  14. all output going to: /usr/local/mongodb/logs/server1.log

  15. forked process: 5831

  16. child process started successfully, parent exiting

  17. [root@server11 ~]# netstat -ntpl |grep mongo

  18. tcp        0      0 192.168.1.112:3306          0.0.0.0:*                   LISTEN      5831/mongod          

  19. tcp        0      0 192.168.1.112:4306          0.0.0.0:*                   LISTEN      5831/mongod

  20. [root@server12 ~]# cat /etc/mongodb.conf  

  21. fork = true

  22. quiet = true

  23. bind_ip = 192.168.1.113

  24. port = 3306

  25. dbpath = /data/mongodb/db2

  26. logpath = /usr/local/mongodb/logs/server2.log

  27. logappend = true

  28. journal = true

  29. rest = true

  30. slave = true

  31. source = 192.168.1.112:3306

  32. auth = true

  33. [root@server12 ~]# /usr/local/mongodb/bin/mongod -f /etc/mongodb.conf  

  34. all output going to: /usr/local/mongodb/logs/server2.log

  35. forked process: 3064

  36. child process started successfully, parent exiting

  37. [root@server11 ~]# /usr/local/mongodb/bin/mongod -f /etc/mongodb.conf  --shutdown

  38. [root@server12 ~]# /usr/local/mongodb/bin/mongod -f /etc/mongodb.conf  --shutdown

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章