连接到 MongoDB 配置服务器以在 Windows10 上运行的 docker 容器上进行分片 [英] Connect to MongoDB config server for Sharding on a docker container running on windows10
问题描述
- 我使用的是
Windows 10
操作系统. - 我的机器上安装了
Docker for windows
. - 我的机器上安装了适用于 Windows 的
mongo shell
. - 我正在使用来自 docker 的最新
mongo
镜像创建配置服务器.
- I am using
Windows 10
Operating system. - I have
Docker for windows
installed on my machine. - I have
mongo shell
for Windows installed on my machine. - I am creating the config servers using the latest
mongo
image from docker.
我正在尝试创建配置服务器(在副本集中;一个主服务器和两个辅助服务器)以便为 MongoDB 设置分片.如果我将它们创建为副本集,我可以连接到 mongod 服务器,而无需指定 --configsvr
参数.但是当我指定 --configsvr
参数时,它失败并显示以下错误 -
I am trying to create config servers (in a replica set; one primary and two secondaries) in order to set up Sharding for MongoDB. I am able to connect to the mongod servers if I create them as replica sets, without specifying the --configsvr
parameter. But when I specify the --configsvr
parameter, it fails with below error -
连接到:mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb错误:无法连接到服务器 127.0.0.1:27017,连接尝试失败:SocketException:连接到 127.0.0.1:27017 时出错:: 导致by :: 无法建立连接,因为目标机器主动拒绝了.:connect@src/mongo/shell/mongo.js:374:17 @(connect):2:6异常:连接失败退出代码 1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: No connection could be ma de because the target machine actively refused it. : connect@src/mongo/shell/mongo.js:374:17 @(connect):2:6 exception: connect failed exiting with code 1
案例 1 - 创建 3 个 mongod 服务器作为副本集
第 1 步:- 在亚洲、美洲和欧洲创建 3 个 mongod 容器.
C:\> docker run -d -p 40001:27017 -v C:/mongodata/data/db --name asia mongo mongod --bind_ip=0.0.0.0 --replSet "rs0"
C:\> docker run -d -p 40002:27017 -v C:/mongodata/data/db --name europe mongo mongod --bind_ip=0.0.0.0 --replSet "rs0"
C:\> docker run -d -p 40003:27017 -v C:/mongodata/data/db --name america mongo mongod --bind_ip=0.0.0.0 --replSet "rs0"
第 2 步:- 执行 docker ps
第 3 步:- 使用 docker exec
连接到名为 asia 的容器.
Step 3:- Using docker exec
to connect to container named asia.
C:\> docker exec -it asia mongo
结果:- 成功连接
第 4 步:-从 mongoshell 连接到容器 asia:-
案例 2 - 创建 3 个 mongod 服务器作为配置服务器作为副本集的一部分
第 1 步:- 创建 3 个 mongod 容器 asiaCS、americaCS 和 europeCS 作为配置服务器.
C:/> docker run -d -p 30001:27017 -v C:/mongodata/data/db --name asiaCS mongo mongod --configsvr --bind_ip=0.0.0.0 --replSet "rs1"
C:/> docker run -d -p 30002:27017 -v C:/mongodata/data/db --name europeCS mongo mongod --configsvr --bind_ip=0.0.0.0 --replSet "rs1"
C:/> docker run -d -p 30003:27017 -v C:/mongodata/data/db --name americaCS mongo mongod --configsvr --bind_ip=0.0.0.0 --replSet "rs1"
第 2 步:- 执行 docker ps
第 3 步:- 使用 docker exec
连接到名为 asiaCS 的容器.
Step 3:- Using docker exec
to connect to container named asiaCS.
docker exec -it asiaCS mongo
结果:- 连接失败
第 4 步:-从 mongoshell 连接到容器 asiaCS:-
这里唯一的区别是启动 mongod 实例作为 MongoDB 分片的配置服务器所需的 --configsvr
参数.有没有人遇到过这样的问题.
The only difference here is the --configsvr
parameter required to start a mongod instance as a config server for MongoDB sharding. Has anyone encountered such an issue before.
附言- 我将 bind_ip 保持为 0.0.0.0 只是为了测试来自 mongoshell 的连接,但在对非本地实例上的生产执行相同操作时要谨慎行事.
P.S. - I have kept the bind_ip to 0.0.0.0 just to test connection from mongoshell, but tread with caution when doing the same for Production on non-local instances.
推荐答案
对于配置服务器来说是 27019.
It's 27019 for config servers.
当您添加 --configsvr 时,您也需要更改端口映射:
When you add --configsvr you need to change port mapping too:
C:/>docker run -d -p 30001:27019 -v C:/mongodata/data/db --name asiaCS mongo mongod --configsvr --bind_ip=0.0.0.0 --replSetrs1"
C:/> docker run -d -p 30001:27019 -v C:/mongodata/data/db --name asiaCS mongo mongod --configsvr --bind_ip=0.0.0.0 --replSet "rs1"
这篇关于连接到 MongoDB 配置服务器以在 Windows10 上运行的 docker 容器上进行分片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!