标签:
dbpath=/home/admin/mongoconf/data logpath=/home/admin/mongoconf/logs/mongodb.log logappend=true fork=true port=30000
logpath=/home/admin/mongos/logs/mongodb.log logappend=true fork=true port=40000
dbpath=/home/admin/mongo1/data logpath=/home/admin/mongo1/logs/mongodb.log logappend=true fork=true port=37117
./mongod -f ../conf/mongodb.conf
./mongos -f ../conf/mongodb.conf --configdb=192.168.0.201:30000
./mongod -f ../conf/mongodb.conf
./mongo 192.168.0.201:40000/admin
db.runCommand({"addshard":"192.168.0.201:37117"}) db.runCommand({"addshard":"192.168.0.201:47117"})
db.runCommand({"enablesharding":"test"}) db.runCommand({"shardcollection":"test.user","key":{"name":1}})
use test for(var i=0;i<100000;i++){ db.user.insert({"name":"test"+i,"age":i}); }
db.printShardingStatus()
db.createUser({"user":"root","pwd":"123456","roles":["root"]})
db.createUser({"user":"test","pwd":"123456","roles":[{"db":"test","role":"readWrite"},"readWrite"]})
openssl rand -base64 753 > keyFile chmod 600 keyFile
auth=true keyFile=/home/admin/mongo2/data/keyFile
public class MonTest { public static void main(String[] args) { mongodbfind2(); } public static void mongodbfind2() { ServerAddress sa = new ServerAddress("192.168.0.201", 40000); List<MongoCredential> mongoCredentialList = new ArrayList<MongoCredential>(); mongoCredentialList.add(MongoCredential.createCredential("test", "test", "123456".toCharArray())); MongoClient client = new MongoClient(sa, mongoCredentialList); MongoDatabase database = client.getDatabase("test"); // ******** MongoCollection collection2 = database.getCollection("user"); FindIterable<Document> findIterable = collection2.find(); MongoCursor<Document> cursor2 = findIterable.iterator(); while (cursor2.hasNext()) { Document doc = cursor2.next(); System.out.println(doc.get("_id") + "," + doc.get("name") + "," + doc.get("age")); } // 关闭数据库连接 client.close(); } }
mongodb3.0分片及java代码连接操作测试(开启用户验证)
标签:
原文地址:http://blog.csdn.net/tuzongxun/article/details/51939976