连接到RethinkDB时出现错误(来自Golang)

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

Error on connecting to rethinkDB from golang

问题

我使用rethinkdb运行了一个docker容器,然后运行了一个连接数据库的go文件,但是在连接时出现了错误。

大家好,我需要关于rethinkdb的帮助,我运行了docker容器:

docker run --name rth -p 8100:8080 -d rethinkdb

然后访问http://localhost:8100,可以看到rethinkdb的主页,所以一切都正常。但是当我尝试从golang连接到数据库时,出现了一些错误:

package main

import (
	r "gopkg.in/rethinkdb/rethinkdb-go.v6"
	"log"
	"fmt"
)

func main() {
	_, err := r.Connect(r.ConnectOpts{
		Address:  "localhost:28015",
		Database: "test",
	})
	if err != nil {
		fmt.Println(err)
		log.Fatal("Could not connect")
	}
}

运行go run main.go后,我得到了以下错误:

rethinkdb: dial tcp 127.0.0.1:28015: connect: connection refused
2023/05/18 01:38:39 Could not connect
exit status 1

我认为这是因为端口号不正确(28015),但是如果我更改端口号,问题仍然存在,除非端口号为8100。如果我将端口号改为8100,我会得到以下错误:

rethinkdb: Unexpected EOF: HTTP/ 400 Bad Request

2023/05/18 01:38:52 Could not connect
exit status 1

也许有人知道如何解决这个问题。

英文:

I ran docker container with rethinkdb, then run go file for connecting to database, but I have errors while connecting.

Hi, everyone. I need help with rethinkdb, I ran docker container:

docker run --name rth -p 8100:8080 -d rethinkdb

Then went to http://localhost:8100 and has main page of rethinkdb, so everythink is fine. But when I try to connect to databases from golang I have some errors:

package main

import (  
	r "gopkg.in/rethinkdb/rethinkdb-go.v6"
	"log"
	"fmt"
)

func main() {
	_, err := r.Connect(r.ConnectOpts{
		Address: "localhost:28015",
		Database: "test",
	})
	if err != nil {
		fmt.Println(err)
		log.Fatal("Could not connect")
  	}
}

After running go run main.go I have this error:

rethinkdb: dial tcp 127.0.0.1:28015: connect: connection refused
2023/05/18 01:38:39 Could not connect
exit status 1

I thank that this happens because of incorrect port(28015), but if I change it, I have same problem, except port = 8100. If I put 8100 instead of 28015 and have this error:

rethinkdb: Unexpected EOF: HTTP/ 400 Bad Request

2023/05/18 01:38:52 Could not connect
exit status 1

May be someone know how to fix this)

答案1

得分: 1

-p 8100:8080 将容器中的端口 8080 映射到主机上的端口 8100。你没有映射其他端口,所以当你尝试访问除了 8100 之外的任何端口(例如 127.0.0.1:28015),你的请求将无法到达容器。主机上可能有其他程序正在监听该端口,或者该端口上没有任何监听。

你提到你能够通过 http://localhost:8100 访问管理界面;如果你查看日志,你会注意到类似以下的内容:

Listening for administrative HTTP connections on port 8080
Listening for client driver connections on port 28015

因此,服务器正在监听多个端口的连接。端口 8080(你映射到 8100)是管理界面的 HTTP 接口,而 28015 是用于驱动程序连接的端口(在文档中有介绍)。你的代码尝试连接到端口 28015(这是正确的),但你没有映射该端口,所以在主机上无法访问;你可以通过以下方式修复:

docker run --name rth -p 8100:8080 -p 28015:28015 -d rethinkdb

这将把容器中的端口 28015 映射到主机上的端口 28015(如果需要,你可以使用不同的主机端口;只需记得更新代码)。现在我们可以成功连接,例如:

package main

import (
	"fmt"
	"log"

	r "gopkg.in/rethinkdb/rethinkdb-go.v6"
)

func main() {
	_, err := r.Connect(r.ConnectOpts{
		Address:  "localhost:28015",
		Database: "test",
	})
	if err != nil {
		fmt.Println(err)
		log.Fatal("Could not connect")
	}
	fmt.Println("Connected")
}
英文:

-p 8100:8080 maps port 8080 in the container to port 8100 on your host. You don't map any other ports so, when you attempt to access any port other than 8100 (for example 127.0.0.1:28015), your request will not reach the container. Something else on the host may be listening on the port, or there may be nothing listening.

You mentioned that you are able to access the admin interface on http://localhost:8100; if you check the logs you will notice something like:

Listening for administrative HTTP connections on port 8080
Listening for client driver connections on port 28015

So the server is listening for connections on multiple ports. Port 8080 (which you map to 8100) is the admin HTTP interface and 28015 is for driver connections (this is covered in the docs). Your code is attempting to connect to port 28015 (which is correct) but you are not mapping that port so it's inaccessible on the host; fix this with:

docker run --name rth -p 8100:8080 -p 28015:28015 -d rethinkdb

This will map port 28015 in the container to port 28015 on the host (you can use a different host port if you want; just remember to update the code). We can now successfully connect with something like:

package main

import (
	"fmt"
	"log"

	r "gopkg.in/rethinkdb/rethinkdb-go.v6"
)

func main() {
	_, err := r.Connect(r.ConnectOpts{
		Address:  "localhost:28015",
		Database: "test",
	})
	if err != nil {
		fmt.Println(err)
		log.Fatal("Could not connect")
	}
	fmt.Println("Connected")
}

huangapple
  • 本文由 发表于 2023年5月18日 06:50:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276673.html
匿名

发表评论

匿名网友

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

确定