【问题标题】:Connecting to shards in MongoDB mongo shell连接到 MongoDB mongo shell 中的分片
【发布时间】:2018-03-01 16:21:15
【问题描述】:

我正在关注MongoDB:权威指南第二版中的数据库课程教程,但它似乎不适用于 3.6.2 版。

基本上我使用mongo --nodb 打开了两个mongo shell。

然后,首先,我运行cluster = new ShardingTest({"shards": 3, "chunksize": 1})(它工作并产生稳定的输出流)。

在第二个 shell 中,书上说运行 db = (new Mongo("localhost:30999")).getDB("test") 失败了。一位同事告诉我改为运行db = (new Mongo("localhost:20000")).getDB("test"),这很有效。

然后,我插入了同样有效的数据。但是,在尝试sh.status() 时,我收到了消息printShardingStatus: this db does not have sharding enabled. be sure you are connecting to a mongos from the shell and not to a mongod.

在网上搜索后,我想我会运行sh.enableSharding(db),这也给了我以下错误:

2018-03-01T11:05:22.654-0500 E QUERY     [thread1] Error: not connected to a mongos :
sh._checkMongos@src/mongo/shell/utils_sh.js:8:15
sh._adminCommand@src/mongo/shell/utils_sh.js:18:9
sh.enableSharding@src/mongo/shell/utils_sh.js:98:12
@(shell):1:1

我在 Windows 10 机器上运行,并设置了正确的环境变量并创建了 db 文件夹,因此非常感谢任何帮助/指针!

编辑 1:

即使先运行db.collection.ensureIndex(),此错误仍然存​​在。

