这是一个相当简单的场景,我不知道为什么它不工作。
我有一个docker compose
version: '3.8'
services:
mongodb:
image: mongo:latest
command: ["mongod", "--replSet", "rs0", "--bind_ip_all"]
ports:
- "27017:27017"
字符串
我运行docker-compose up
来旋转它
我看到一些木头
sandbox-mongodb-1 | {"t":{"$date":"2023-12-21T18:46:26.001+00:00"},"s":"W", "c":"QUERY", "id":23799, "ctx":"ftdc","msg":"Aggregate command executor error","attr":{"error":{"code":26,"codeName":"NamespaceNotFound","errmsg":"Unable to retrieve storageStats in $collStats stage :: caused by :: Collection [local.oplog.rs] not found."},"stats":{},"cmd":{"aggregate":"oplog.rs","cursor":{},"pipeline":[{"$collStats":{"storageStats":{"waitForLock":false,"numericOnly":true}}}],"$db":"local"}}}
sandbox-mongodb-1 | {"t":{"$date":"2023-12-21T18:46:26.128+00:00"},"s":"I", "c":"-", "id":4939300, "ctx":"monitoring-keys-for-HMAC","msg":"Failed to refresh key cache","attr":{"error":"ReadConcernMajorityNotAvailableYet: Read concern majority reads are currently not possible.","nextWakeupMillis":800}}
sandbox-mongodb-1 | {"t":{"$date":"2023-12-21T18:46:26.929+00:00"},"s":"I", "c":"-", "id":4939300, "ctx":"monitoring-keys-for-HMAC","msg":"Failed to refresh key cache","attr":{"error":"ReadConcernMajorityNotAvailableYet: Read concern majority reads are currently not possible.","nextWakeupMillis":1000}}
sandbox-mongodb-1 | {"t":{"$date":"2023-12-21T18:46:27.001+00:00"},"s":"W", "c":"QUERY", "id":23799, "ctx":"ftdc","msg":"Aggregate command executor error","attr":{"error":{"code":26,"codeName":"NamespaceNotFound","errmsg":"Unable to retrieve storageStats in $collStats stage :: caused by :: Collection [local.oplog.rs] not found."},"stats":{},"cmd":{"aggregate":"oplog.rs","cursor":{},"pipeline":[{"$collStats":{"storageStats":{"waitForLock":false,"numericOnly":true}}}],"$db":"local"}}}
sandbox-mongodb-1 | {"t":{"$date":"2023-12-21T18:46:27.930+00:00"},"s":"I", "c":"-", "id":4939300, "ctx":"monitoring-keys-for-HMAC","msg":"Failed to refresh key cache","attr":{"error":"ReadConcernMajorityNotAvailableYet: Read concern majority reads are currently not possible.","nextWakeupMillis":1200}}
sandbox-mongodb-1 | {"t":{"$date":"2023-12-21T18:46:28.001+00:00"},"s":"W", "c":"QUERY", "id":23799, "ctx":"ftdc","msg":"Aggregate command executor error","attr":{"error":{"code":26,"codeName":"NamespaceNotFound","errmsg":"Unable to retrieve storageStats in $collStats stage :: caused by :: Collection [local.oplog.rs] not found."},"stats":{},"cmd":{"aggregate":"oplog.rs","cursor":{},"pipeline":[{"$collStats":{"storageStats":{"waitForLock":false,"numericOnly":true}}}],"$db":"local"}}}
型
现在我做一个
docker ps
型
我可以看到集装箱ID
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fdea0af9dc8e mongo:latest "docker-entrypoint.s…" 12 minutes ago Up 12 minutes 0.0.0.0:27017->27017/tcp sandbox-mongodb-1
型
我可以用mongosh连接到它
$ docker exec -it 5d44bda7d148 mongosh
Current Mongosh Log ID: 658488458e2ace9369e0f846
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.1.1
Using MongoDB: 7.0.4
Using Mongosh: 2.1.1
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.
------
The server generated these startup warnings when booting
2023-12-21T18:46:24.293+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2023-12-21T18:46:24.911+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2023-12-21T18:46:24.911+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
2023-12-21T18:46:24.911+00:00: vm.max_map_count is too low
------
型
我初始化副本集
test> rs.initiate()
{
info2: 'no configuration specified. Using a default configuration for the set',
me: '5d44bda7d148:27017',
ok: 1
}
型
我可以看到当前配置
rs0 [direct: other] test> rs.config()
{
_id: 'rs0',
version: 1,
term: 1,
members: [
{
_id: 0,
host: '5d44bda7d148:27017',
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 1,
tags: {},
secondaryDelaySecs: Long('0'),
votes: 1
}
],
protocolVersion: Long('1'),
writeConcernMajorityJournalDefault: true,
settings: {
chainingAllowed: true,
heartbeatIntervalMillis: 2000,
heartbeatTimeoutSecs: 10,
electionTimeoutMillis: 10000,
catchUpTimeoutMillis: -1,
catchUpTakeoverDelayMillis: 30000,
getLastErrorModes: {},
getLastErrorDefaults: { w: 1, wtimeout: 0 },
replicaSetId: ObjectId('6584887336c122ec814c9914')
}
}
型
现在我打开,在我的主机(Windows)3 T工作室,我创建连接.
此连接字符串工作正常
mongodb://127.0.0.1:27017
型
此连接字符串未连接
mongodb://127.0.0.1:27017/?replicaSet=rs0&ssl=false
型
为什么
显然副本集运行正常,很明显我可以访问服务器
3 T Studio显示超时错误
SERVER [5d44bda7d148:27017] (Type: UNKNOWN)
|_/ Connection error (MongoSocketOpenException): Exception opening socket
|____/ Unknown host: 5d44bda7d148
Details:
Timed out after 30000 ms while waiting for a server that matches com.mongodb.client.internal.MongoClientDelegate$1@482d37d9. Client view of cluster state is {type=REPLICA_SET, servers=[{address=5d44bda7d148:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.UnknownHostException: 5d44bda7d148}}]
型
我需要让MongoDb副本集在本地作为docker容器运行,因为我需要它进行更改流的本地开发。我希望得到解释和解决方案。
**更新1(2023-12-21)**我也试过
command: ["mongod", "--replSet", "rs0", "--bind_ip", "localhost"]
型
在docker composer中,根据https://www.mongodb.com/docs/manual/tutorial/deploy-replica-set/#ip-binding,但没有运气
1条答案
按热度按时间h9a6wy2h1#
似乎问题是与主机的副本集是默认配置的。它似乎选择了容器ID,这不是我想要的。
如果我连接到mongosh并这样做:
字符串
本质上就是修改宿主,
型
我现在可以从主机连接到
型
这感觉很古怪,但似乎很有效。
更新
另一个更简洁的选择是连接到mongosh并执行此操作,以传递配置而无需重新配置
型
这给了我一个类似的结果,其中主机具有127.0.0.1或localhost而不是容器ID。