为了账号安全,请及时绑定邮箱和手机立即绑定

Mongodb主从配置

标签:
MongoDB


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

[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 & 

[1] 14449 

  

[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 & 

[1] 16853 

 二:连接测试数据同步情况 

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

MongoDB shell version: 2.2.2 

connecting to: 192.168.1.112:3306/test 

> use test 

switched to db test 

> db.test.save({b:2}) 

> db.test.find() 

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

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

 

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

MongoDB shell version: 2.2.2 

connecting to: 192.168.1.113:3306/test 

> use test 

switched to db test 

> db.test.find() 

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

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

 

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

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

> use local 

switched to db local 

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

        "user" : "repl", 

        "readOnly" : false, 

        "pwd" : "418b80a28664aeaeb1ec8bf792ea3052", 

        "_id" : ObjectId("50fce98cc4553449b56c6e9f") 

 

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

> use local 

switched to db local 

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

        "user" : "repl", 

        "readOnly" : false, 

        "pwd" : "418b80a28664aeaeb1ec8bf792ea3052", 

        "_id" : ObjectId("50fce98cc4553449b56c6e9f") 

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

> use admin 

switched to db admin 

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

> show users 

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

        "user" : "yang", 

        "readOnly" : false, 

        "pwd" : "c26040a2869fb7579c83e85c54faaffa" 

 

> db.system.users.find() 

{ "_id" : ObjectId("50fce0bd15861bedf081584a"), "user" : "yang", "readOnly" : false, "pwd" : "c26040a2869fb7579c83e85c54faaffa" } 

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

[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  

 

 --rest --master & 

[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  

 

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

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

MongoDB shell version: 2.2.2 

connecting to: 192.168.1.112:3306/test 

> use admin 

switched to db admin 

 

> show users 

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

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

        "code" : 10057 

 

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

 

> show users 

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

        "user" : "yang", 

        "readOnly" : false, 

        "pwd" : "c26040a2869fb7579c83e85c54faaffa" 

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

五:使用配置文件管理mongodb

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

fork = true 

quiet = true 

bind_ip = 192.168.1.112 

port = 3306 

dbpath = /data/mongodb/db1 

logpath = /usr/local/mongodb/logs/server1.log 

logappend = true 

journal = true 

rest = true 

master= true 

auth = true 

 

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

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

forked process: 5831 

child process started successfully, parent exiting 

 

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

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

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

 

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

fork = true 

quiet = true 

bind_ip = 192.168.1.113 

port = 3306 

dbpath = /data/mongodb/db2 

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

logappend = true 

journal = true 

rest = true 

slave = true 

source = 192.168.1.112:3306 

auth = true 

 

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

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

forked process: 3064 

child process started successfully, parent exiting 

 

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

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

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

©著作权归作者所有:来自51CTO博客作者ylw6006的原创作品,谢绝转载,否则将追究法律责任

masterslavemongodbWorkSpace


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消