简体   繁体   English

如何使用Python访问Docker容器中的ScyllaDB?

[英]How can I access ScyllaDB in Docker container using Python?

I have set up Docker container with ScyllaDB in accordance to the official doc . 我已经根据官方文档使用ScyllaDB设置了Docker容器。 I created keyspace and table. 我创建了键空间和表。 Now I would like to insert some data to this table. 现在,我想向该表中插入一些数据。 I wonder have can I do this? 我想知道我能做得到吗? I found example: 我找到了例子:

import cql
con = cql.connect('localhost', 9160,  'logs', cql_version='3.0.0')
print ("Connected!")
cursor = con.cursor()
CQLString = "INSERT INTO event_attend (event_id, event_type, event_user_id) VALUES (131, 'Party', 3156);"
cursor.execute(CQLString)

however I get error shown below. 但是我得到如下所示的错误。 I can't find any examples that show how it could be done with Docker. 我找不到任何示例可以说明如何使用Docker来完成。 Any ideas? 有任何想法吗?

No handlers could be found for logger "thrift.transport.TSocket"
Traceback (most recent call last):
  File "scylla_test.py", line 2, in <module>
    con = cql.connect('localhost', 9160,  'logs', cql_version='3.0.0')
  File "/usr/local/lib/python2.7/dist-packages/cql/connection.py", line 143, in connect
    consistency_level=consistency_level, transport=transport)
  File "/usr/local/lib/python2.7/dist-packages/cql/connection.py", line 59, in __init__
    self.establish_connection()
  File "/usr/local/lib/python2.7/dist-packages/cql/thrifteries.py", line 151, in establish_connection
    self.transport.open()
  File "/usr/local/lib/python2.7/dist-packages/thrift/transport/TTransport.py", line 271, in open
    return self.__trans.open()
  File "/usr/local/lib/python2.7/dist-packages/thrift/transport/TSocket.py", line 113, in open
    raise TTransportException(TTransportException.NOT_OPEN, msg)
thrift.transport.TTransport.TTransportException: Could not connect to any of [('::1', 9160, 0, 0), ('127.0.0.1', 9160)]

Can you check with port 9042, which is the default CQL port. 您能否使用端口9042(默认的CQL端口)进行检查。 The 9160 is the thrift port, which is less used these days. 9160是节俭的端口,如今已很少使用。 Also, can you make sure you are forwarding the ports, for example: 另外,是否可以确保转发端口,例如:

docker run --name some-scylla \\ --volume /var/lib/scylla:/var/lib/scylla \\ -p 9042:9042 -p 7000:7000 -p 7001:7001 -p 7199:7199 \\ -p 9160:9160 -p 9180:9180 -p 10000:10000 \\ -d scylladb/scylla --overprovisioned 1

The 'localhost' address of the container is not accessible outside the container itself. 在容器本身外部无法访问容器的“ localhost”地址。

You might want to do the following. 您可能需要执行以下操作。 First make sure that ScyllaDB nodes are up using 首先确保ScyllaDB节点已使用

docker exec -it scylla nodetool status

It should output something similar to: 它应该输出类似于:

Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address     Load       Tokens       Owns (effective)  Host ID                               Rack
UN  172.17.0.2  153.41 KB  256          100.0%            70a72849-5608-47d5-8c8d-0f1db0f57444  rack1

If the status of all nodes is "down" (DN) you might need to first bring them up. 如果所有节点的状态均为“关闭”(DN),则可能需要先将它们启动。

Now, use the displayed address of the node that is up. 现在,使用显示的节点地址。 If you have multiple nodes in your cluster, any of those addresses will do. 如果您的集群中有多个节点,则这些地址中的任何一个都可以。 You can first try to connect manually with cqlsh if it is installed in your system: 如果您的系统中已安装cqlsh,则可以首先尝试手动连接它:

cqlsh 172.17.0.2

Otherwise, just put it into your Python script and run it. 否则,只需将其放入您的Python脚本中并运行即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Python在Docker容器中获取主机ip? - How can I get the host ip in a docker container using Python? 我可以使用 python 安装 docker 容器吗? - Can I install a docker container using python? docker容器中的python代码如何访问外部oracle数据库? - How can a python code in a docker container access external oracle database? 如何访问同一 Docker 容器中的其他文件? - How can I access other files in the same Docker container? 如何使用Docker容器内的python套接字连接到服务器? - How can I connect to a server using python sockets inside a Docker container? 如何提高向ScyllaDB添加数据的性能? - How can I improve performance of adding data to ScyllaDB? 如何使用命令将参数传递给具有python入口点脚本的docker容器? - How can I pass arguments to a docker container with a python entry-point script using command? 如何在 docker 容器中安装 Python-dev? - How can i install Python-dev inside a docker container? Python docker module: How can I create a terminal shell to a docker container in python? - Python docker module: How can I create a terminal shell to a docker container in python? 设置 docker 容器,以便我可以访问 ubuntu 服务器上的 python 包 - Setting up docker container so that I can access python packages on ubuntu server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM