> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# system.connections

> 显示原生 TCP 和 HTTP 查询接口上当前打开的客户端连接。连接在执行查询时处于 `active` 状态，在等待下一个查询时处于 `idle` 状态。

## 描述

显示原生 TCP 和 HTTP 查询接口上已打开的客户端连接。
连接正在执行查询时，其状态为 `active`；连接正在等待下一个查询时，其状态为 `idle`。

该表不显示以下连接：

* 其他协议的连接，例如 MySQL、PostgreSQL 和 gRPC。
* 服务器间连接。
* 未执行过查询的 HTTP 连接，例如 `/ping` 和 Prometheus 连接。

仅当服务器设置 `collect_connection_metrics` 为 `true` 时，服务器才会收集连接信息。如果该设置为 `false`，则该表为空。
更改该设置无需重启服务器 (`SYSTEM RELOAD CONFIG`) ：

* 将该设置改为 `false` 后，该表会被清空，服务器也会停止登记新连接。
* 将该设置改为 `true` 后，已打开的连接会在执行下一个查询时出现在表中。

TCP 连接在完成身份验证后被添加到表中，并在连接关闭时被移除。
HTTP 连接在执行第一个查询时被添加到表中，并在套接字关闭时被移除。
因此，HTTP keep-alive 连接在两次请求之间会显示为 `idle`。

没有 `SHOW CONNECTIONS` 特权的用户只能看到自己的连接。
该检查依据的是用户 ID，而非用户名。因此，用户被重命名后仍保留其连接，而使用旧用户名的新用户则看不到这些连接。

`connection_id` 是一个随机 UUID，因此在各服务器之间唯一，您可以合并多个副本上的表，例如使用 `clusterAllReplicas`。

`connected_time` 对 TCP 和 HTTP 含义相同：即服务器开始处理该套接字的时间。
该时间早于 TCP 握手、身份验证和第一个查询，因此可能早于该连接出现在表中的时间。

## 列

* `connection_id` ([UUID](https://clickhouse.com/docs/zh/reference/data-types/uuid.md)) — 连接的唯一标识符，为随机生成的 UUID。该标识符在多个服务器之间同样唯一，例如在 `clusterAllReplicas` 中。TCP 连接在完成身份验证后获得该标识符；HTTP 连接在执行第一个查询时获得该标识符。
* `protocol` ([LowCardinality(String)](https://clickhouse.com/docs/zh/reference/data-types/lowcardinality.md)) — 连接所用的协议：TCP (原生协议) 或 HTTP。
* `client_address` ([IPv6](https://clickhouse.com/docs/zh/reference/data-types/ipv6.md)) — 客户端的 IP 地址。位于 proxy 之后时，如果服务器信任 PROXY 协议或 `X-Forwarded-For` 请求头，则该值为客户端的地址。对于 HTTP，该值为最后一个请求的地址。
* `client_port` ([UInt16](https://clickhouse.com/docs/zh/reference/data-types/int-uint.md)) — 客户端的 TCP 端口。位于 proxy 之后时，如果服务器信任 PROXY 协议或 `X-Forwarded-For` 请求头，则该值为从中获取的端口。对于 HTTP，该值为最后一个请求的端口。
* `server_port` ([UInt16](https://clickhouse.com/docs/zh/reference/data-types/int-uint.md)) — 接受该连接的服务器端口。
* `user` ([String](https://clickhouse.com/docs/zh/reference/data-types/string.md)) — 已通过身份验证的用户名。对于 HTTP 连接，该值为最后一个请求的用户。keep-alive 连接上的每个请求都会单独进行身份验证。如果用户被重命名，在下一次身份验证之前，此列仍显示旧名称。访问检查依据的是用户 ID，而非用户名。
* `status` ([LowCardinality(String)](https://clickhouse.com/docs/zh/reference/data-types/lowcardinality.md)) — 连接的状态。`active`：当前有查询正在运行。`idle`：连接处于打开状态，正在等待下一个查询或下一个 HTTP 请求。
* `query_id` ([String](https://clickhouse.com/docs/zh/reference/data-types/string.md)) — 当前正在运行的查询的标识符。如果连接处于空闲状态，则为空。
* `client_name` ([String](https://clickhouse.com/docs/zh/reference/data-types/string.md)) — 客户端应用程序的名称。TCP 客户端会在握手时发送该名称。对于 HTTP，该值为最后一个请求的 `User-Agent` 请求头。
* `client_version_major` ([UInt64](https://clickhouse.com/docs/zh/reference/data-types/int-uint.md)) — 从 TCP 握手中获取的客户端主版本号。HTTP 连接为 0。
* `client_version_minor` ([UInt64](https://clickhouse.com/docs/zh/reference/data-types/int-uint.md)) — 从 TCP 握手中获取的客户端次版本号。HTTP 连接为 0。
* `client_version_patch` ([UInt64](https://clickhouse.com/docs/zh/reference/data-types/int-uint.md)) — 从 TCP 握手中获取的客户端补丁版本号。HTTP 连接为 0。
* `connected_time` ([DateTime](https://clickhouse.com/docs/zh/reference/data-types/datetime.md)) — 服务器开始处理该套接字的时间。该时间对 TCP 和 HTTP 含义相同：早于握手、身份验证和第一个查询。
* `last_query_time` ([Nullable(DateTime)](https://clickhouse.com/docs/zh/reference/data-types/nullable.md)) — 此连接上最后一个查询的开始时间。如果该连接从未运行过查询，则为 NULL。

## 示例

```sql theme={null}
SELECT connection_id, protocol, user, status, query_id, client_name, connected_time, last_query_time
FROM system.connections
FORMAT Vertical;
```

```sql theme={null}
SELECT hostName(), protocol, status, count()
FROM clusterAllReplicas(default, system.connections)
GROUP BY ALL;
```

```response theme={null}
Row 1:
──────
connection_id:   0d6c1b7e-31a2-4f5c-9a43-7e2b8c5d1f90
protocol:        TCP
user:            default
status:          active
query_id:        845dcadd-7249-4ce4-8c3d-c08032594155
client_name:     ClickHouse client
connected_time:  2026-03-17 09:18:18
last_query_time: 2026-03-17 09:18:27

Row 2:
──────
connection_id:   a4f3e2d1-8b7c-4c6d-9e5f-1a2b3c4d5e6f
protocol:        HTTP
user:            default
status:          idle
query_id:
client_name:     curl/8.5.0
connected_time:  2026-03-17 09:18:14
last_query_time: 2026-03-17 09:18:14
```
