服务器
openssl rand -base64 741 > mongodb-keyfile
bind_ip=192.168.0.11
port=27018
fork=true
dbpath=/data/db/shard0/rs0-0
logpath=/data/log/mongod.log
logappend=true
journal=true
profile=1
replSet=rs0
#keyFile=/data/mongodb-keyfile
bind_ip=192.168.0.15
port=27018
fork=true
dbpath=/data/db/shard0/rs0-1
logpath=/data/log/mongod.log
logappend=true
journal=true
profile=1
replSet=rs0
#keyFile=/data/mongodb-keyfile
port=30000
bind_ip=192.168.0.15
fork=true
dbpath=/data/arb
logpath=/data/log/arb.log
logappend=true
journal=false
smallfiles=true
replSet=rs0
#keyFile=/data/mongodb-keyfile
bin/mongod -f mongo.conf
bin/mongod -f arb.conf
bin/mongo --host 192.168.0.11 --port 27018
rsconf={
_id:"rs0",
members:[
{_id:0,host:"192.168.0.11:27018"},
{_id:1,host:"192.168.0.15:27018"},
{_id:2,host:"192.168.0.15:30000"}
]
}
rsconf.members[0].priority=10
rs.initiate(rsconf);
#create admin user
use admin
db.createUser(
{
user:"admin",
pwd:"admin",
roles:["readWrite","root"]
}
)
bind_ip=192.168.0.12
port=27018
fork=true
dbpath=/data/db/shard1/rs1-0
logpath=/data/log/mongod.log
logappend=true
journal=true
profile=1
replSet=rs1
#keyFile=/data/mongodb-keyfile
bind_ip=192.168.0.15
port=27018
fork=true
dbpath=/data/db/shard1/rs1-1
logpath=/data/log/mongod.log
logappend=true
journal=true
profile=1
replSet=rs1
#keyFile=/data/mongodb-keyfile
port=30000
bind_ip=192.168.0.15
fork=true
dbpath=/data/arb
logpath=/data/log/arb.log
logappend=true
journal=false
smallfiles=true
replSet=rs1
#keyFile=/data/mongodb-keyfile
bin/mongo --host 192.168.0.12 --port 27018
rsconf={
_id:"rs1",
members:[
{_id:0,host:"192.168.0.12:27018"},
{_id:1,host:"192.168.0.16:27018"},
{_id:2,host:"192.168.0.16:30000"}
]
}
rsconf.members[0].priority=10
rs.initiate(rsconf);
#create admin user
use admin
db.createUser(
{
user:"admin",
pwd:"admin",
roles:["readWrite","root"]
}
)
fork=true
port=27019
bind_ip=192.168.0.11
dbpath=/data/configdb
logpath=/data/log/configsrv.log
logappend=true
configsvr=true
#keyFile=/data/mongodb-keyfile
bin/mongod -f configsvr.conf
fork=true
port=27017
bind_ip=192.168.0.11
configdb=192.168.0.11:27019,192.168.0.12:27019,192.168.0.13:27019
logpath=/data/log/mongos.log
logappend=true
#keyFile=/data/mongodb-keyfile
bin/mongos --port 27017 --host 192.168.0.11
sh.addShard("rs0/192.168.0.11:27018,192.168.0.15:27018")
sh.addShard("rs1/192.168.0.12:27018,192.168.0.16:27018")
sh.addShard("rs2/192.168.0.13:27018,192.168.0.17:27018")
#for test
sh.enableSharding("testdb")
sh.shardCollection("testdb.user",{_id:1,num:1})
#create cluster admin
use admin
db.createUser(
{
user:"admin",
pwd:"admin",
roles:["readWrite","root","clusterAdmin"]
}
)
bin/mongos --port 27017 --host 192.168.0.11 -u admin -p admin admin
use testdb
for(var i=0;i<100000;i++){
db.user.insert({"name":"test","num":i,"age":10,"comment":"test})
}
#check
sh.status();
db.user.stats();
db.printShardingStatus();
package com.jttx.export;
import com.mongodb.*;
import org.junit.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* Created by louis on 2015/1/26.
*/
@RunWith(JUnit4.class)
public class ConnectMongoWithAuth {
private static MongoClient mongoClient;
private static final String username="jt_admin";
private static final String pwd="admin";
private static final String database="dnt_mongodb2";
private static final String ip="192.168.0.10";
private static final int port=27017;
@BeforeClass
public static void setUp() throws UnknownHostException {
MongoCredential credential = MongoCredential.createMongoCRCredential(username, database, pwd.toCharArray());
MongoClientOptions options = MongoClientOptions.builder()
.connectionsPerHost(3000)
.threadsAllowedToBlockForConnectionMultiplier(10)
.readPreference(ReadPreference.nearest())
.build();
mongoClient = new MongoClient(Arrays.asList(new ServerAddress(ip, port)), Arrays.asList(credential),options);
assertNotNull(mongoClient);
}
@org.junit.Test
// @Ignore
public void testConnection() {
DB db = mongoClient.getDB(database);
assertNotNull(db);
Set<String> set=db.getCollectionNames();
assertTrue(set.size()>0);
for (String str : set) {
System.out.println(str);
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
@Ignore
public void testReadWrite(){
DB db = mongoClient.getDB(database);
DBCollection dbCollection=db.getCollection("test");
DBObject dbObject = new BasicDBObject("aa", "cc");
dbCollection.insert(dbObject);
DBObject readObject=dbCollection.findOne(dbObject);
assertNotNull(readObject);
System.out.println(readObject);
}
@AfterClass
public static void tearDown(){
mongoClient.close();
}
}