我正在尝试连接到在 docker 映像中运行的 Neo4j 集群,但使用 neo4j-go-driver/v5
连接总是失败docker-compose.yml
version: '3.8'
services:
crebito-debug:
ports:
- 8080:8080
build:
context: .
dockerfile: ${MODE}.dockerfile
container_name: server-debug
volumes:
- ./src:/src
depends_on:
- 'neo4j'
env_file:
- .env
restart: always
neo4j:
image: neo4j:latest
ports:
# bolt port (tcp)
- 7687:7687
# http port
- 7474:7474
volumes:
- ./neo4j/data:/data/
- ./neo4j/conf:/conf/
- ./neo4j/import:/import/
container_name: neo4j
restart: on-failure
environment:
# - NEO4J_AUTH=neo4j/password
- NEO4J_AUTH=none
volumes:
db-neo:
当我实例化一个新的驱动程序时,它不会产生任何问题,但是在测试连接时,它告诉我它尚未连接
conn, err := neo4j.NewDriverWithContext("neo4j://localhost", neo4j.NoAuth())
if err != nil {
log.Panicf("error connecting database : error=%v", err)
}
if err := database.DBClient.VerifyConnectivity(database.DBContext); err != nil {
log.Panicf("error in database connectivity : error=%v", err)
}
这引发了我以下错误。
panic: error in database connectivity : error=ConnectivityError: Unable to retrieve routing table from localhost:7687: dial tcp 127.0.0.1:7687: connect: connection refused
奇怪的是,端口 7474 上的连接工作正常,我可以通过浏览器访问数据库并运行查询。
我已经尝试过使用身份验证凭据,将 URI 更改为 0.0.0.0 或 127.0.0.1,使用端口 7474 连接到驱动程序,在不同的操作系统端口上公开端口 7687,但都没有有效并且错误保持不变。 有谁知道如何使用 Neo4j Bolt 关闭此连接?
容器有自己的
localhost
,因此您尝试连接到应用程序容器中的 Neo4j 实例,而您显然没有运行该实例。您需要连接到 neo4j 容器。
您可以通过使用该容器的名称作为 DNS 名称来实现这一点。 e.连接到
neo4j://neo4j
。