logo

What is TiDB?

Sources
Answer

TiDB is an open-source, distributed SQL database that supports Hybrid Transactional and Analytical Processing (HTAP) workloads1. It is designed to provide a scalable, highly available, and MySQL-compatible database solution for large-scale data management. Here's a comprehensive overview of TiDB:

Key Features:

  1. Horizontal Scalability: TiDB's architecture separates computing from storage, allowing you to scale out or scale in the computing or storage capacity independently as needed2.

  2. High Availability: TiDB ensures financial-grade high availability through its multi-replica architecture and Multi-Raft protocol. Data is stored in multiple replicas, and a transaction is only committed when the majority of replicas have successfully written the data2.

  3. MySQL Compatibility: TiDB is compatible with the MySQL 5.7 protocol, common features, and syntax. This allows for easy migration of existing MySQL applications to TiDB with minimal code changes3.

  4. HTAP Capabilities: TiDB supports both Online Transactional Processing (OLTP) and Online Analytical Processing (OLAP) workloads. It achieves this through its two storage engines: TiKV (row-based) for transactional processing and TiFlash (columnar) for analytical processing12.

  5. Strong Consistency: TiDB supports ACID transactions, making it suitable for scenarios requiring strong consistency, such as financial applications3.

  6. Cloud-Native Design: TiDB is built for cloud environments, offering flexible scalability, reliability, and security on various cloud platforms2.

Architecture:

TiDB's architecture consists of several key components4:

  1. TiDB Server: This is the SQL layer that handles query parsing, optimization, and execution.

  2. TiKV: A distributed key-value storage engine that stores the actual data.

  3. Placement Driver (PD): The cluster manager that handles metadata management, timestamp allocation, and data placement decisions.

  4. TiFlash: A columnar storage engine that accelerates analytical queries.

  5. TiSpark: A connector that allows Apache Spark to access data stored in TiDB.

  6. TiDB Binlog: A tool for capturing and replicating data changes in TiDB.

  7. TiDB Lightning: A high-performance data import tool.

How TiDB Achieves High Availability and Scalability:

  1. Multi-Replica Architecture: Data in TiDB is automatically replicated across multiple nodes. This ensures that if one node fails, the data remains accessible from other replicas2.

  2. Multi-Raft Protocol: TiDB uses the Raft consensus algorithm to maintain consistency across replicas. This allows for automatic failover when a minority of replicas fail, ensuring continuous operation23.

  3. Separation of Computing and Storage: By separating the SQL processing layer (TiDB) from the storage layer (TiKV), TiDB can scale these components independently. This allows for flexible resource allocation based on workload demands2.

  4. Automatic Sharding: TiKV automatically shards data into smaller chunks (called Regions) and distributes them across the cluster. This enables TiDB to handle large datasets by spreading the load across multiple nodes4.

  5. Load Balancing: The Placement Driver continuously monitors the cluster and automatically balances data and workload across nodes to ensure optimal performance4.

Example SQL:

To demonstrate TiDB's MySQL compatibility and some of its features, here's a simple example:

-- Create a table
CREATE TABLE users (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    age INT,
    created_at TIMESTAMP
);

-- Insert some data
INSERT INTO users (id, name, age, created_at) VALUES 
(1, 'Alice', 30, NOW()),
(2, 'Bob', 25, NOW()),
(3, 'Charlie', 35, NOW());

-- Perform a simple query
SELECT name, age FROM users WHERE age > 25;

-- Demonstrate TiDB's ability to handle analytical queries
SELECT AVG(age) as average_age FROM users;

This example showcases TiDB's SQL compatibility and its ability to handle both transactional (INSERT) and analytical (AVG) queries. In a real-world scenario, TiDB's distributed nature would allow these operations to scale across multiple nodes seamlessly.

Footnotes

  1. TiDB Introduction | PingCAP Docs 2

  2. TiDB Key Features | PingCAP Docs 2 3 4 5 6 7

  3. TiDB Architecture | PingCAP Docs 2 3

  4. TiDB Architecture Components | PingCAP Docs 2 3

