python 连接 clickhouse 的端口问题

时间:2025-01-24 20:53:39
   <!-- It is the name that will be shown in the clickhouse-client.
         By default, anything with "production" will be highlighted in red in query prompt.
    -->
    <!--display_name>production</display_name-->

    <!-- Port for HTTP API. See also 'https_port' for secure connections.
         This interface is also used by ODBC and JDBC drivers (DataGrip, Dbeaver, ...)
         and by most of web interfaces (embedded UI, Grafana, Redash, ...).
      -->
    <http_port>8123</http_port>

    <!-- Port for interaction by native protocol with:
         - clickhouse-client and other native ClickHouse tools (clickhouse-benchmark, clickhouse-copier);
         - clickhouse-server with other clickhouse-servers for distributed query processing;
         - ClickHouse drivers and applications supporting native protocol
         (this protocol is also informally called as "the TCP protocol");
         See also 'tcp_port_secure' for secure connections.
    -->
    <tcp_port>9000</tcp_port>

注意到可以使用两个端口,8123 和 9000 分别接收 http 协议和tcp协议。
如果用 jdbc 连接,端口为 8123
如果用 driver 连接,端口为 9000

from clickhouse_driver import Client

client = Client(host=host, port=port, user=user, password=password, database='default')

client  = get_client()
client.execute("show tables;")
print(client.execute("select * from test_arr"))
client.disconnect()

此时如果用8123端口则会得到如下报错:
clickhouse_driver.: Code: 102. Unexpected packet from server 192.168.137.101:8123 (expected Hello or Exception, got Unknown packet)