MongoDB复制连接无需直接连接

huangapple go评论78阅读模式
英文:

mongodb replication connect without directConnection

问题

我在配置Mongo和Docker的复制过程中遇到了问题。我可以正确地使复制服务工作,但是如果我尝试使用MongoCompass连接,它会一直加载,然后出现错误:

getaddrinfo ENOTFOUND dbrepmongotr

URI:
mongodb://localhost:27020/?readPreference=primary

mongodb://localhost:27020/?readPreference=secondary

如果我使用directConnection,它可以正常工作,但是当应用到带有密码等的数据库时,directConnection会跳过它们并连接,所以我们必须禁用directConnection。

我的docker文件:

FROM mongo:5.0.5

COPY mongod.conf /etc/mongod.conf
COPY keyfile.txt /etc/keyfile.txt

EXPOSE 27017

CMD ["mongod", "--config", "/etc/mongod.conf","--keyfile", "/etc/keyfile.txt"]

我的mongod.conf:

# mongod.conf

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,dbRepMongoTR2
#replication:
replication:
  replSet: "myRs0"
#snmp:
security:
  keyFile: /etc/keyfile.txt

我的docker-compose:

version: '3.5'
services:
  dbRepMongoTR:
    container_name: dbRepMongoTR
    image: mongorep:3.3
    ports:
      - 27020:27017
    restart: always
    # volumes:
    #   - ./databdGR:/data/db
    command: bin/sh -c "mongod --port 27017 --replSet myRs0 --bind_ip 127.0.0.1,dbRepMongoTR"
  dbRepMongoTR2:
    container_name: dbRepMongoTR2
    image: mongorep:1.3
    ports:
      - 27021:27017
    restart: always
    # volumes:
    #   - ./databdGR:/data/db
    command: bin/sh -c "mongod --port 27017 --replSet myRs0 --bind_ip 127.0.0.1,dbRepMongoTR2"

然后我进入mongosh中的dbRepMongoTR并执行:

rs.initiate( { _id: "myRs0", members: [{ _id: 0, host: "dbRepMongoTR" }, { _id: 1, host: "dbRepMongoTR2" }] })
rs.status()
英文:

I have a problem configuring the replication with mongo and docker, I can make the replication service work correctly, but if I try to connect with MongoCompass it keeps loading and then the error occurs:

getaddrinfo ENOTFOUND dbrepmongotr

URI:
mongodb://localhost:27020/?readPreference=primary
and
mongodb://localhost:27020/?readPreference=secondary

If I use directConnection if it leaves, but when applying it to a database with password and everything, the directConnection skips them and connects, so we must disable directConnection

my docker-file:

FROM mongo:5.0.5

COPY mongod.conf /etc/mongod.conf
COPY keyfile.txt /etc/keyfile.txt

EXPOSE 27017

CMD ["mongod", "--config", "/etc/mongod.conf","--keyfile", "/etc/keyfile.txt"]

My mongod.conf:

# mongod.conf

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,dbRepMongoTR2
#replication:
replication:
  replSet: "myRs0"
#snmp:
security:
  keyFile: /etc/keyfile.txt

My docker-compose:

version: '3.5'
services:
  dbRepMongoTR:
    container_name: dbRepMongoTR
    image: mongorep:3.3
    ports:
      - 27020:27017
    restart: always
    # volumes:
    #   - ./databdGR:/data/db
    command: bin/sh -c "mongod --port 27017 --replSet myRs0 --bind_ip 127.0.0.1,dbRepMongoTR"
  dbRepMongoTR2:
    container_name: dbRepMongoTR2
    image: mongorep:1.3
    ports:
      - 27021:27017
    restart: always
    # volumes:
    #   - ./databdGR:/data/db
    command: bin/sh -c "mongod --port 27017 --replSet myRs0 --bind_ip 127.0.0.1,dbRepMongoTR2"

Then I go to dbRepMongoTR in mongosh and execute:

rs.initiate( { _id: "myRs0", members: [{ _id: 0, host: "dbRepMongoTR" }, { _id: 1, host: "dbRepMongoTR2" }] })
rs.status()

答案1

得分: 0

没有directCOnnection是不可能的,大部分问题可以通过以下方式解决:

command: '/bin/sh -c "mongod --auth --dbpath /data/db --storageEngine wiredTiger --keyFile /etc/keyfile.txt --replSet myRsGr --bind_ip 127.0.0.1,dbCodesaMongoGR"'
英文:

It's not possible without directCOnnection, much of it can be fixed using:

command: '/bin/sh -c "mongod --auth --dbpath /data/db --storageEngine wiredTiger --keyFile /etc/keyfile.txt --replSet myRsGr --bind_ip 127.0.0.1,dbCodesaMongoGR"'

huangapple
  • 本文由 发表于 2023年8月8日 21:53:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定