cannot connect to cassandra docker with cqlsh

Alexandre Kalendarev picture Alexandre Kalendarev · Jan 7, 2016 · Viewed 24.2k times · Source

I'm run Cassandra docker container:

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

The netstat -tpln is:

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

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

Connection to C* from local cqlsh is Ok:

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.  

I install the local cqlsh:

$cqlsh --version
cqlsh 4.1.1

but, I don't connecton with docker container from localhost:

$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

So, I don't connection from localhost php-driver.

How I can connection cassandra docker with my php script & cqlsh?

Why docker mapping port to tcp6, do not tcp4 ? resolve

Why the local cqlsh (version 4.1) connect by 9160 port, but docker container cqlsh(version 5.0.1) connect by 9042 port?


added info

If run conteiner as:

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

I have listen ip4 ports:

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

but I havn't connection with 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

Answer

Phani picture Phani · Jan 7, 2016

Try to change your docker run command as :

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

This will ensure the docker container maps to the IPv4.

9160 - Thrift client API
9042 - CQL native transport port

From your PHP application, you have to connect to the Thrift port. Please follow the example as in http://support.qualityunit.com/942764-Example-of-PHP-application-readingwriting-to-Cassandra In the above example, for connecting to the cassandra container from the same machine where the container is running, you can still use the same TSocket('127.0.0.1', 9160).

If you plan to connect from a different machine, then you have to use TSocket('IP/Domain name', 9160) in this, the IP/ Domain name is the identifier for the machine where the docker container is running.

If your PHP application is in another docker container on the same machine, first you have to link the containers, then you can use the TSocket('alias name', 9160) in this, the alias name is the name you have for the link.

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