Golang 与 Cassandra db 使用 docker-compose:无法连接(gocql)

发布于 2025-01-12 17:49:38 字数 1121 浏览 6 评论 0 原文

我正在尝试设置 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()

(还尝试将元素添加为 ConsistencyProtoVersion=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

你呢。知道为什么无法连接吗?

谢谢 !

I am trying to setup a cassandra DB and connect to it with a golang app.

this is my 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

I start the Cassandra using

docker-compose up cassandra

and then I wait it to be ready.

Then I try to connect to Cassandra in local using

> cqlsh
Connected to mycluster at 127.0.0.1:9042

and then I try to connect to it in my go app (dockerized) using gocql

    cluster := gocql.NewCluster("127.0.0.1")
    session, err := cluster.CreateSession()

( also tried to add element as Consistency, ProtoVersion=4 etc. same results)

it says then :

Cannot connect to db: gocql: unable to create session: unable to discover protocol version: dial tcp 127.0.0.1:9042: connect: connection refused

Do you. have any idea why it can't connect?

thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆沫 2025-01-19 17:49:38

每个容器都有自己的本地主机 (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 use bridge), or maybe better to connect by the name (cassandra)

︶ ̄淡然 2025-01-19 17:49:38

如果两个容器都使用桥接网络,则需要在两个容器和应用程序容器中指定网络名称,主机将是 cassandra (docker) 容器。

services:
  cassandra:
    image: cassandra:4.0
    container_name: cassandra
    
    ports:
      - 9042:9042

    volumes:
      - ~/apps/cassandra:/var/lib/cassandra

    networks:
      - default
    environment:
      - CASSANDRA_CLUSTER_NAME=mycluster

  myapp:
    ...
    ports:
      - 4242:4242
      - 4243:4243
    depends_on:
      - cassandra
    networks:
      - default

    environment:
      - HOSTS=cassandra
      ...

networks:
  default:
    driver: bridge

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.

services:
  cassandra:
    image: cassandra:4.0
    container_name: cassandra
    
    ports:
      - 9042:9042

    volumes:
      - ~/apps/cassandra:/var/lib/cassandra

    networks:
      - default
    environment:
      - CASSANDRA_CLUSTER_NAME=mycluster

  myapp:
    ...
    ports:
      - 4242:4242
      - 4243:4243
    depends_on:
      - cassandra
    networks:
      - default

    environment:
      - HOSTS=cassandra
      ...

networks:
  default:
    driver: bridge
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文