使用python驱动程序连接到停靠的clickhouse服务器的问题

wooyq4lh  于 2021-07-15  发布在  ClickHouse
关注(0)|答案(2)|浏览(725)

我在使用python驱动程序连接windows docker容器中的clickhouse时遇到问题。clickhouse服务器正在我的电子驱动器上运行,在端口8123的docker容器中。我可以很容易地用这个软件包在r连接https://github.com/hannesmuehleisen/clickhouse-r 因此:

conn = DBI::dbConnect(clickhouse::clickhouse(), 
              host = "my_ip", 
              port = 8123L,
              user = "myun",
              password = "mypwd")

但是当我在python中使用https://clickhouse-driver.readthedocs.io/en/latest/quickstart.html 我遇到一个问题:

from clickhouse_driver import Client
client = Client(host = 'my_ip',
                port = '8123',
                user='myun',
                password='mypwd',
                secure=True,
                verify=False,
                database='db_name')

print(client.execute('SELECT now()'))

File "d:\ProgramData\Anaconda3\lib\site-packages\clickhouse_driver\connection.py", line 249, in connect
    '{} ({})'.format(e.strerror, self.get_description())

NetworkError: Code: 210. [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:777) (my_ip:8123)

有人知道问题是什么吗?
更新:
尝试安全=f并获得:

File "d:\ProgramData\Anaconda3\lib\site-packages\clickhouse_driver\connection.py", line 243, in connect
    '{} ({})'.format(e.strerror, self.get_description())

SocketTimeoutError: Code: 209. None
xmakbtuz

xmakbtuz1#

clickhouse服务器和客户端之间有两种通信协议:http(端口8123)和native(端口9000)。
http适用于curl/wget和其他工具。大多数clickhouse客户端使用http进行数据传输。在你的案子里包括r。但是有些客户机使用本机协议(go和这个python客户机)。这个协议应该比http更有效。https://clickhouse-driver.readthedocs.io/en/latest/#user-s形导轨
从服务器容器中公开端口9000并在客户端中使用。此端口也是此客户端中的默认端口。

nx7onnlm

nx7onnlm2#

让我们分别考虑不安全通信和安全通信:
tcp(非安全通信)
clickhouse驱动程序通过9000端口上的本机协议与clickhouse服务器通信
docker容器应将端口9000发布到主机

docker run -d -p 9000:9000 --ulimit nofile=262144:262144 yandex/clickhouse-server

应用程序代码

client = Client(host='localhost',
                port='9000', # this param can be missed because port 9000 is used by default
                # ..
                database='test')

tcp(安全通信)
clickhouse驱动程序通过9440端口上的本机协议与clickhouse服务器通信
docker容器应将端口9440发布到主机

docker run -d -p 9440:9440 --ulimit nofile=262144:262144 yandex/clickhouse-server

配置clickhouse
在容器上执行交互式bash shell:

docker exec -it {CONTAINER_ID} bash

在容器内进行所需的更改:

apt-get update

# modify config-file

apt-get install nano

# uncomment the '<tcp_port_secure>'-section in config-file & save changes

nano /etc/clickhouse-server/config.xml

# generate a self-signed certificate & 'dhparam.pem'-file

apt-get install openssl

openssl req -subj "/CN=my.host.name" -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout /etc/clickhouse-server/server.key -out /etc/clickhouse-server/server.crt

openssl dhparam -out /etc/clickhouse-server/dhparam.pem 512 # 4096

# set required access mode to 'server.key'-file

chmod 644 /etc/clickhouse-server/server.key

# exit from interactive mode

exit

重新启动容器:

docker restart {CONTAINER_ID}

应用程序代码

client = Client(host='localhost',
                port='9440', # this param can be missed because port 9440 is used by default
                secure=True,
                verify=False,
                # ..
                database='test')

评论:
docker容器中的ssl的手动配置仅用于测试。最好将所需的ssl文件和config.xml装载到容器中,或者创建带有所需更改的自定义docker映像
请参阅altinity-clickhouse networking,第2部分—深入了解ssl配置的文章

相关问题