【问题讨论】:

    标签: mongodb port sharding mongo-shell


    【解决方案1】:

    下面的命令显示了如何在您的 localhost 中运行 3 个分片实例和 3 个配置实例。对于这些分片中的每一个,还创建了 3 个副本集(mongod 实例),它可以帮助您:

    清理一切

    echo "killing mongod and mongos"
    killall mongod
    killall mongos
    echo "removing data files"
    rm -rf /data/config
    rm -rf /data/shard*
    

    启动一个副本集并告诉它它将是 shard0

    echo "starting servers for shard 0"
    mkdir -p /data/shard0/rs0 /data/shard0/rs1 /data/shard0/rs2
    mongod --replSet s0 --logpath "s0-r0.log" --dbpath /data/shard0/rs0 --port 37017 --fork --shardsvr
    mongod --replSet s0 --logpath "s0-r1.log" --dbpath /data/shard0/rs1 --port 37018 --fork --shardsvr
    mongod --replSet s0 --logpath "s0-r2.log" --dbpath /data/shard0/rs2 --port 37019 --fork --shardsvr
    
    sleep 5
    

    连接到一台服务器并启动设置

    echo "Configuring s0 replica set"
    mongo --port 37017 << 'EOF'
    config = { _id: "s0", members:[
              { _id : 0, host : "localhost:37017" },
              { _id : 1, host : "localhost:37018" },
              { _id : 2, host : "localhost:37019" }]};
    rs.initiate(config)
    EOF
    

    启动一个复制集并告诉它它将是一个 shard1

    echo "starting servers for shard 1"
    mkdir -p /data/shard1/rs0 /data/shard1/rs1 /data/shard1/rs2
    mongod --replSet s1 --logpath "s1-r0.log" --dbpath /data/shard1/rs0 -port 47017 --fork --shardsvr
    mongod --replSet s1 --logpath "s1-r1.log" --dbpath /data/shard1/rs1 --port 47018 --fork --shardsvr
    mongod --replSet s1 --logpath "s1-r2.log" --dbpath /data/shard1/rs2 --port 47019 --fork --shardsvr
    
    sleep 5
    
    echo "Configuring s1 replica set"
    mongo --port 47017 << 'EOF'
    config = { _id: "s1", members:[
              { _id : 0, host : "localhost:47017" },
              { _id : 1, host : "localhost:47018" },
              { _id : 2, host : "localhost:47019" }]};
    rs.initiate(config)
    EOF
    

    启动一个复制集并告诉它它将是一个 shard2

    echo "starting servers for shard 2"
    mkdir -p /data/shard2/rs0 /data/shard2/rs1 /data/shard2/rs2
    mongod --replSet s2 --logpath "s2-r0.log" --dbpath /data/shard2/rs0 --port 57017 --fork --shardsvr
    mongod --replSet s2 --logpath "s2-r1.log" --dbpath /data/shard2/rs1 --port 57018 --fork --shardsvr
    mongod --replSet s2 --logpath "s2-r2.log" --dbpath /data/shard2/rs2 --port 57019 --fork --shardsvr
    
    sleep 5
    
    echo "Configuring s2 replica set"
    mongo --port 57017 << 'EOF'
    config = { _id: "s2", members:[
          { _id : 0, host : "localhost:57017" },
          { _id : 1, host : "localhost:57018" },
          { _id : 2, host : "localhost:57019" }]};
    rs.initiate(config)
    EOF
    

    现在启动 3 个配置服务器

    echo "Starting config servers"
    mkdir -p /data/config/config-a /data/config/config-b /data/config/config-c
    mongod --replSet csReplSet --logpath "cfg-a.log" --dbpath /data/config/config-a --port 57040 --fork --configsvr
    mongod --replSet csReplSet --logpath "cfg-b.log" --dbpath /data/config/config-b --port 57041 --fork --configsvr
    mongod --replSet csReplSet --logpath "cfg-c.log" --dbpath /data/config/config-c --port 57042 --fork --configsvr
    
    echo "Configuring configuration server replica set"
    mongo --port 57040 << 'EOF'
    config = { _id: "csReplSet", members:[
              { _id : 0, host : "localhost:57040" },
              { _id : 1, host : "localhost:57041" },
              { _id : 2, host : "localhost:57042" }]};
    rs.initiate(config)
    EOF
    

    现在在标准端口上启动 mongos

    mongos --logpath "mongos-1.log" --configdb csReplSet/localhost:57040,localhost:57041,localhost:57042 --fork
    echo "Waiting 60 seconds for the replica sets to fully come online"
    sleep 60
    echo "Connnecting to mongos and enabling sharding"
    

    添加分片并在测试数据库上启用分片

    mongo <<'EOF'
    use admin
    db.runCommand( { addshard : "s0/localhost:37017" } );
    db.runCommand( { addshard : "s1/localhost:47017" } );
    db.runCommand( { addshard : "s2/localhost:57017" } );
    db.runCommand( { enableSharding: "test" } );
    db.runCommand( { shardCollection: "test.some_collection", key: { some_id:1 } } );
    EOF
    

    【讨论】:

    • 所以这可能很疯狂,但这些新增内容有多新?我使用的指南是 2013 年的,它没有提到任何这些(基本上,它只使用上面的代码)。我想我还应该提到 enableShardingaddshard 不被识别为命令。
    • 其实我上周已经完成了mongo大学的一门关于它的课程:)
    • 试试这个,很简单
    • 哇!它奏效了,我学习了很多教程,但是这个对我有用,嘿@romulomadu 你能通过链接或 gmail 与我联系吗linkedin.com/in/abhimanyu0301
    • 你能不能为多台服务器写一些类似的东西,如果本地机器基本上会死机,如何在生产中实现它。
    【解决方案2】:

    尝试通过打开一个新的mongo --nodb 连接到端口 20006 上的 shell,然后执行db = (new Mongo("localhost:20006")).getDB("test")

    这应该会打开所有分片的 mongos,所以现在命令 sh.status() 以及设置平衡器状态和启动平衡器等其他命令应该可以工作了。

    【讨论】:

    • 是的,就是这么简单,只是端口问题。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多