没什么仔细的解释,直接上配置过程,有问题的同学请看原文或百度查详细资料,有想法的同学也欢迎留言交流哈
原文链接 :
主从: http://blog.jobbole.com/72610/自己动手配置一遍后总结如下:
1.创建各个目录(config,mongos.shard1......)
2.如下
Start Mongodb :./mongod -dbpath /usr/local/mongodbtest/master -master
随便进一台机器配置副本集
./mongo
use admin
config = {_id:"repset",members:[{_id:0,host:"192.168.75.129:27017"},{_id:1,host:"192.168.75.130:27017"},{_id:2,host:"192.168.75.131:27017"}]}正文开始
分片配置(注意:3台服务器的时间一定要相同,不然分片会失败!!!!)
1.启动配置服务器4.定义副本集配置
./mongo 127.0.0.1:22001rs.initiate(configshard3);
备注:
arbiterOnly:true表示该节点是仲裁节点,不参数存储数据
db.runCommand({shardcollection:"testdb.table1",key:{id:1}})
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
7.Spring整合MongoDB分片集群的配置:
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">
connect-timeout="60000"
max-wait-time="30000"
auto-connect-retry="true"
socket-keep-alive="false"
socket-timeout="60000"
slave-ok="true"
write-number="1"
write-timeout="0"
write-fsync="true" />
--------------------------------------------------------------------------------------------------------------------------------------------------------------
8.Java连接MongoDB
public class Test {
public static void main(String[] args) {
List
ServerAddress ad1 = new ServerAddress("192.168.75.129", 20000);
ServerAddress ad2 = new ServerAddress("192.168.75.130", 20000);
ServerAddress ad3 = new ServerAddress("192.168.75.131", 20000);
addresses.add(ad1);
addresses.add(ad2);
addresses.add(ad3);
@SuppressWarnings("resource")
MongoClient client = new MongoClient(addresses);
@SuppressWarnings("deprecation")
DB db = client.getDB("testdb");
DBCollection coll = db.getCollection("table1");
searchAll(coll);
}
public static void save(DBCollection coll){
BasicDBObject basicDBObject = new BasicDBObject();
basicDBObject.append("id", 100001);
basicDBObject.append("testKey", "testVal");
coll.save(basicDBObject);
}
public static void searchAll(DBCollection coll){
DBCursor dbObject = coll.find();
List
for(DBObject dbObject2 : dbObjects){
System.out.println(dbObject2);
}
}
}