【问题标题】:cannot connect to cassandra docker with cqlsh无法使用 cqlsh 连接到 cassandra docker
【发布时间】:2016-04-11 07:03:42
【问题描述】:

我正在运行 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

但是,我没有从 localhost 连接 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

所以,我没有从 localhost php-driver 连接。

如何将 cassandra docker 与我的 php 脚本和 cqlsh 连接?

为什么 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-2.0 datastax cqlsh


【解决方案1】:

尝试将您的 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 端口。请按照http://support.qualityunit.com/942764-Example-of-PHP-application-readingwriting-to-Cassandra中的示例进行操作 在上面的示例中,为了从运行容器的同一台机器连接到 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 连接。查看添加的问题文本。
  • 它解决了您的问题吗?如果是,请接受解决方案,以便其他有类似问题的人从中受益。
  • 问题特别解决了。我从 cassandra 源安装了新的 cqlsh 并连接了 docker 容器,但没有来自 PHP 客户端的连接。所以,我可能会使用另一个 PHP 客户端,但没有时间进行实验。感谢您的帮助。
猜你喜欢
  • 2016-02-14
  • 2022-10-25
  • 2015-02-05
  • 1970-01-01
  • 2018-08-12
  • 2016-05-30
  • 2015-11-22
  • 2016-01-05
  • 2019-02-15
相关资源
最近更新 更多