使用docker-compose的Golang与Cassandra数据库:无法连接(gocql)

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

Golang with Cassandra db using docker-compose : cannot connect (gocql)

问题

我正在尝试设置一个Cassandra数据库,并使用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

然后我尝试在我的Go应用程序(容器化)中使用gocql连接到它:

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 !

答案1

得分: 3

每个容器都有自己的本地主机地址(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)

答案2

得分: 0

如果两个容器都使用桥接网络,你需要在两个容器中指定网络名称,并且在你的应用容器中,主机名称将是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

huangapple
  • 本文由 发表于 2022年3月9日 01:03:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/71398835.html
匿名

发表评论

匿名网友

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

确定