docker 无法连接到MongoDB,其中MongoSet在localhost中作为容器运行

4urapxun  于 2024-01-06  发布在  Docker
关注(0)|答案(1)|浏览(214)

这是一个相当简单的场景,我不知道为什么它不工作。
我有一个docker compose

  1. version: '3.8'
  2. services:
  3. mongodb:
  4. image: mongo:latest
  5. command: ["mongod", "--replSet", "rs0", "--bind_ip_all"]
  6. ports:
  7. - "27017:27017"

字符串
我运行docker-compose up来旋转它
我看到一些木头

  1. 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"}}}
  2. 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}}
  3. 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}}
  4. 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"}}}
  5. 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}}
  6. 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"}}}


现在我做一个

  1. docker ps


我可以看到集装箱ID

  1. $ docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. fdea0af9dc8e mongo:latest "docker-entrypoint.s…" 12 minutes ago Up 12 minutes 0.0.0.0:27017->27017/tcp sandbox-mongodb-1


我可以用mongosh连接到它

  1. $ docker exec -it 5d44bda7d148 mongosh
  2. Current Mongosh Log ID: 658488458e2ace9369e0f846
  3. Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.1.1
  4. Using MongoDB: 7.0.4
  5. Using Mongosh: 2.1.1
  6. For mongosh info see: https://docs.mongodb.com/mongodb-shell/
  7. To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
  8. You can opt-out by running the disableTelemetry() command.
  9. ------
  10. The server generated these startup warnings when booting
  11. 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
  12. 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
  13. 2023-12-21T18:46:24.911+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
  14. 2023-12-21T18:46:24.911+00:00: vm.max_map_count is too low
  15. ------


我初始化副本集

  1. test> rs.initiate()
  2. {
  3. info2: 'no configuration specified. Using a default configuration for the set',
  4. me: '5d44bda7d148:27017',
  5. ok: 1
  6. }


我可以看到当前配置

  1. rs0 [direct: other] test> rs.config()
  2. {
  3. _id: 'rs0',
  4. version: 1,
  5. term: 1,
  6. members: [
  7. {
  8. _id: 0,
  9. host: '5d44bda7d148:27017',
  10. arbiterOnly: false,
  11. buildIndexes: true,
  12. hidden: false,
  13. priority: 1,
  14. tags: {},
  15. secondaryDelaySecs: Long('0'),
  16. votes: 1
  17. }
  18. ],
  19. protocolVersion: Long('1'),
  20. writeConcernMajorityJournalDefault: true,
  21. settings: {
  22. chainingAllowed: true,
  23. heartbeatIntervalMillis: 2000,
  24. heartbeatTimeoutSecs: 10,
  25. electionTimeoutMillis: 10000,
  26. catchUpTimeoutMillis: -1,
  27. catchUpTakeoverDelayMillis: 30000,
  28. getLastErrorModes: {},
  29. getLastErrorDefaults: { w: 1, wtimeout: 0 },
  30. replicaSetId: ObjectId('6584887336c122ec814c9914')
  31. }
  32. }


现在我打开,在我的主机(Windows)3 T工作室,我创建连接.
此连接字符串工作正常

  1. mongodb://127.0.0.1:27017

此连接字符串未连接

  1. mongodb://127.0.0.1:27017/?replicaSet=rs0&ssl=false


为什么
显然副本集运行正常,很明显我可以访问服务器
3 T Studio显示超时错误

  1. SERVER [5d44bda7d148:27017] (Type: UNKNOWN)
  2. |_/ Connection error (MongoSocketOpenException): Exception opening socket
  3. |____/ Unknown host: 5d44bda7d148
  4. Details:
  5. 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)**我也试过

  1. command: ["mongod", "--replSet", "rs0", "--bind_ip", "localhost"]


在docker composer中,根据https://www.mongodb.com/docs/manual/tutorial/deploy-replica-set/#ip-binding,但没有运气

h9a6wy2h

h9a6wy2h1#

似乎问题是与主机的副本集是默认配置的。它似乎选择了容器ID,这不是我想要的。
如果我连接到mongosh并这样做:

  1. let cfg = rs.config()
  2. cfg.members[0].host = 'localhost:27017'
  3. rs.reconfig(cfg)

字符串
本质上就是修改宿主,

  1. rs0 [direct: primary] test> rs.config()
  2. {
  3. _id: 'rs0',
  4. version: 3,
  5. term: 1,
  6. members: [
  7. {
  8. _id: 0,
  9. host: '127.0.0.1:27017',
  10. arbiterOnly: false,
  11. buildIndexes: true,
  12. hidden: false,
  13. priority: 1,
  14. tags: {},
  15. secondaryDelaySecs: Long('0'),
  16. votes: 1
  17. }
  18. ],
  19. protocolVersion: Long('1'),
  20. writeConcernMajorityJournalDefault: true,
  21. settings: {
  22. chainingAllowed: true,
  23. heartbeatIntervalMillis: 2000,
  24. heartbeatTimeoutSecs: 10,
  25. electionTimeoutMillis: 10000,
  26. catchUpTimeoutMillis: -1,
  27. catchUpTakeoverDelayMillis: 30000,
  28. getLastErrorModes: {},
  29. getLastErrorDefaults: { w: 1, wtimeout: 0 },
  30. replicaSetId: ObjectId('6584bfe7812f7983ae2da944')
  31. }
  32. }


我现在可以从主机连接到

  1. mongodb://localhost:27017/?replicaSet=rs0


这感觉很古怪,但似乎很有效。
更新
另一个更简洁的选择是连接到mongosh并执行此操作,以传递配置而无需重新配置

  1. test> const configuration = { _id: "rs0", members: [{ _id: 0, host: "127.0.0.1:27017"}]}
  2. test> rs.initiate(configuration)


这给了我一个类似的结果,其中主机具有127.0.0.1或localhost而不是容器ID。

展开查看全部

相关问题