MongoDB Connection Error in Go: Target Machine actively refusing connection. Code from Mongo website used

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

MongoDB Connection Error in Go: Target Machine actively refusing connection. Code from Mongo website used

问题

我正在尝试使用MongoDB连接到Facebook API。

我收到了一个错误,错误信息如下:"server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: localhost:27017, Type: Unknown, Last error: connection() error occured during connection handshake: dial tcp [::1]:27017: connectex: No connection could be made because the target machine actively refused it. }, ] }"

为了解决这个错误,我尝试切换到服务器 "mongodb://localhost:27018" 和 "mongodb://localhost:27019"。

我也扫描了我的代码,但没有找到任何错误。我对编程还很陌生,所以非常感谢任何帮助、额外的建议或知识。

以下是代码:

package main

import (
	"context"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"log"
)

// Client 是导出的Mongo数据库客户端
var Client *mongo.Client

// ConnectDatabase 用于连接MongoDB数据库
func ConnectDatabase() {
	log.Println("Database connecting...")
	// 设置客户端选项
	clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")

	// 连接MongoDB
	client, err := mongo.Connect(context.TODO(), clientOptions)
	Client = client
	if err != nil {
		log.Fatal(err)
	}

	// 检查连接
	err = Client.Ping(context.TODO(), nil)

	if err != nil {
		log.Fatal(err)
	}

	log.Println("Database Connected.")
}
英文:

I am working on connecting to the Facebook API using MongoDB.

I am receiving an error that states: "server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: localhost:27017, Type: Unknown, Last error: connection() error occured during connection handshake: dial tcp [::1]:27017: connectex: No connection could be made because the target machine actively refused it. }, ] }"

In attempts to fix this error, I have tried switching to servers "mongodb://localhost:27018" and "mongodb://localhost:27019"

I have also scanned my code for errors but can't find anything. I am still very new to programming, so any help or additional advice/ knowledge is appreciated.

Code Below:

package main

import (
	"context"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"log"
)

// Client is exported Mongo Database client
var Client *mongo.Client

// ConnectDatabase is used to connect the MongoDB database
func ConnectDatabase() {
	log.Println("Database connecting...")
	// Set client options
	clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")

	// Connect to MongoDB
	client,err := mongo.Connect(context.TODO(), clientOptions)
	Client = client
	if err != nil{
		log.Fatal(err)
	}

	// Check the connection
	err = Client.Ping(context.TODO(), nil)

	if err != nil {
		log.Fatal(err)
	}

	log.Println("Database Connected.")
}

答案1

得分: 0

找到了非常简单的答案。我没有下载MongoDB社区服务器并将其安装为服务!

安装后,我使用http://localhost:8000/运行了我的代码,立即就可以工作了。

链接在这里:https://www.mongodb.com/try/download/community

英文:

Found the incredibly simple answer. I hadn't downloaded the MongoDB community server and installed it as a service!

After installing I ran my code with http://localhost:8000/ and it worked immediately.

Link here: https://www.mongodb.com/try/download/community

huangapple
  • 本文由 发表于 2021年9月15日 12:52:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/69187281.html
匿名

发表评论

匿名网友

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

确定