pingcap/tidb
tidb-server
The TiDB SQL server daemon. It accepts MySQL-protocol connections, talks to TiKV/PD/TiFlash, and is the application most of this wiki covers.
Source
- Entry point:
cmd/tidb-server/main.go(≈ 1180 lines). - Build target:
make(default) ormake server. Output:bin/tidb-server. - Variants:
make server_debug,make server_check(extra admin checks),make server_coverage.
What main.go does, in order
cmd/tidb-server/main.go is mostly orchestration. The notable steps:
- Parse flags (a long flag list at the top of the file:
--store,--path,--config,--host,--port,--status,--metrics-addr,--tmp-storage-path,--lease,--keyspace-name,--standby-cli, …). - Load config from the TOML file (
pkg/config/config.go). - Set GOMAXPROCS via
automaxprocsand apply OS-level adjustments (pkg/util/sys/linux/,pkg/util/sys/storage/). - Initialise logging (
pkg/util/logutil/), pyroscope, and OpenTracing. - Open the storage (
kvstore.New) — selects the production driver (pkg/store/driver/tikv_driver.go) for--store=tikvor the in-process unistore for--store=unistore. - Bootstrap session and Domain:
session.BootstrapSessionruns cluster-bootstrap migrations on first start;domain.NewDomainbrings up the per-instance services (see Domain). - Start the SQL server (
pkg/server/): listeners, status HTTP, gRPC, metrics, etc. - Wire DDL, plugins, telemetry, runaway, TTL, statistics, plan replayer, statement summary.
- Block on signals (
pkg/util/signal/) for graceful shutdown.
The standby flow (pkg/standby/) is also entered from here if --standby-cli is set: a TiDB process that warms up but does not accept client traffic until promoted.
Configuration
- TOML file path:
--config <path>. - An annotated example is at
pkg/config/config.toml.example. - Parsing and defaults:
pkg/config/config.go. - Per-config sections:
[security],[performance],[opentracing],[tikv-client],[log],[status],[experimental],[isolation-read],[instance], … - Many runtime knobs are exposed only as system variables (see
pkg/sessionctx/variable/sysvar.go).
System variables are documented at https://docs.pingcap.com/tidb/stable/system-variables; the in-repo source of truth is sysvar.go.
Network surface
| Port (default) | Purpose |
|---|---|
4000 |
MySQL protocol (configurable via --port) |
10080 |
HTTP status/admin (configurable via --status) |
Status endpoints are documented in docs/tidb_http_api.md. The HTTP server also exposes Prometheus metrics at /metrics.
Architecture context
TiDB-server is stateless: it stores no SQL data locally. Its persistence is the TiKV cluster (for user data, system tables, and DDL state) plus PD (for TSO, region routing, and cluster metadata). See Architecture for the cluster picture.
Subsystems used
In rough call order:
- Server / MySQL protocol
- Session
- Parser
- Planner
- Executor
- Expression
- DDL
- Infoschema and meta
- Statistics
- Domain
- Storage client
Operational notes
- Graceful shutdown:
tidb-servertraps SIGTERM and finishes in-flight transactions before exiting. Tests for this are intests/graceshutdown/. - GlobalKill:
KILL TIDB <conn>works across the cluster (the connection ID encodes the instance). End-to-end tests are intests/globalkilltest/. - Standby: the
pkg/standby/subsystem implements warm-standby semantics; coordinated through PD. - Cluster integrations:
tests/clusterintegrationtest/exercises multi-instance behaviour.
For deployment guidance (TiUP, Kubernetes, monitoring) see the official docs at https://docs.pingcap.com/tidb/stable.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.