首页 > 解决方案 > 尝试打开与 ClickhouseDB 的连接时程序挂起?

问题描述

我正在尝试从我的应用程序连接到本地 ClickhouseDB 以执行一些原始查询。当我尝试打开连接时出现问题。它永远不会从方法调用中返回。

ClickhouseDB 在 docker 容器中启动。

using (ClickHouseConnection conn = connFactory.CreateConnection(new ClickHouseConnectionSettings(connectionString)))
            {
                var cmd = conn.CreateCommand();

                cmd.CommandText = "select * from default.table_name";

                conn.Open(); // it never goes past this line
                using (var reader = cmd.ExecuteReader())
                ...

我想在 之后打开一个连接conn.Open()并继续程序到执行阅读器的下一行。

注意: 如果我尝试从 python 控制台连接,绝对没有问题。

from clickhouse_driver import Client
client = Client(host='localhost')
client.execute('select * from default.table_name')
# [(4, 'four'), (5, 'five'), (1, 'one'), (2, 'two'), (3, 'three')]

更新: 连接超时后的堆栈跟踪:

System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at InhouseNamespace.ClickHouse.Impl.ProtocolFormatter.ReadBytes(Int32 i)
   ...

部署 Clickhouse 的 docker-compose。我不是 docker 专家,所以当数据库运行时我对它很满意,并且我能够通过 Tabix Web UI 连接到它:

version: "3.7"
services:
  clickhouse:
    image: yandex/clickhouse-server
    ports:
    - "8123:8123"
    - "9000:9000"
    - "9009:9009"

    ulimits:
     nproc: 65535
     nofile:
      soft: 262144
      hard: 262144
 client:
    image: yandex/clickhouse-client
    command: ['--host', 'server']

更新: 连接到非本地 CassandraDB 后,问题消失了,这意味着连接到我的本地 CassandraDb(在 docker 容器中)时出现问题。

标签: c#dockerdocker-composeado.netclickhouse

解决方案


端口 8123 用作 HTTP 接口的默认端口。但是在使用时:

conn.Open();
using (var reader = cmd.ExecuteReader())

通信是通过本地 Clickhouse 客户端完成的,因此必须使用端口 9000。

要解决此问题,请更改连接字符串中的端口。完整的连接字符串将如下所示:connString = "Host=127.0.0.1;Port=9000;User=default;Password=;Database=default;Compress=True;CheckCompressedHash=False;SocketTimeout=60000000;Compressor=lz4"


推荐阅读