无法使用cqlsh连接Cassandra Docker

18

我在运行Cassandra的Docker容器:

docker pull cassandra
run --name cassandra -p 9042:9042 -p 9160:9160   -d cassandra  

netstat -tpln是什么:

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

LISTEN    -  tcp6       0      0 [::]:9160               [::]:*
LISTEN    -  tcp6       0      0 [::]:9042               [::]:*

本地cqlsh连接到C*成功:

docker exec -it cassandra /bin/bash
#cqlsh
Connected to Test Cluster at 127.0.0.1:9042. 
[cqlsh 5.0.1 | Cassandra 3.1.1 | CQL spec 3.3.1 | Native protocol v4] 
Use HELP for help.
cqlsh> show host     
Connected to Test Cluster at 127.0.0.1:9042.  

我安装了本地的 cqlsh:

$cqlsh --version
cqlsh 4.1.1

但是,我无法从本地主机连接到Docker容器:

$sqlsh
Traceback (most recent call last):
  File "/usr/sbin/cqlsh", line 2067, in <module>
    main(*read_options(sys.argv[1:], os.environ))
  . . .
  File "/home/akalend/src/cqlsh_standalone/lib/thrift-python-internal-only-0.9.1.zip/thrift/transport/TSocket.py", line 103, in read
socket.error: [Errno 104] Connection reset by peer

所以,我无法从本地主机php驱动程序连接。

我如何使用我的php脚本和cqlsh连接cassandra docker?

为什么docker将端口映射到tcp6而不是tcp4? 解决方案

为什么本地cqlsh(版本4.1)通过9160端口连接,但是docker容器cqlsh(版本5.0.1)通过9042端口连接?


添加的信息:

如果运行内容器如下:

run --name cassandra -p 127.0.0.1:9042:9042 -p 127.0.0.1:9160:9160   -d cassandra 

我听过ip4端口:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address  State       PID/Program name
tcp        0      0 127.0.0.1:9160 0.0.0.0:*       LISTEN      2454/docker-proxy
tcp        0      0 127.0.0.1:9042 0.0.0.0:*       LISTEN      2462/docker-proxy

但我与cqlsh和php没有连接

socket.error: [Errno 104] Connection reset by peer

PHP Fatal error:  Uncaught exception 'Cassandra\Exception\RuntimeException' with message 'No hosts available for the control connection' in /home/akalend/projects/test/cassa/test.php:7
Stack trace:
#0 /home/akalend/projects/test/cassa/test.php(7): Cassandra\DefaultCluster->connect('system')
#1 {main} thrown in /home/akalend/projects/test/cassa/test.php on line 7

如果您在Docker中运行Cassandra,则它将在自己的网络中运行;因此,您可以创建一个Docker网络并将其用于Cassandra,并将相同的网络用于需要连接到它的其他容器。https://medium.com/techlogs/cassandra-in-docker-3e5eb98cdb22 - Alex Punnen
1个回答

42

尝试将您的docker run命令更改为:

docker pull cassandra
docker run --name cassandra -p 127.0.0.1:9042:9042 -p 127.0.0.1:9160:9160   -d cassandra 

这将确保Docker容器映射到IPv4。

9160 - Thrift client API
9042 - CQL native transport port

从你的PHP应用程序,你需要连接到Thrift端口。请按照示例中的方式操作。

在上面的示例中,如果要从同一台计算机连接到正在运行容器的Cassandra,则仍然可以使用相同的TSocket('127.0.0.1', 9160)

如果您计划从不同的机器连接,则必须在其中使用TSocket('IP/Domain name', 9160),其中IP/域名是运行docker容器的计算机的标识符。

如果您的PHP应用程序在同一台机器上的另一个Docker容器中,则首先必须链接容器,然后可以在其中使用TSocket('alias name', 9160),其中别名是您为该链接指定的名称。

try {
  // Make a connection to the Thrift interface to Cassandra
  $socket = new TSocket('127.0.0.1', 9160);

谢谢,但我与PHP和Cqlsh没有联系。请查看问题中添加的文本。 - Alexandre Kalendarev
解决了你的问题吗?如果是这样,请接受解决方案,以便其他遇到类似问题的人可以从中受益。 - Phani
问题已经得到解决。我从Cassandra源安装了新的cqlsh,并与Docker容器建立了连接,但是无法从PHP客户端建立连接。因此,我可以尝试使用另一个PHP客户端,但是我没有时间进行实验。感谢您的帮助。 - Alexandre Kalendarev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接