mongoDB配置主从同步

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongoDB配置主从同步相关的知识,希望对你有一定的参考价值。

官方网站:
http://www.mongodb.org/

MongoDB 安裝,主从配置

一 MongoDB 安装

  1. [[email protected]_server src]# wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.0.tgz

  2. [[email protected]_server src]# tar xzvf mongodb-linux-x86_64-2.0.0.tgz

  3. [[email protected]_server src]# mv mongodb-linux-x86_64-2.0.0 /usr/local/mongodb


  4. [[email protected]_server src]# mkdir /usr/local/mongodb/data

  5. [[email protected]_server src]# touch /usr/local/mongodb/logs


  6. [[email protected]_server bin]# /usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --logappend --port=27017 --fork




  7. [[email protected]_server bin]#./mongo

  8. MongoDB shell version: 2.0.0

  9. connecting to:test

  10. > use test;

  11. switched to db test

  12. > exit

  13. bye



  14. [[email protected]_server bin]# netstat -anlpt | grep mongo

  15. tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 11504/mongod 

  16. tcp 0 0 0.0.0.0:28017 0.0.0.0:* LISTEN 11504/mongod


注意:
如果报错
-bash: /usr/local/mongodb/bin/mongod: cannot execute binary file
说明你的服务器和mongodb 的版本不对应, 如果服务器是64位,下载x86_64的mongodb ,如果服务器是32位的, 下载i686的mongodb/


二 MongoDB 主从配置

1)实验环境
主:192.168.0.14
从:192.168.0.64

2)时间同步
两台机器做时间同步
[[email protected]_server src]# ntpdate time.windows.com

3)启动服务
master:192.168.0.14
启动命令
/usr/local/mongodb/bin/mongod --master --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --logappend --port=27017 --fork

slave:192.168.0.64
启动命令
/usr/local/mongodb/bin/mongod --slave --source 192.168.0.14:27017 --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --logappend --port=27017 --fork


4)测试主从
在主上插入数据
测试1:

  1. 测试1:

  2. 1 在主库上插入数据

  3. [[email protected] bin]#./mongo

  4. MongoDB shell version: 2.0.0

  5. connecting to:test

  6. > db.foo.save({"id":123456,"name":‘lizonggang‘})

  7. > db.foo.find({"id":123456})

  8. "_id": ObjectId("51c3f4b21399022afd992f39"),"id": 123456,"name":"lizonggang" }

  9. >


  10. 2 在从上查看数据(已有在主上插入的数据,说明主从成功)

  11. [[email protected]_server bin]#./mongo

  12. MongoDB shell version: 2.0.0

  13. connecting to:test

  14. > db.foo.find({"id":123456})

  15. "_id": ObjectId("51c3f4b21399022afd992f39"),"id": 123456,"name":"lizonggang" }

  16. >


测试2:

  1. 1 在主库上插入数据

  2. > use elaindb

  3. switched to db elaindb

  4. > db.test.save({title:"This is a test!"})

  5. > db.test.find();

  6. "_id": ObjectId("51c3f8f11a90c8da157c7f64"),"title":"This is a test!" }

  7. >


  8. 2 在从库上验证数据

  9. [[email protected]_server bin]#./mongo

  10. MongoDB shell version: 2.0.0

  11. connecting to:test

  12. > use elaindb

  13. switched to db elaindb

  14. > db.test.find();

  15. "_id": ObjectId("51c3f8f11a90c8da157c7f64"),"title":"This is a test!" }

  16. > db.printReplicationInfo();

  17. this is a slave, printing slave replication info.

  18. source: 192.168.0.14:27017

  19.      syncedTo: Fri Jun 21 2013 14:59:10 GMT+0800 (CST)

  20. = 9 secs ago (0hrs)

  21. >

  22. > db.printSlaveReplicationInfo();

  23. source: 192.168.0.14:27017

  24.      syncedTo: Fri Jun 21 2013 14:59:30 GMT+0800 (CST)

  25. = 16 secs ago (0hrs)

  26. >



