话说,我正打算加入新的功能。
有这么一段对话。
表弟“貌似一台MongoDB主机容易挂掉,如果停电了或者硬盘损坏了怎么办?”咱可是要提供99.99999999999的可靠性啊。
我“别慌,咱有master-slave机制”
表弟“选主从机制还是副本集机制?”
经过一番思索,我决定上副本集。谁让它更安全呢!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·
在192.168.56.254上 Cd /usr/local/mongodb-linux-x86_64-2.6.1/ ./bin/mongod --dbpath ./data/db --port 27017 --replSet fubenji/192.168.56.66:27018 数据路径 /data/db 在192.168.56.66上 Cd /usr/local/mongodb-linux-x86_64-2.6.1 ./bin/mongod --dbpath ./data/db --port 27018 --replSet fubenji/192.168.56.254:27017
然后执行
db.runCommand( {"replSetInitiate": { "_id":"fubenji", "members": [ { "_id":1,"host":"192.168.56.254:27017"}, { "_id":2,"host":"192.168.56.66:27018"} ] } })
> rs.status() { "set" : "fubenji", "date" : ISODate("2014-06-03T17:42:38Z"), "myState" : 1, "members" : [ { "_id" : 1, "name" : "192.168.56.254:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 270, "optime" : Timestamp(1401817313, 1), "optimeDate" : ISODate("2014-06-03T17:41:53Z"), "electionTime" : Timestamp(1401817319, 1), "electionDate" : ISODate("2014-06-03T17:41:59Z"), "self" : true }, { "_id" : 2, "name" : "192.168.56.66:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 45, "optime" : Timestamp(1401817313, 1), "optimeDate" : ISODate("2014-06-03T17:41:53Z"), "lastHeartbeat" : ISODate("2014-06-03T17:42:37Z"), "lastHeartbeatRecv" : ISODate("2014-06-03T17:42:37Z"), "pingMs" : 0, "syncingTo" : "192.168.56.254:27017" } ], "ok" : 1 }
查看数据是否已经同步:
fubenji:PRIMARY> db.category.find() { "_id" : ObjectId("538cc53227051105c2943c96"), "name" : "framework", "userid" : 1, "score" : 1 } fubenji:SECONDARY> db.category.find() { "_id" : ObjectId("538cc53227051105c2943c96"), "name" : "framework", "userid" : 1, "score" : 1 }
表明副本集建立成功。
然后修改代码,将之前的单机连接池修改为副本集
package controllers; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import play.Play; import com.mongodb.MongoClient; import com.mongodb.ServerAddress; public class MongoConnectionPool { private static MongoClient mongoClient = null; private MongoConnectionPool() { } // 如果是主从机制,请参考 // http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/ public static void setup() { try { /* String ip = Play.application().configuration() .getString("conf.mongo.ip"); String port = Play.application().configuration() .getString("conf.mongo.port"); */ List list = new ArrayList(); String ip_ports = Play.application().configuration() .getString("conf.mongo.ip_port"); String[] array = ip_ports.split(","); int index = 0; for(index=0;index<array.length;index++){ String str = array[index]; String[] ip_port = str.split(":"); list.add(new ServerAddress(ip_port[0],Integer.parseInt(ip_port[1]))); System.out.println("ip-"+ip_port[0]+" port-"+ip_port[1]); } mongoClient = new MongoClient(list); } catch (UnknownHostException e) { e.printStackTrace(); mongoClient = null; } } public static MongoClient getConnection() { return mongoClient; } }
配置项为:
#mongodb conf.mongo.ip_port="192.168.56.254:27017,192.168.56.66:27018"
则现在的架构为:
下一篇:
http://my.oschina.net/qiangzigege/blog/272194