tidb有没有过多久就断开连接的设置

Sources
Answer

是的,TiDB 确实有自动断开空闲连接的设置。这个设置叫做 wait_timeout。以下是关于这个设置的详细信息:

  1. 在 TiDB 5.4 版本之前,TiDB 默认不会主动关闭客户端连接(除非报错)1

  2. 从 TiDB 5.4 版本开始,TiDB 默认会在连接空闲 28800 秒(即 8 小时)后自动关闭客户端连接1

  3. 你可以通过调整 wait_timeout 变量来控制这个超时设置。这个变量与 MySQL 兼容,用于控制非交互式连接的空闲超时时间2

  4. 对于交互式连接,还有一个类似的设置叫做 interactive_timeout2

  5. 如果你的应用程序使用连接池,你可能需要配置连接池来定期检查连接是否可用,或者定期发送测试查询来保持连接活跃1

需要注意的是,除了 TiDB 自身的设置外,如果你的 TiDB 集群前面有负载均衡器或代理(如 LVS 或 HAProxy),这些中间件也可能会在特定的空闲时间后主动清理连接1。因此,在配置连接超时时,还需要考虑整个网络架构。

如果你经常遇到连接被意外关闭的问题,可以考虑以下解决方案:

  1. 增加 wait_timeout 的值。
  2. 在应用程序中启用 TCP keep-alive 选项。
  3. 配置连接池以定期检查连接或发送保活查询。

Example SQL:

-- 查看当前的 wait_timeout 设置
SHOW VARIABLES LIKE 'wait_timeout';

-- 修改 wait_timeout 设置(单位为秒)
SET GLOBAL wait_timeout = 3600;  -- 设置为1小时

-- 查看当前的 interactive_timeout 设置
SHOW VARIABLES LIKE 'interactive_timeout';

-- 修改 interactive_timeout 设置(单位为秒)
SET GLOBAL interactive_timeout = 3600;  -- 设置为1小时

请注意,修改这些全局变量可能需要重新启动 TiDB 服务器或重新建立连接才能生效。同时,设置过长的超时时间可能会导致无用连接占用资源,设置过短则可能导致正常查询被中断,因此需要根据实际情况进行权衡。

Footnotes

  1. Connection pool | PingCAP Docs 2 3 4

  2. JDBC query timeout | PingCAP Docs 2

