我正在尝试使用docker创建mongo分片。我有:

配置服务器:

docker run -it --rm --net=xnet -p 27016:27016 \
    --hostname xmongo-config-1 --name xmongo-config-1 \
    -v $PWD/docker/.data/xmongo-config-1:/data/db \
    mongo:latest --port 27016 --replSet xmongo-config --configsvr

mongos(路由器)服务器:
docker run -it --rm --net=xnet -p 27015:27015 \
    --hostname xmongo-mongos --name xmongo-mongos \
    -v $PWD/docker/.data/xmongo-mongos:/data/db \
    mongo:latest mongos --port 27015 --configdb configserver/xmongo-config-1:27016

现在,当我运行时:
docker exec -it xmongo-config-1 mongo --port 27016 --eval 'db.version()'

我看到结果MongoDB server version: 3.4.9但是当我跑步时:
docker exec -it xmongo-mongos mongo --port 27015 --eval 'db.version()'

我懂了:
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27015/
2018-02-16T05:54:26.434+0000 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27015, in(checking socket for error after poll), reason: Connection refused
2018-02-16T05:54:26.434+0000 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27015, connection attempt failed :
connect@src/mongo/shell/mongo.js:237:13
@(connect):1:6
exception: connect failed

最佳答案

错误#1

我在mongos日志中发现了此错误:

[mongosMain] No primary detected for set configserver

这意味着我必须为我的配置副本集初始化主目录,因此运行以下命令:
docker exec -it xmongo-config-1 mongo --port 27016 --eval '
    rs.initiate({ _id: "xmongo-config", members: [
        { _id : 0, host : "xmongo-config-1:27016" }
    ]});
'

错误#2

初始化mongos替换行
--configdb configserver/xmongo-config-1:27016


--configdb xmongo-config/xmongo-config-1:27016

结果

有用!)

10-02 18:58