Golang 与 Cassandra db 使用 docker-compose:无法连接(gocql)
我正在尝试设置 cassandra DB 并使用 golang 应用程序连接到它。
这是我的 docker-compose
version: "3.6"
services:
cassandra:
image: cassandra:4.0
ports:
- 9042:9042
volumes:
- ~/apps/cassandra:/var/lib/cassandra
environment:
- CASSANDRA_CLUSTER_NAME=mycluster
myapp:
...
ports:
- 4242:4242
- 4243:4243
depends_on:
- cassandra
...
networks:
default:
driver: bridge
启动 Cassandra
我使用docker-compose up cassandra
,然后等待它准备就绪。
然后我尝试在本地使用连接到 Cassandra
> cqlsh
Connected to mycluster at 127.0.0.1:9042
,然后尝试使用 gocql
在我的 go 应用程序(dockerized)中连接到它
cluster := gocql.NewCluster("127.0.0.1")
session, err := cluster.CreateSession()
(还尝试将元素添加为 Consistency
, ProtoVersion=4
等相同的结果)
然后它说:
Cannot connect to db: gocql: unable to create session: unable to discover protocol version: dial tcp 127.0.0.1:9042: connect: connection refused
你呢。知道为什么无法连接吗?
谢谢 !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个容器都有自己的本地主机 (
127.0.0.1
) 地址 - 您需要连接到您计算机的 IP 地址(如果您使用bridge
),或者最好通过以下方式连接名称 (cassandra
)Each container has its own localhost (
127.0.0.1
) address - you need to connect to IP address of your machine (if you usebridge
), or maybe better to connect by the name (cassandra
)如果两个容器都使用桥接网络,则需要在两个容器和应用程序容器中指定网络名称,主机将是 cassandra (docker) 容器。
If both containers using bridge network you need to specify the network name in both containers and in your app container the host will be cassandra (docker) container.