我已经建立了一个docker网络,其中已经建立了3个mongo容器。
我所做的摘要:
创建了一个docker网络,其中我设置了3个mongo docker容器
为第一个节点打开mongo shell,并为副本集设置配置
试图从节点应用连接,失败
成功地从mongo shell连接到copysetSet
下面我将详细介绍我尝试过的内容。
这些是我为docker运行的以下cmd:
docker network create my-mongo-cluster
docker run -d -p 30001:27017 --name mongo1 --net my-mongo-cluster mongo mongod --replSet my-mongo-set
docker run -d -p 30002:27017 --name mongo2 --net my-mongo-cluster mongo mongod --replSet my-mongo-set
docker run -d -p 30003:27017 --name mongo3 --net my-mongo-cluster mongo mongod --replSet my-mongo-set
docker exec -it mongo1 mongo
config = { "_id": "my-mongo-set", "members": [{"_id": 0, "host": "mongo1:27017"},{"_id": 1,"host": "mongo2:27017"},{"_id": 3,"host": "mongo3:27017" } ]}
rs.initiate(config)
在MongoDB Compass中,我已连接到主节点192.168.1.3:30001,并使用一个集合用户创建了一个数据库测试。
在节点上,我尝试以下操作:
const app = require('express')();
const mongoose = require('mongoose');
//set up mongo connect
const uri = 'mongodb://192.168.1.3:30001,192.168.1.3:30002,192.168.1.3:30003/test'
mongoose.connect(uri, {useNewUrlParser: true, replicaSet: 'my-mongo-set'})
.then(() => console.log("MongoDB Connected"))
.catch(error => console.log(error));
我从那里得到
Debugger attached.
{ MongoNetworkError: failed to connect to server [mongo3:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND mongo3 mongo3:27017]
at Pool.<anonymous> (C:\Workspaces\intelij\trial&error\spring-reactive-security\mongo-transactional\node_modules\mongodb-core\lib\topologies\server.js:431:11)
at Pool.emit (events.js:198:13)
at connect (C:\Workspaces\intelij\trial&error\spring-reactive-security\mongo-transactional\node_modules\mongodb-core\lib\connection\pool.js:557:14)
at makeConnection (C:\Workspaces\intelij\trial&error\spring-reactive-security\mongo-transactional\node_modules\mongodb-core\lib\connection\connect.js:39:11)
at callback (C:\Workspaces\intelij\trial&error\spring-reactive-security\mongo-transactional\node_modules\mongodb-core\lib\connection\connect.js:261:5)
at Socket.err (C:\Workspaces\intelij\trial&error\spring-reactive-security\mongo-transactional\node_modules\mongodb-core\lib\connection\connect.js:286:7)
at Object.onceWrapper (events.js:286:20)
at Socket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
Waiting for the debugger to disconnect...
Process finished with exit code 0
但是如果我从mongo shell尝试,我就能连接:
mongo "mongodb://192.168.1.3:30001,192.168.1.3:30002,192.168.1.3:30003/test"
MongoDB shell version v4.0.10
connecting to: mongodb://192.168.1.3:30001,192.168.1.3:30002,192.168.1.3:30003/test?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("89994673-11c2-4d6c-8cb5-04041094c147") }
MongoDB server version: 4.0.10
Server has startup warnings:
2019-07-20T06:22:00.476+0000 I STORAGE [initandlisten]
2019-07-20T06:22:00.476+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-07-20T06:22:00.476+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-07-20T06:22:01.185+0000 I CONTROL [initandlisten]
2019-07-20T06:22:01.185+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-07-20T06:22:01.185+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-07-20T06:22:01.185+0000 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
my-mongo-set:PRIMARY>
答案 0 :(得分:0)
好的,所以我终于弄清楚我做错了什么。也许这会帮助某人:
您可以进入C:\ Windows \ System32 \ drivers \ etc并放入主机 MACHINE_IP mongo1 mongo2 mongo3,或者只需将配置替换为:
config = { "_id": "my-mongo-set", "members": [{"_id": 0, "host": "MACHINE_IP:30001"},{"_id": 1,"host": "<MACHINE_IP>:30002"},{"_id": 2,"host": "<MACHINE_IP>:30003" } ]}
我在mongo上也遇到了一些问题,弄清楚哪个是主要的,所以我通过添加 MONGODB_REPLICA_SET_MODE 和 MONGODB_PRIMARY_HOST 来对docker运行进行了一些修改:
docker run -d -p 30001:27017 --name mongo1 -e MONGODB_REPLICA_SET_MODE=primary --net my-mongo-cluster mongo mongod --replSet my-mongo-set
docker run -d -p 30002:27017 --name mongo2 -e MONGODB_REPLICA_SET_MODE=secondary -e MONGODB_PRIMARY_HOST=mongo1 --net my-mongo-cluster mongo mongod --replSet my-mongo-set
docker run -d -p 30003:27017 --name mongo3 -e MONGODB_REPLICA_SET_MODE=secondary -e MONGODB_PRIMARY_HOST=mongo1 --net my-mongo-cluster mongo mongod --replSet my-mongo-set