cluster-2:~ # kubectl logs -f -n namespace basic-pd-0 Server: 10.96.0.10 Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local Name: basic-pd-0.basic-pd-peer.namespace.svc Address 1: 100.66.32.25 basic-pd-0.basic-pd-peer.namespace.svc.cluster.local nslookup domain basic-pd-0.basic-pd-peer.namespace.svc.svc success starting pd-server … /pd-server --data-dir=/var/lib/pd --name=basic-pd-0 --peer-urls=http://0.0.0.0:2380 --advertise-peer-urls=http://basic-pd-0.basic-pd-peer.namespace.svc:2380 --client-urls=http://0.0.0.0:2379 --advertise-client-urls=http://basic-pd-0.basic-pd-peer.namespace.svc:2379 --config=/etc/pd/pd.toml --join=http://basic-pd-2.basic-pd-peer.namespace.svc:2380,http://basic-pd-1.basic-pd-peer.namespace.svc:2380,http://basic-pd-0.basic-pd-peer.namespace.svc:2380/ [2024/09/03 10:14:42.281 +00:00] [INFO] [util.go:43] [“Welcome to Placement Driver (PD)”] [2024/09/03 10:14:42.281 +00:00] [INFO] [util.go:44] [PD] [release-version=v6.5.8] [2024/09/03 10:14:42.281 +00:00] [INFO] [util.go:45] [PD] [edition=Community] [2024/09/03 10:14:42.281 +00:00] [INFO] [util.go:46] [PD] [git-hash=4506d63ba4fba7123ecc8277da7ef5f635efee90] [2024/09/03 10:14:42.281 +00:00] [INFO] [util.go:47] [PD] [git-branch=heads/refs/tags/v6.5.8] [2024/09/03 10:14:42.281 +00:00] [INFO] [util.go:48] [PD] [utc-build-time=“2024-01-25 10:03:20”] [2024/09/03 10:14:42.281 +00:00] [INFO] [metricutil.go:83] [“disable Prometheus push client”] [2024/09/03 10:14:42.281 +00:00] [INFO] [server.go:253] [“PD Config”] [config=“{"client-urls":"http://0.0.0.0:2379","peer-urls":"http://0.0.0.0:2380","advertise-client-urls":"http://basic-pd-0.basic-pd-peer.namespace.svc:2379","advertise-peer-urls":"http://basic-pd-0.basic-pd-peer.namespace.svc:2380","name":"basic-pd-0","data-dir":"/var/lib/pd","force-new-cluster":false,"enable-grpc-gateway":true,"initial-cluster":"basic-pd-2=http://basic-pd-2.basic-pd-peer.namespace.svc:2380,basic-pd-1=http://basic-pd-1.basic-pd-peer.namespace.svc:2380,basic-pd-0=http://basic-pd-0.basic-pd-peer.namespace.svc:2380/\“,\“initial-cluster-state\”:\“existing\”,\“initial-cluster-token\”:\“pd-cluster\”,\“join\”:\“http://basic-pd-2.basic-pd-peer.namespace.svc:2380,http://basic-pd-1.basic-pd-peer.namespace.svc:2380,http://basic-pd-0.basic-pd-peer.namespace.svc:2380/\”,\“lease\”:3,\“log\”:{\“level\”:\“info\”,\“format\”:\“text\”,\“disable-timestamp\”:false,\“file\”:{\“filename\”:\”\“,\“max-size\”:0,\“max-days\”:0,\“max-backups\”:0},\“development\”:false,\“disable-caller\”:false,\“disable-stacktrace\”:false,\“disable-error-verbose\”:true,\“sampling\”:null,\“error-output-path\”:\”\“},\“tso-save-interval\”:\“3s\”,\“tso-update-physical-interval\”:\“50ms\”,\“enable-local-tso\”:false,\“metric\”:{\“job\”:\“basic-pd-0\”,\“address\”:\”\“,\“interval\”:\“15s\”},\“schedule\”:{\“max-snapshot-count\”:64,\“max-pending-peer-count\”:64,\“max-merge-region-size\”:20,\“max-merge-region-keys\”:0,\“split-merge-interval\”:\“1h0m0s\”,\“swtich-witness-interval\”:\“1h0m0s\”,\“enable-one-way-merge\”:\“false\”,\“enable-cross-table-merge\”:\“true\”,\“patrol-region-interval\”:\“10ms\”,\“max-store-down-time\”:\“30m0s\”,\“max-store-preparing-time\”:\“48h0m0s\”,\“leader-schedule-limit\”:4,\“leader-schedule-policy\”:\“count\”,\“region-schedule-limit\”:2048,\“replica-schedule-limit\”:64,\“merge-schedule-limit\”:8,\“hot-region-schedule-limit\”:4,\“hot-region-cache-hits-threshold\”:3,\“store-limit\”:{},\“tolerant-size-ratio\”:0,\“low-space-ratio\”:0.8,\“high-space-ratio\”:0.7,\“region-score-formula-version\”:\“v2\”,\“scheduler-max-waiting-operator\”:5,\“enable-remove-down-replica\”:\“true\”,\“enable-replace-offline-replica\”:\“true\”,\“enable-make-up-replica\”:\“true\”,\“enable-remove-extra-replica\”:\“true\”,\“enable-location-replacement\”:\“true\”,\“enable-debug-metrics\”:\“false\”,\“enable-joint-consensus\”:\“true\”,\“enable-tikv-split-region\”:\“true\”,\“schedulers-v2\”:[{\“type\”:\“balce-region\”,\“args\”:null,\“disable\”:false,\“args-payload\”:\”\“},{\“type\”:\“balce-leader\”,\“args\”:null,\“disable\”:false,\“args-payload\”:\”\“},{\“type\”:\“hot-region\”,\“args\”:null,\“disable\”:false,\“args-payload\”:\”\“},{\“type\”:\“split-bucket\”,\“args\”:null,\“disable\”:false,\“args-payload\”:\”\“}],\“schedulers-payload\”:null,\“store-limit-mode\”:\“manual\”,\“hot-regions-write-interval\”:\“10m0s\”,\“hot-regions-reserved-days\”:7,\“enable-diagnostic\”:\“false\”,\“enable-witness\”:\“false\”},\“replication\”:{\“max-replicas\”:3,\“location-labels\”:\”\“,\“strictly-match-label\”:\“false\”,\“enable-placement-rules\”:\“true\”,\“enable-placement-rules-cache\”:\“false\”,\“isolation-level\”:\”\“},\“pd-server\”:{\“use-region-storage\”:\“true\”,\“max-gap-reset-ts\”:\“24h0m0s\”,\“key-type\”:\“table\”,\“runtime-services\”:\”\“,\“metric-storage\”:\”\“,\“dashboard-address\”:\“auto\”,\“trace-region-flow\”:\“true\”,\“flow-round-by-digit\”:3,\“min-resolved-ts-persistence-interval\”:\“1s\”},\“cluster-version\”:\“0.0.0\”,\“labels\”:{},\“quota-backend-bytes\”:\“8GiB\”,\“auto-compaction-mode\”:\“periodic\”,\“auto-compaction-retention-v2\”:\“1h\”,\“TickInterval\”:\“500ms\”,\“ElectionInterval\”:\“3s\”,\“PreVote\”:true,\“max-request-bytes\”:157286400,\“security\”:{\“cacert-path\”:\”\“,\“cert-path\”:\”\“,\“key-path\”:\”\“,\“cert-allowed-cn\”:null,\“SSLCABytes\”:null,\“SSLCertBytes\”:null,\“SSLKEYBytes\”:null,\“redact-info-log\”:false,\“encryption\”:{\“data-encryption-method\”:\“plaintext\”,\“data-key-rotation-period\”:\“168h0m0s\”,\“master-key\”:{\“type\”:\“plaintext\”,\“key-id\”:\”\“,\“region\”:\”\“,\“endpoint\”:\”\“,\“path\”:\”\“}}},\“label-property\”:null,\“WarningMsgs\”:null,\“DisableStrictReconfigCheck\”:false,\“HeartbeatStreamBindInterval\”:\“1m0s\”,\“LeaderPriorityCheckInterval\”:\“1m0s\”,\“dashboard\”:{\“tidb-cacert-path\”:\”\“,\“tidb-cert-path\”:\”\“,\“tidb-key-path\”:\”\“,\“public-path-prefix\”:\”\“,\“internal-proxy\”:false,\“enable-telemetry\”:false,\“enable-experimental\”:false},\“replication-mode\”:{\“replication-mode\”:\“majority\”,\“dr-auto-sync\”:{\“label-key\”:\”\“,\“primary\”:\”\“,\“dr\”:\”\“,\“primary-replicas\”:0,\“dr-replicas\”:0,\“wait-store-timeout\”:\“1m0s\”,\“pause-region-split\”:\“false\”}}}”] [2024/09/03 10:14:42.289 +00:00] [INFO] [server.go:228] [“register REST path”] [path=/pd/api/v1] [2024/09/03 10:14:42.289 +00:00] [INFO] [server.go:228] [“register REST path”] [path=/pd/api/v2/] [2024/09/03 10:14:42.289 +00:00] [INFO] [server.go:228] [“register REST path”] [path=/autoscaling] [2024/09/03 10:14:42.289 +00:00] [INFO] [distro.go:51] [“Using distribution strings”] [strings={}] [2024/09/03 10:14:42.292 +00:00] [INFO] [server.go:228] [“register REST path”] [path=/dashboard/api/] [2024/09/03 10:14:42.292 +00:00] [INFO] [server.go:228] [“register REST path”] [path=/dashboard/] [2024/09/03 10:14:42.293 +00:00] [INFO] [etcd.go:117] [“configuring peer listeners”] [listen-peer-urls=”[http://0.0.0.0:2380]“] [2024/09/03 10:14:42.293 +00:00] [INFO] [systimemon.go:30] [“start system time monitor”] [2024/09/03 10:14:42.293 +00:00] [INFO] [etcd.go:127] [“configuring client listeners”] [listen-client-urls=”[http://0.0.0.0:2379]“] [2024/09/03 10:14:42.293 +00:00] [INFO] [etcd.go:611] [“pprof is enabled”] [path=/debug/pprof] [2024/09/03 10:14:42.294 +00:00] [INFO] [etcd.go:369] [“closing etcd server”] [name=basic-pd-0] [data-dir=/var/lib/pd] [advertise-peer-urls=”[http://basic-pd-0.basic-pd-peer.namespace.svc:2380]“] [advertise-client-urls=”[http://basic-pd-0.basic-pd-peer.namespace.svc:2379]“] [2024/09/03 10:14:42.294 +00:00] [INFO] [etcd.go:373] [“closed etcd server”] [name=basic-pd-0] [data-dir=/var/lib/pd] [advertise-peer-urls=”[http://basic-pd-0.basic-pd-peer.namespace.svc:2380]“] [advertise-client-urls=”[http://basic-pd-0.basic-pd-peer.namespace.svc:2379]“] [2024/09/03 10:14:42.294 +00:00] [FATAL] [main.go:120] [“run server failed”] [error=”[PD:etcd:ErrStartEtcd]error setting up initial cluster: URL must not contain a path: http://basic-pd-0.basic-pd-peer.namespace.svc:2380/: error setting up initial cluster: URL must not contain a path: http://basic-pd-0.basic-pd-peer.namespace.svc:2380/"] [stack=“main.main\n\t/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/pd/cmd/pd-server/main.go:120\nruntime.main\n\t/usr/local/go/src/runtime/proc.go:250”] 有没有大神回答下,后面这个报错的原因,我看了正常启动的时候,join的成员是3个啊。为啥说不能包含自己。

Sources
Answer

这个错误的原因是 PD 服务器的 join 配置中包含了一个无效的 URL。具体来说,问题出在以下配置行:

--join=http://basic-pd-2.basic-pd-peer.namespace.svc:2380,http://basic-pd-1.basic-pd-peer.namespace.svc:2380,http://basic-pd-0.basic-pd-peer.namespace.svc:2380/

错误信息指出 URL 不能包含路径,但最后一个 URL http://basic-pd-0.basic-pd-peer.namespace.svc:2380/ 末尾多了一个斜杠 /。这个多余的斜杠被解释为路径,导致了错误。

要解决这个问题,你需要移除最后一个 URL 末尾的斜杠。正确的配置应该是:

--join=http://basic-pd-2.basic-pd-peer.namespace.svc:2380,http://basic-pd-1.basic-pd-peer.namespace.svc:2380,http://basic-pd-0.basic-pd-peer.namespace.svc:2380

这个错误与 join 配置包含自身无关。PD 服务器在启动时可以包含自己的 URL 在 join 列表中,这是允许的。问题纯粹是由于 URL 格式不正确造成的。

要修复这个问题:

  1. 检查你的 PD 配置文件(通常是 pd.toml)。
  2. 找到 join 配置项。
  3. 确保所有的 URL 都不包含末尾的斜杠。
  4. 保存更改并重启 PD 服务器。

如果你使用的是 Kubernetes 部署,你可能需要更新你的 Kubernetes 配置文件(如 ConfigMap 或 StatefulSet 定义)来修复这个问题。

这个错误强调了在配置分布式系统时需要格外注意细节,即使是一个小小的斜杠也可能导致启动失败12

Footnotes

  1. PD Configuration File | PingCAP Docs

  2. TiDB Cluster Troubleshooting | PingCAP Docs


Further questions