如何仅连接到Mongo实例的主节点?

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

How does one attach to just the primary node of a mongo instance?

问题

这可能是我对Mongo的工作方式或新的Go开发有误解,但我无法从Go中连接到我的Mongo实例。当我使用Studio 3T连接到我的Mongo实例时,可以正常连接,浏览表等。但是,如果我尝试使用Go模块连接,它会抱怨无法找到所有节点。它需要能够访问所有节点吗?我以为副本集本身应该处理复制?

例如,我有以下Go代码:

    package main
    
    import (
    	"context"
    	"go.mongodb.org/mongo-driver/mongo"
    	"go.mongodb.org/mongo-driver/mongo/options"
    	"go.mongodb.org/mongo-driver/mongo/readpref"
    	"log"
    	"time"
    )
    
    func main() {
    	log.Println("Hello World!")
    
    	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    	defer cancel()
    
    	client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://MongoInsance:27017"))
    	if err != nil {
    		log.Fatal("Failed to connect to Mongo DB", err)
    	}
    
    	if err := client.Ping(ctx, readpref.Primary()); err != nil {
    		log.Fatal("Not actually connected ", err)
    	}
    
    	res, err := client.ListDatabases(ctx, nil)
    	if err != nil{
    		log.Fatal("Failed to list databases")
    	}
    
    	for _, val := range res.Databases {
    		log.Println(val.Name)
    	}
    	defer disconnect(client, &ctx)
    }
    
    func disconnect(client *mongo.Client, ctx *context.Context) {
    	if err := client.Disconnect(*ctx); err != nil {
    		panic(err)
    	}
    }

但是,当我运行上述代码时,收到以下错误响应:

Not actually connected server selection error: context deadline exceeded, current topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr: mqtt-ingester-db-mast:27017, Type: Unknown, Last error: connection() error occured during connection handshake: dial tcp: lookup mqtt-ingester-db-mast: no such host }, { Addr: mqtt-ingester-db-rep1:27017, Type: Unknown, Last error: connection() error occured during connection handshake: dial tcp: lookup mqtt-ingester-db-rep1: no such host }, { Addr: mqtt-ingester-db-rep2:27017, Type: Unknown, Last error: connection() error occured during connection handshake: dial tcp: lookup mqtt-ingester-db-rep2: no such host }, ] }

我是否真的需要暴露所有副本集?
目前,我在主机MongoInstance上使用Docker运行主节点和2个从节点,主节点连接到端口27017:

docker-compose.yml

    services:
      ...
      mqtt-ingester-db-mast:
        container_name: mqtt-ingester-db-mast
        restart: always
        image: mongo:latest
        ports:
          - 27017:27017
        volumes:
          - 'mongo_main:/data/db'
        entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
      mqtt-ingester-db-rep1:
        container_name: mqtt-ingester-db-rep1
        restart: always
        image: mongo:latest
        expose:
          - 27017
        volumes:
          - 'mongo_rep1:/data/db'
        entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
      mqtt-ingester-db-rep2:
        container_name: mqtt-ingester-db-rep2
        restart: always
        image: mongo:latest
        expose:
          - 27017
        volumes:
          - 'mongo_rep2:/data/db'
        entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
      mqtt-ingester-setup:
        image: mongo:latest
        container_name: mqtt-ingester-setup
        links:
          - mqtt-ingester-db-mast:mqtt-ingester-db-mast
          - mqtt-ingester-db-rep1:mqtt-ingester-db-rep1
          - mqtt-ingester-db-rep2:mqtt-ingester-db-rep2
        depends_on:
          - mqtt-ingester-db-mast
          - mqtt-ingester-db-rep1
          - mqtt-ingester-db-rep2
        volumes:
          - ./mqtt-ingester:/scripts
        restart: "no"
        entrypoint: [ "bash", "/scripts/mqtt_ingester_setup.sh" ]
      mqtt-ingester-explorer:
        container_name: mqtt-ingester-explorer
        restart: always
        image: mongo-express:latest
        ports:
          - '8081:8081'
        depends_on:
          - mqtt-ingester-db-mast
        links:
          - mqtt-ingester-db-mast:mongo
      ...
英文:

This might be me misunderstanding how Mongo works/new Go dev - but I'm not able to connect to my mongo instance from Go. When I connect to my Mongo instance using Studio 3T, I can connect just fine, browse the tables, etc. But If I try to connect using the Go module, it complains about not being able to find all the nodes. Is it necessary for it to be able to access all nodes? I thought the replica set itself was supposed to handle the replication?

For example, I have this Go code:

    package main
    
    import (
    	"context"
    	"go.mongodb.org/mongo-driver/mongo"
    	"go.mongodb.org/mongo-driver/mongo/options"
    	"go.mongodb.org/mongo-driver/mongo/readpref"
    	"log"
    	"time"
    )
    
    func main() {
    	log.Println("Hello World!")
    
    	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    	defer cancel()
    
    	client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://MongoInsance:27017"))
    	if err != nil {
    		log.Fatal("Failed to connect to Mongo DB", err)
    	}
    
    	if err := client.Ping(ctx, readpref.Primary()); err != nil {
    		log.Fatal("Not actually connected ", err)
    	}
    
    	res, err := client.ListDatabases(ctx, nil)
    	if err != nil{
    		log.Fatal("Failed to list databases")
    	}
    
    	for _, val := range res.Databases {
    		log.Println(val.Name)
    	}
    	defer disconnect(client, &ctx)
    }
    
    func disconnect(client *mongo.Client, ctx *context.Context) {
    	if err := client.Disconnect(*ctx); err != nil {
    		panic(err)
    	}
    }

But the response I get when running said code gives the error:

Not actually connected server selection error: context deadline exceeded, current topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr: mqtt-ingester-db-mast:27017, Type: Unknown, Last error: connection() error occured during connection handshake: dial tcp: lookup mqtt-ingester-db-mast: no such host }, { Addr: mqtt-ingester-db-rep1:27017, Type: Unknown, Last error: connection() error occured during connection handshake: dial tcp: lookup mqtt-ingester-db-rep1: no such host }, { Addr: mqtt-ingester-db-rep2:27017, Type: Unknown, Last error: connection() error occured during connection handshake: dial tcp: lookup mqtt-ingester-db-rep2: no such host }, ] }

Do I actually need to expose all the replica sets as well?
Currently I have the primary node and 2 secondary nodes running in docker on host MongoInstance, with the primary node attached to port 27017:

docker-compose.yml

    services:
      ...
      mqtt-ingester-db-mast:
        container_name: mqtt-ingester-db-mast
        restart: always
        image: mongo:latest
        ports:
          - 27017:27017
        volumes:
          - 'mongo_main:/data/db'
        entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
      mqtt-ingester-db-rep1:
        container_name: mqtt-ingester-db-rep1
        restart: always
        image: mongo:latest
        expose:
          - 27017
        volumes:
          - 'mongo_rep1:/data/db'
        entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
      mqtt-ingester-db-rep2:
        container_name: mqtt-ingester-db-rep2
        restart: always
        image: mongo:latest
        expose:
          - 27017
        volumes:
          - 'mongo_rep2:/data/db'
        entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
      mqtt-ingester-setup:
        image: mongo:latest
        container_name: mqtt-ingester-setup
        links:
          - mqtt-ingester-db-mast:mqtt-ingester-db-mast
          - mqtt-ingester-db-rep1:mqtt-ingester-db-rep1
          - mqtt-ingester-db-rep2:mqtt-ingester-db-rep2
        depends_on:
          - mqtt-ingester-db-mast
          - mqtt-ingester-db-rep1
          - mqtt-ingester-db-rep2
        volumes:
          - ./mqtt-ingester:/scripts
        restart: "no"
        entrypoint: [ "bash", "/scripts/mqtt_ingester_setup.sh" ]
      mqtt-ingester-explorer:
        container_name: mqtt-ingester-explorer
        restart: always
        image: mongo-express:latest
        ports:
          - '8081:8081'
        depends_on:
          - mqtt-ingester-db-mast
        links:
          - mqtt-ingester-db-mast:mongo
      ...

答案1

得分: 1

是的。客户端需要看到复制集中的所有节点,这样当主节点宕机时,它们可以进行故障转移。

英文:

> Do I actually need to expose all the replica sets as well?

Yes. Clients need to see all nodes in a replica set, so they can fail over when master goes down.

huangapple
  • 本文由 发表于 2021年6月8日 19:52:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/67886337.html
匿名

发表评论

匿名网友

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

确定