问题
I'm trying the MongoDB's shard tutorial, for some simple testing (proof-of-concept project).
I want to try sharding on a single machine. Is this possible and/or does it make any sense?
When I follow the guide steps, it does not work.
First, I start the mongod configsrv database instances on my machine:
bin/mongod --configsvr --dbpath $BASEDIR/data/configdb --port 27019 &
bin/mongod --configsvr --dbpath $BASEDIR/data/configdb1 --port 27020 &
bin/mongod --configsvr --dbpath $BASEDIR/data/configdb2 --port 27021 &
Then, I start the mongos instances so that they "bind" to the config servers:
HOST=$(uname -n) # my machine's name
# starts on default poort 27017
bin/mongos --configdb $HOST:27019,$HOST:27020,$HOST:27021
Until here, everything looks good.
Now I want to add a Shard to the cluster:
bin/mongo --host $(uname -n)
It enters the MongoDB shell.
connecting to: my.machine.name:27017/test
But when I try to add a new shard, I have the following error:
mongos> sh.addShard( "rs1/my.machine.name:27017" )
{
"ok" : 0,
"errmsg" : "couldn't connect to new shard socket exception [CONNECT_ERROR] for rs1/my.machine.name:27017"
}
I have tried with ip, machine's alias, localhost ... nothing seems to work.
Anyone could help me on this? Maybe I'm missing a point.
Thanks in advance
回答1:
I had the same problem, you have to run
mongod &
on your each of your shards
and then you can call
sh.addShard( "rs1/my.machine.name:27017" )
回答2:
Its possible, follow below sequence.
lets say we want to create a cluster with 3 shards each with 1 replica on same machine.
- Start first mongod server (shard 1 with replica 1)with a replica set named s1
- Start second mongod server (shard 2 with replica 1)with a replica set named s2
- Start third mongod server (shard 3 with replica 1)with a replica set named s3
mkdir -p /data/shard0/rs0 /data/shard0/rs1 /data/shard0/rs2
/bin/mongod --replSet Sv1 --logpath "Sv1-r0.log" --dbpath /data/shard0/rs0 --port 37017
/bin/mongod --replSet Sv2 --logpath "Sv2-r0.log" --dbpath /data/shard0/rs1 --port 47017
/bin/mongod --replSet Sv3 --logpath "Sv3-r0.log" --dbpath /data/shard0/rs2 --port 57017
This will start 3 mongod servers/shards with one replica for each shard
/bin/mongo --port 37017 << 'EOF'
config = { _id: "Sv1", members:[
{ _id : 0, host : "<ip>:37017" }]};
rs.initiate(config)
EOF
This will add replicas to replica set Sv1. This should be done for each shard i.e. on each port 47017 and 57017
Now we have cluster ready with shards and replicas but no one knows other shards and replicas, thus we need config server
/bin/mongod --logpath "cfg-a.log" --dbpath /data/config/config-a --port 57040 --fork --configsvr
Then start mongos for admin access,
/bin/mongos --logpath "mongos-1.log" --configdb 10.88.66.218:57040
Now add shards to our cluster,
/bin/mongo <<'EOF'
db.adminCommand( { addShard : "Sv1/"+"<ip>:37017" } );
db.adminCommand( { addShard : "Sv2/"+"<ip>:47017" } );
db.adminCommand( { addShard : "Sv3/"+"<ip>:57017" } );
use db
db.createCollection("col1")
db.adminCommand({enableSharding:"db"}) db.adminCommand({shardCollection: "db.col1", key: {_id:"hashed"}});
EOF
And that's it.... Cluster should be up with 3 shards. You can check with sh.status() command on mongos.
来源:https://stackoverflow.com/questions/29768355/mongodb-cant-shard-on-localhost-because-of-shard-socket-exception