测试3:

  1. 在这个状态下,slave 下是不可以插入数据的

  2. > db.test.save({title:"This is slave test!"})

  3. not master


5) 一些查询状态的语句

1)查询库

  1. > show dbs;

  2. elaindb 0.0625GB

  3. local 0.125GB

  4. test 0.0625GB


2)在从库上查询主库的地址

  1. > use local;

  2. switched to db local

  3. > db.sources.find()

  4. "_id": ObjectId("51c3f5dd568d10529950b10d"),"host":"192.168.0.14:27017","source":"main","syncedTo": { "t": 1371797300000,"i": 1 } }

  5. >


3)查看主从复制状态

  1. > db.printReplicationInfo();

  2. configured oplog size: 47.6837158203125MB

  3. log length start to end: 1375secs (0.38hrs)

  4. oplog first event time: Fri Jun 21 2013 14:30:35 GMT+0800 (CST)

  5. oplog last event time: Fri Jun 21 2013 14:53:30 GMT+0800 (CST)

  6. now: Fri Jun 21 2013 14:53:32 GMT+0800 (CST)

  7. >


4)查看Collection状态

  1. > db.printCollectionStats();

  2. foo

  3. {

  4. "ns":"test.foo",

  5. "count": 1,

  6. "size": 56,

  7. "avgObjSize": 56,

  8. "storageSize": 4096,

  9. "numExtents": 1,

  10. "nindexes": 1,

  11. "lastExtentSize": 4096,

  12. "paddingFactor": 1,

  13. "flags": 1,

  14. "totalIndexSize": 8176,

  15. "indexSizes": {

  16. "_id_": 8176

  17.     },

  18. "ok": 1

  19. }

  20. ---

  21. system.indexes

  22. {

  23. "ns":"test.system.indexes",

  24. "count": 1,

  25. "size": 64,

  26. "avgObjSize": 64,

  27. "storageSize": 4096,

  28. "numExtents": 1,

  29. "nindexes": 0,

  30. "lastExtentSize": 4096,

  31. "paddingFactor": 1,

  32. "flags": 0,

  33. "totalIndexSize": 0,

  34. "indexSizes": {


  35.     },

  36. "ok": 1

  37. }

  38. ---

  39. >



6)应急
如果在应用中主挂掉了怎么办?
这个情况就需要,停止从库, 把从库以主库的启动命令起来就可以了。

1)停掉从库

kill -9 进程号

2)删除locat下文件


  1. rm mongodb/db/locat.*-rf


3)启动从库


  1. /usr/local/mongodb/bin/mongod --master --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --logappend --port=27017 --fork


7) MongoDB 增,删,改,查


  1. [[email protected] bin]#./mongo

  2. MongoDB shell version: 2.0.0

  3. connecting to:test

  4. > db.test.insert({"Name":"ymind","age": 8 }); #在test数据库里插入数据 

  5. >

  6. > db.test.find({"Name":"ymind" } ); #查询数据

  7. "_id": ObjectId("51c4187090db4e4751a3cd05"),"Name":"ymind","age": 8 }

  8. > db.test.find({"age": 8} ); #查询数据 

  9. "_id": ObjectId("51c4187090db4e4751a3cd05"),"Name":"ymind","age": 8 }

  10. >

  11. >

  12. > db.test.update({"Name":"ymind"}, { "$set": { "age":"5" } } ); #修改数据

  13. > db.test.find({"Name":"ymind" } );

  14. "Name":"ymind","_id": ObjectId("51c4187090db4e4751a3cd05"),"age":"5" }

  15. >

  16. > db.test.remove({"Name":"ymind" } ); #删除数据 

  17. > db.test.find({"Name":"ymind" } );

  18. >

  19. >



以上是关于mongoDB配置主从同步的主要内容,如果未能解决你的问题,请参考以下文章

Linux下Mongodb数据库主从同步配置

RocketMQ源码-主从同步复制和异步复制

Mongodb 主从同步

mongodb配置主从(官方已经不建议使用)

【mongoDB】mongoDB 主从从裁架构重启

MongoDB的主从复制和副本集