|
Here is how to reproduce this issue.
First, set up the cluster with authentication disabled.(set up data and log folder first)
~/mongodb/bin/mongod --shardsvr --replSet shard1 --dbpath ~/mongodb/data/shard11 --oplogSize 100 --logpath ~/mongodb/log/mongod11.log --logappend --fork --port 27200
~/mongodb/bin/mongod --shardsvr --replSet shard1 --dbpath ~/mongodb/data/shard12 --oplogSize 100 --logpath ~/mongodb/log/mongod12.log --logappend --fork --port 27201
~/mongodb/bin/mongod --shardsvr --replSet shard2 --dbpath ~/mongodb/data/shard21 --oplogSize 100 --logpath ~/mongodb/log/mongod21.log --logappend --fork --port 27202
~/mongodb/bin/mongod --shardsvr --replSet shard2 --dbpath ~/mongodb/data/shard22 --oplogSize 100 --logpath ~/mongodb/log/mongod22.log --logappend --fork --port 27203
~/mongodb/bin/mongod --configsvr --dbpath ~/mongodb/data/config --logpath ~/mongodb/log/config.log --logappend --fork
sleep 15
~/mongodb/bin/mongo localhost:27200 --quiet --eval "printjson(config={_id:'shard1',members:[{_id:0,host:'localhost:27200'},{_id: 1, host:'localhost:27201'}]});printjson(rs.initiate(config));"
~/mongodb/bin/mongo localhost:27202 --quiet --eval "printjson(config={_id:'shard2',members:[{_id:0,host:'localhost:27202'},{_id: 1, host:'localhost:27203'}]});printjson(rs.initiate(config));"
sleep 15
~/mongodb/bin/mongos --configdb localhost:27019 --logpath ~/mongodb/log/mongos.log --logappend --fork
sleep 15
~/mongodb/bin/mongo localhost:27017/admin --eval "printjson(db.runCommand(
{addshard:\"shard1/localhost:27200,localhost:27201\",name:\"s1\"}
))"
~/mongodb/bin/mongo localhost:27017/admin --eval "printjson(db.runCommand(
{addshard:\"shard2/localhost:27202,localhost:27203\",name:\"s2\"}
))"
~/mongodb/bin/mongo localhost:27017/admin --eval "printjson(db.addUser(\"admin\",\"admin\"))"
Secondly, stop all processes, and restart them with authentication enabled.
~/mongodb/bin/mongod --keyFile ~/mongodb/bin/key --shardsvr --replSet shard1 --dbpath ~/mongodb/data/shard11 --oplogSize 100 --logpath ~/mongodb/log/mongod11.log --logappend --fork --port 27200
~/mongodb/bin/mongod --keyFile ~/mongodb/bin/key --shardsvr --replSet shard1 --dbpath ~/mongodb/data/shard12 --oplogSize 100 --logpath ~/mongodb/log/mongod12.log --logappend --fork --port 27201
~/mongodb/bin/mongod --keyFile ~/mongodb/bin/key --shardsvr --replSet shard2 --dbpath ~/mongodb/data/shard21 --oplogSize 100 --logpath ~/mongodb/log/mongod21.log --logappend --fork --port 27202
~/mongodb/bin/mongod --keyFile ~/mongodb/bin/key --shardsvr --replSet shard2 --dbpath ~/mongodb/data/shard22 --oplogSize 100 --logpath ~/mongodb/log/mongod22.log --logappend --fork --port 27203
~/mongodb/bin/mongod --keyFile ~/mongodb/bin/key --configsvr --dbpath ~/mongodb/data/config --logpath ~/mongodb/log/config.log --logappend --fork
sleep 15
~/mongodb/bin/mongos --keyFile ~/mongodb/bin/key --configdb localhost:27019 --logpath ~/mongodb/log/mongos.log --logappend --fork
Then run the java code i mentioned. you will get
{ "serverUsed" : "localhost:27200" , "errmsg" : "need to login" , "ok" : 0.0}
but when you excute "mongo localhost:27200 --eval 'printjson(db.serverStatus())'" in your console, you will get the correct result.
Finally, Here is the result you asked:
dentakeshimatoMacBook-Pro:bin tianyi$ ./mongo localhost:27200
MongoDB shell version: 2.0.2
connecting to: localhost:27200/test
PRIMARY> db.isMaster()
{
"setName" : "shard1",
"ismaster" : true,
"secondary" : false,
"hosts" : [
"localhost:27200",
"localhost:27201"
],
"primary" : "localhost:27200",
"me" : "localhost:27200",
"maxBsonObjectSize" : 16777216,
"ok" : 1
}
PRIMARY> db.adminCommand("getCmdLineOpts")
{
"argv" : [
"/Users/tianyi/mongodb/bin/mongod",
"--keyFile",
"/Users/tianyi/mongodb/bin/key",
"--shardsvr",
"--replSet",
"shard1",
"--dbpath",
"/Users/tianyi/mongodb/data/shard11",
"--oplogSize",
"100",
"--logpath",
"/Users/tianyi/mongodb/log/mongod11.log",
"--logappend",
"--fork",
"--port",
"27200"
],
"parsed" :
{
"dbpath" : "/Users/tianyi/mongodb/data/shard11",
"fork" : true,
"keyFile" : "/Users/tianyi/mongodb/bin/key",
"logappend" : true,
"logpath" : "/Users/tianyi/mongodb/log/mongod11.log",
"oplogSize" : 100,
"port" : 27200,
"replSet" : "shard1",
"shardsvr" : true
}
,
"ok" : 1
}
|