Open-Source Wikis

/

TiDB

/

Applications

/

tidb-server

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) or make 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:

  1. 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, …).
  2. Load config from the TOML file (pkg/config/config.go).
  3. Set GOMAXPROCS via automaxprocs and apply OS-level adjustments (pkg/util/sys/linux/, pkg/util/sys/storage/).
  4. Initialise logging (pkg/util/logutil/), pyroscope, and OpenTracing.
  5. Open the storage (kvstore.New) — selects the production driver (pkg/store/driver/tikv_driver.go) for --store=tikv or the in-process unistore for --store=unistore.
  6. Bootstrap session and Domain: session.BootstrapSession runs cluster-bootstrap migrations on first start; domain.NewDomain brings up the per-instance services (see Domain).
  7. Start the SQL server (pkg/server/): listeners, status HTTP, gRPC, metrics, etc.
  8. Wire DDL, plugins, telemetry, runaway, TTL, statistics, plan replayer, statement summary.
  9. 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:

Operational notes

  • Graceful shutdown: tidb-server traps SIGTERM and finishes in-flight transactions before exiting. Tests for this are in tests/graceshutdown/.
  • GlobalKill: KILL TIDB <conn> works across the cluster (the connection ID encodes the instance). End-to-end tests are in tests/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.

tidb-server – TiDB wiki | Factory