CentOS6.5下安装配置MongoDB分片集群

没什么仔细的解释,直接上配置过程,有问题的同学请看原文或百度查详细资料,有想法的同学也欢迎留言交流哈

原文链接 : 

主从: http://blog.jobbole.com/72610/
副本集: http://www.thebigdata.cn/JieJueFangAn/12416.html
分片: http://www.thebigdata.cn/jiejueFangAn/12418.html


自己动手配置一遍后总结如下:

1.创建各个目录(config,mongos.shard1......)

2.如下

Start Mongodb : 
./mongod --dbpath /usr/local/mongodbtest/single/data/

Start Master MongoDB : 

./mongod -dbpath /usr/local/mongodbtest/master -master


每台机器启动副本集命令 : 
./mongod --dbpath /usr/local/mongodbtest/replset/data --replSet repset

随便进一台机器配置副本集

./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"}]}
rs.initiate(config);

正文开始

分片配置(注意:3台服务器的时间一定要相同,不然分片会失败!!!!)

1.启动配置服务器
./mongod --configsvr --dbpath /usr/local/mongodbtest/shard/config/data --port 21000 --logpath /usr/local/mongodbtest/shard/config/log/config.log --fork

2.启动mongos服务器
./mongos --configdb  192.168.75.129:21000,192.168.75.130:21000,192.168.75.131:21000 --port 20000 --logpath /usr/local/mongodbtest/shard/mongos/log/mongos.log --fork

3.配置各个分片的副本集
./mongod --shardsvr --replSet shard1  --port 22001 --dbpath /usr/local/mongodbtest/shard/shard1/data --logpath /usr/local/mongodbtest/shard/shard1/log/shard1.log --fork --nojournal --oplogSize 10

./mongod --shardsvr --replSet shard2  --port 22002 --dbpath /usr/local/mongodbtest/shard/shard2/data --logpath /usr/local/mongodbtest/shard/shard2/log/shard2.log --fork --nojournal --oplogSize 10

./mongod --shardsvr --replSet shard3  --port 22003 --dbpath /usr/local/mongodbtest/shard/shard3/data --logpath /usr/local/mongodbtest/shard/shard3/log/shard3.log --fork --nojournal --oplogSize 10

4.定义副本集配置

./mongo 127.0.0.1:22001
use admin
configshard1={_id:"shard1",members:[{_id:0,host:"192.168.75.129:22001"},{_id:1,host:"192.168.75.130:22001"},{_id:2,host:"192.168.75.131:22001",arbiterOnly:true}]}
rs.initiate(configshard1);

./mongo 127.0.0.1:22002
use admin
configshard2={_id:"shard2",members:[{_id:0,host:"192.168.75.129:22002"},{_id:1,host:"192.168.75.130:22002"},{_id:2,host:"192.168.75.131:22002",arbiterOnly:true}]}
rs.initiate(configshard2);

./mongo 127.0.0.1:22003
use admin
configshard3={_id:"shard3",members:[{_id:0,host:"192.168.75.129:22003"},{_id:1,host:"192.168.75.130:22003"},{_id:2,host:"192.168.75.131:22003",arbiterOnly:true}]}

rs.initiate(configshard3);

备注:

arbiterOnly:true表示该节点是仲裁节点,不参数存储数据


5.串联路由服务器与分配副本集
./mongo 127.0.0.1:20000
use admin
db.runCommand({addshard:"shard1/192.168.75.129:22001,192.168.75.130:22001,192.168.75.131:22001"});

db.runCommand({addshard:"shard2/192.168.75.129:22002,192.168.75.130:22002,192.168.75.131:22002"});

db.runCommand({addshard:"shard3/192.168.75.129:22003,192.168.75.130:22003,192.168.75.131:22003"});

6.指定的数据库和集合分片生效
db.runCommand({enablesharding:"testdb"})

db.runCommand({shardcollection:"testdb.table1",key:{id:1}})

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

7.Spring整合MongoDB分片集群的配置:


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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">


                    threads-allowed-to-block-for-connection-multiplier="100"
            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 addresses = new ArrayList<>();
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 dbObjects = dbObject.toArray();
for(DBObject dbObject2 : dbObjects){
System.out.println(dbObject2);
}
}
}


你可能感兴趣的:(Linux)