准備環境
准備一台部署主機,確保其軟件滿足需求:
- 推薦安裝 CentOS 7.3 及以上版本
- Linux 操作系統開放外網訪問,用於下載 TiDB 及相關軟件安裝包
最小規模的 TiDB 集群拓撲:
注意:
下表中拓撲實例的 IP 為示例 IP。在實際部署時,請替換為實際的 IP。
實例 | 個數 | IP | 配置 |
---|---|---|---|
TiKV | 3 | 10.0.1.1 10.0.1.1 10.0.1.1 |
避免端口和目錄沖突 |
TiDB | 1 | 10.0.1.1 | 默認端口 全局目錄配置 |
PD | 1 | 10.0.1.1 | 默認端口 全局目錄配置 |
TiFlash | 1 | 10.0.1.1 | 默認端口 全局目錄配置 |
Monitor | 1 | 10.0.1.1 | 默認端口 全局目錄配置 |
部署主機軟件和環境要求:
- 部署需要使用部署主機的 root 用戶及密碼
- 部署主機關閉防火牆或者開放 TiDB 集群的節點間所需端口
- 目前 TiUP 支持在 x86_64(AMD64 和 ARM)架構上部署 TiDB 集群
- 在 AMD64 架構下,建議使用 CentOS 7.3 及以上版本 Linux 操作系統
- 在 ARM 架構下,建議使用 CentOS 7.6 1810 版本 Linux 操作系統
實施部署
注意:
你可以使用 Linux 系統的任一普通用戶或 root 用戶登錄主機,以下步驟以 root 用戶為例。
-
下載並安裝 TiUP:
curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
-
安裝 TiUP 的 cluster 組件:
tiup cluster
-
如果機器已經安裝 TiUP cluster,需要更新軟件版本:
tiup update --self && tiup update cluster
-
由於模擬多機部署,需要通過
root
用戶調大 sshd 服務的連接數限制:-
修改
/etc/ssh/sshd_config
將MaxSessions
調至 20。 -
重啟 sshd 服務:
service sshd restart
-
-
創建並啟動集群
按下面的配置模板,編輯配置文件,命名為
topo.yaml
,其中:user: "tidb"
:表示通過tidb
系統用戶(部署會自動創建)來做集群的內部管理,默認使用 22 端口通過 ssh 登錄目標機器replication.enable-placement-rules
:設置這個 PD 參數來確保 TiFlash 正常運行host
:設置為本部署主機的 IP
配置模板如下:
# # Global variables are applied to all deployments and used as the default value of # # the deployments if a specific deployment value is missing. global: user: "tidb" ssh_port: 22 deploy_dir: "/tidb-deploy" data_dir: "/tidb-data" # # Monitored variables are applied to all the machines. monitored: node_exporter_port: 9100 blackbox_exporter_port: 9115 server_configs: tidb: log.slow-threshold: 300 tikv: readpool.storage.use-unified-pool: false readpool.coprocessor.use-unified-pool: true pd: replication.enable-placement-rules: true replication.location-labels: ["host"] tiflash: logger.level: "info" pd_servers: - host: 10.0.1.1 tidb_servers: - host: 10.0.1.1 tikv_servers: - host: 10.0.1.1 port: 20160 status_port: 20180 config: server.labels: { host: "logic-host-1" } - host: 10.0.1.1 port: 20161 status_port: 20181 config: server.labels: { host: "logic-host-2" } - host: 10.0.1.1 port: 20162 status_port: 20182 config: server.labels: { host: "logic-host-3" } tiflash_servers: - host: 10.0.1.1 monitoring_servers: - host: 10.0.1.1 grafana_servers: - host: 10.0.1.1
-
執行集群部署命令:
tiup cluster deploy <cluster-name> <tidb-version> ./topo.yaml --user root -p
- 參數
<cluster-name>
表示設置集群名稱 - 參數
<tidb-version>
表示設置集群版本,可以通過tiup list tidb
命令來查看當前支持部署的 TiDB 版本
按照引導,輸入”y”及 root 密碼,來完成部署:
Do you want to continue? [y/N]: y Input SSH password:
- 參數
-
啟動集群:
tiup cluster start <cluster-name>
-
訪問集群:
-
安裝 MySQL 客戶端。如果已安裝 MySQL 客戶端則可跳過這一步驟。
yum -y install mysql
-
訪問 TiDB 數據庫,密碼為空:
mysql -h 10.0.1.1 -P 4000 -u root
-
訪問 TiDB 的 Grafana 監控:
通過 http://{grafana-ip}:3000 訪問集群 Grafana 監控頁面,默認用戶名和密碼均為 admin。
-
訪問 TiDB 的 Dashboard:
通過 http://{pd-ip}:2379/dashboard 訪問集群 TiDB Dashboard 監控頁面,默認用戶名為 root,密碼為空。
-
執行以下命令確認當前已經部署的集群列表:
tiup cluster list
-
執行以下命令查看集群的拓撲結構和狀態:
tiup cluster display <cluster-name>
升級:
1.查看所有版本 tiup list tidb
2.tiup cluster upgrade tidb_andyxi v5.2.1創建、授權和刪除用戶
使用
CREATE USER
語句創建一個用戶tiuser
,密碼為123456
:CREATE USER 'tiuser'@'localhost' IDENTIFIED BY '123456';
授權用戶
tiuser
可檢索數據庫samp_db
內的表:GRANT SELECT ON samp_db.* TO 'tiuser'@'localhost';
查詢用戶
tiuser
的權限:SHOW GRANTS for tiuser@localhost;
刪除用戶
tiuser
:DROP USER 'tiuser'@'localhost';
root@db 11:50: [test]SELECT CONCAT(table_schema,'.',table_name) AS 'Table Name', table_rows AS 'Number of Rows', CONCAT(ROUND(data_length/(1024*1024*1024),4),'G') AS 'Data Size', CONCAT(ROUND(index_length/(1024*1024*1024),4),'G') AS 'Index Size', CONCAT(ROUND((data_length+index_length)/(1024*1024*1024),4),'G') AS'Total'FROM information_schema.TABLES WHERE table_schema LIKE 'test';
+---------------+----------------+-----------+------------+---------+
| Table Name | Number of Rows | Data Size | Index Size | Total |
+---------------+----------------+-----------+------------+---------+
| test.nation | 25 | 0.0000G | 0.0000G | 0.0000G |
| test.region | 5 | 0.0000G | 0.0000G | 0.0000G |
| test.part | 200000 | 0.0245G | 0.0000G | 0.0245G |
| test.supplier | 10000 | 0.0014G | 0.0000G | 0.0014G |
| test.partsupp | 800000 | 0.1166G | 0.0119G | 0.1285G |
| test.customer | 150000 | 0.0242G | 0.0000G | 0.0242G |
| test.orders | 80896 | 0.0088G | 0.0000G | 0.0088G |
| test.lineitem | 324608 | 0.0404G | 0.0048G | 0.0453G |
+---------------+----------------+-----------+------------+---------+
-