我运行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连接 .
How I can connection cassandra docker with my php script & cqlsh?
为什么docker映射端口到tcp6,不要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
如果运行conteiner为:
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
1 回答
尝试将docker run命令更改为:
这将确保docker容器映射到IPv4 .
从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)
,别名是您对该链接的名称 .