# 使用 TiUP 部署 TiDB 集群

## 概述

在本实验中，你将安装 TiUP 工具，使用 TiUP 部署一个 TiDB 集群，检查 TiDB 集群的运行状态。连接到 TiDB server，启动 / 停止 TiDB 集群，然后查看配置文件。下图展示了 TiDB 集群的部署拓扑。

   ![60%](https://lab-static.pingcap.com/quick-demo/demo_010_en.png)

### 实验时长

+ 完成此练习大约需要 90 分钟。

> [!TIP]
> 点击 **开始** 启动实验环境。
> 环境准备就绪后，点击底部工具栏中的 **前进** 进入 **模块 1**。
> 然后，按照实验指南的步骤连接到实践环境并开始你的任务。

## 模块 1：安装 TiUP 并熟悉实验环境

### 步骤 1. 连接到远程 Linux 机器

+ 点击这里 - [终端 1](ssh://monitor1-ssh#1) 打开一个新的终端，连接到为你的实践实验分配的虚拟机。

+ 登录后显示的横幅：

    ```output
     ,     #_
    ~\_  ####_        Amazon Linux 2
    ~~  \_#####\
    ~~     \###|      
    ~~       \#/ ___
    ~~       V~' '->
    ~~~         /    A newer version of Amazon Linux is available!
     ~~._.   _/
        _/ _/       Amazon Linux 2023, GA and supported until 2028-03-15.
      _/m/'           https://aws.amazon.com/linux/amazon-linux-2023/
    ```

### 步骤 2. 获取实验资源信息

+ 保持在 `ec2-user` 主目录下，查看 `hosts-env.sh` 文件的内容以获取 IP 地址信息。你的输出可能与 `sample_output` 不同：

   ```bash
   cat hosts-env.sh
   ```
   ```output
   export HOST_MONITOR1_PRIVATE_IP=10.90.4.19
   export HOST_MONITOR1_PUBLIC_IP=44.234.33.106
   export HOST_CM_PRIVATE_IP=10.90.4.19
   export HOST_CM_PUBLIC_IP=44.234.33.106
   export HOST_PD1_PRIVATE_IP=10.90.1.197
   export HOST_PD1_PUBLIC_IP=18.236.76.37
   export HOST_PD2_PRIVATE_IP=10.90.3.218
   export HOST_PD2_PUBLIC_IP=18.237.226.109
   export HOST_PD3_PRIVATE_IP=10.90.2.84
   export HOST_PD3_PUBLIC_IP=18.237.105.199
   export HOST_DB1_PRIVATE_IP=10.90.3.216
   export HOST_DB1_PUBLIC_IP=34.222.212.46
   export HOST_DB2_PRIVATE_IP=10.90.2.245
   export HOST_DB2_PUBLIC_IP=54.188.31.139
   export HOST_KV1_PRIVATE_IP=10.90.1.94
   export HOST_KV1_PUBLIC_IP=35.87.137.198
   export HOST_KV2_PRIVATE_IP=10.90.2.38
   export HOST_KV2_PUBLIC_IP=35.91.214.228
   export HOST_KV3_PRIVATE_IP=10.90.3.112
   export HOST_KV3_PUBLIC_IP=54.184.160.176
   export HOST_TIFLASH1_PRIVATE_IP=10.90.4.28
   export HOST_TIFLASH1_PUBLIC_IP=34.223.94.92
   ```

+ 在你连接的 Linux 上设置节点 IP 地址变量。
   ```bash
   source ./hosts-env.sh
   ```

+ 你也可以随时点击本实验指南顶部的 <kbd>资源</kbd> 选项卡，以访问所有实验资源信息。

### 步骤 3. 下载并安装 TiUP - TiDB 集群管理工具

+ 在终端提示符下输入以下命令来安装 `tiup` 工具。请注意，`tiup` 在你的环境中可能已预装，也可能未预装。
   
   ```bash
   curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
   ```

   ```output
   curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
     % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                    Dload  Upload   Total   Spent    Left  Speed
   100 5152k  100 5152k    0     0  14.8M      0 --:--:-- --:--:-- --:--:-- 14.8M
   Successfully set mirror to https://tiup-mirrors.pingcap.com
   Detected shell: bash
   Shell profile:  /home/ec2-user/.bash_profile
   Installed path: /home/ec2-user/.tiup/bin/tiup
   ===============================================
   Have a try:     tiup playground
   ===============================================
   ```

### 步骤 4. 设置 TiUP 环境变量

+ 在终端提示符下输入以下命令。
   
   ```bash
   source .bash_profile
   ```

### 步骤 5. 将 TiUP 镜像更改为本地文件夹

+ 确保将本地 TiUP 镜像设置为已连接 Linux 上的 `tidb-community-server-tidb_version-linux-amd64` 文件夹。它已为本次实践实验 [cloned](https://docs.pingcap.com/tidb/stable/production-deployment-using-tiup) 完成。

   ```bash
   tiup mirror set tidb-community-server-tidb_version-linux-amd64
   tiup mirror show
   ```
   ```output
   Successfully set mirror to /home/ec2-user/tidb-community-server-tidb_version-linux-amd64
   /home/ec2-user/tidb-community-server-tidb_version-linux-amd64
   ```

+ 有关设置本地 TiUP 镜像的更多信息，请参考 [TiUP 文档](https://docs.pingcap.com/zh/tidb/stable/production-deployment-using-tiup)。现在，部署 TiDB 集群的环境已准备就绪，请继续下一步实践。


### 步骤 6. 通过执行 `tiup cluster` 来验证 TiUP 集群组件已安装

+ 在终端提示符下输入以下命令并查看显示的结果。请注意，如果尚未安装， `tiup cluster` 命令可以从配置的 TiUP 镜像中安装 `cluster` 组件。
   
   ```bash
   tiup cluster
   ```
   ```output
   Checking updates for component cluster... Deploy a TiDB cluster for production

   Usage:
   tiup cluster [command]

   Available Commands:
   check       Perform preflight checks for the cluster.
   deploy      Deploy a cluster for production
   start       Start a TiDB cluster
   stop        Stop a TiDB cluster
   restart     Restart a TiDB cluster
   scale-in    Scale in a TiDB cluster
   scale-out   Scale out a TiDB cluster
   destroy     Destroy a specified cluster
   clean       (EXPERIMENTAL) Cleanup a specified cluster
   upgrade     Upgrade a specified TiDB cluster
   display     Display information of a TiDB cluster
   prune       Destroy and remove instances that is in tombstone state
   list        List all clusters
   audit       Show audit log of cluster operation
   import      Import an exist TiDB cluster from TiDB-Ansible
   edit-config Edit TiDB cluster config
   show-config Show TiDB cluster config
   reload      Reload a TiDB cluster's config and restart if needed
   patch       Replace the remote package with a specified package and restart the service
   rename      Rename the cluster
   enable      Enable a TiDB cluster automatically at boot
   disable     Disable automatic enabling of TiDB clusters at boot
   replay      Replay previous operation and skip successed steps
   template    Print topology template
   tls         Enable/Disable TLS between TiDB components
   meta        backup/restore meta information
   rotatessh   rotate ssh keys on all nodes
   help        Help about any command
   completion  Generate the autocompletion script for the specified shell

   Flags:
   -c, --concurrency int     max number of parallel tasks allowed (default 5)
      --format string       (EXPERIMENTAL) The format of output, available values are [default, json] (default "default")
   -h, --help                help for tiup
      --ssh string          (EXPERIMENTAL) The executor type: 'builtin', 'system', 'none'.
      --ssh-timeout uint    Timeout in seconds to connect host via SSH, ignored for operations that don't need an SSH connection. (default 5)
   -v, --version             version for tiup
      --wait-timeout uint   Timeout in seconds to wait for an operation to complete, ignored for operations that don't fit. (default 120)
   -y, --yes                 Skip all confirmations and assumes 'yes'

   Use "tiup cluster help [command]" for more information about a command.
   ```

### 步骤 7. 查看已为你设置好公/私钥

+ 请注意，在本实验中，公/私钥对由实验环境准备。在实验结束后，你必须在自己的环境中自行使用 `ssh-keygen` 准备公/私钥对。

+ 私钥位于 `~/.ssh` 目录下，且私钥文件的权限为只读。你将使用此私钥来部署 TiDB 集群。
   
   ```bash
   ls -l /home/ec2-user/.ssh/pe-class-key-oregon.pem
   ```
   ```output
   -r-------- 1 ec2-user ec2-user 1674 May 25 04:09 /home/ec2-user/.ssh/pe-class-key-oregon.pem
   ```

+ 在本实验的每个节点上，对等公钥位于 `~/.ssh/authorized_keys` 文件中，因此你可以无密码通过 SSH 连接到任何节点。

   ```bash
   cat ~/.ssh/authorized_keys | grep oregon
   ```
   ```output
   ssh-rsa AAAAB3Nza..........FvuLk3f5/Wsdx7jdNrP pe-class-key-oregon
   ```

## 模块 2：部署 TiDB 集群

### 步骤 1. 查看拓扑文件

+ 查看提供的拓扑文件 `solution-topology-ten-nodes.yaml`，它用于部署 TiDB 集群。请注意，`sample_output`中显示的 IP 地址会与你的不同。

   ```bash
   cat solution-topology-ten-nodes.yaml
   ```
   ```output
   global:
     user: "ec2-user"
     ssh_port: 22
     deploy_dir: "/tidb-deploy"
     data_dir: "/tidb-data"
     arch: "amd64"
   server_configs:
     pd:
       replication.location-labels: ["region","zone"]
       replication.max-replicas: 3
   pd_servers:
     - host: 10.90.3.184
       client_port: 2379
       peer_port: 2380
       advertise_client_addr: "http://35.88.150.142:2379"
       config:
         server.labels:
           region: oregon
           zone: az1
     - host: 10.90.3.28
       client_port: 2379
       peer_port: 2380
       advertise_client_addr: "http://18.236.135.17:2379"
       config:
         server.labels:
           region: oregon
           zone: az1
     - host: 10.90.3.247
       client_port: 2379
       peer_port: 2380
       advertise_client_addr: "http://34.219.57.252:2379"
       config:
         server.labels:
           region: oregon
           zone: az1
   tidb_servers:
     - host: 10.90.1.164
       port: 4000
       status_port: 10080
       deploy_dir: /tidb-deploy/tidb-4000
       log_dir: /tidb-deploy/tidb-4000/log
     - host: 10.90.1.14
       port: 4000
       status_port: 10080
       deploy_dir: /tidb-deploy/tidb-4000
       log_dir: /tidb-deploy/tidb-4000/log
   tikv_servers:
     - host: 10.90.3.197
       port: 20160
       status_port: 20180
       config:
         server.labels:
           region: oregon
           zone: az1
     - host: 10.90.3.39
       port: 20160
       status_port: 20180
       config:
         server.labels:
           region: oregon
           zone: az1
     - host: 10.90.3.82
       port: 20160
       status_port: 20180
       config:
         server.labels:
           region: oregon
           zone: az1
   tiflash_servers:
     - host: 10.90.4.194
       tcp_port: 9000
       flash_service_port: 3930
       deploy_dir: /tidb-deploy/tiflash-9000
       data_dir: /tidb-data/tiflash-9000
       log_dir: /tidb-deploy/tiflash-9000/log
   monitoring_servers:
     - host: 10.90.4.156
   grafana_servers:
     - host: 10.90.4.156
   alertmanager_servers:
     - host: 10.90.4.156
   ```

### 步骤 2. 安装所需软件包

+ 确保在所有集群节点上安装 `numactl`在课堂环境中该任务应已完成，所以无需在所有节点上实际执行安装）。例如，在终端提示符下输入以下命令在 `TiKV server 1` 上安装 `numactl` （如果提示进行 SSH 连接，输入 `yes`）：
   
   ```bash
   ssh -i ~/.ssh/pe-class-key-oregon.pem ec2-user@${HOST_KV1_PRIVATE_IP}
   ```
   ```output
      ,     #_
      ~\_  ####_        Amazon Linux 2
     ~~  \_#####\
     ~~     \###|       
     ~~       \#/ ___
      ~~       V~' '->
       ~~~         /    A newer version of Amazon Linux is available!
         ~~._.   _/
            _/ _/       Amazon Linux 2023, GA and supported until 2028-03-15.
          _/m/'           https://aws.amazon.com/linux/amazon-linux-2023/
   ```
   ```bash
   sudo yum -y install numactl
   ```
   ```output
   Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
   amzn2-core                                                                                                                                 | 3.6 kB  00:00:00     
   60 packages excluded due to repository priority protections
   Package numactl-2.0.9-7.amzn2.x86_64 already installed and latest version
   Nothing to do
   ```

+ 从 `TiKV server 1` 注销并返回 TiUP 控制机的提示符：
   
   ```bash
   exit
   ```

### 步骤 3. 使用 `tiup cluster check` 检查系统要求

+ 检查系统设置以满足集群部署前提条件。在终端提示符下输入以下命令并查看显示的结果。你会发现有些项目未通过系统检查。我们将在下一步解决此问题。
   
   ```bash
   tiup cluster check ./solution-topology-ten-nodes.yaml \
    --user ec2-user \
    -i /home/ec2-user/.ssh/pe-class-key-oregon.pem
   ```
   ```output
   + Detect CPU Arch Name
     - Detecting node 10.90.2.90 Arch info ... ⠙ Shell: host=10.90.2.90, sudo=false, command=`uname -m`
     - Detecting node 10.90.2.231 Arch info ... ⠙ Shell: host=10.90.2.231, sudo=false, command=`uname -m`
     - Detecting node 10.90.2.138 Arch info ... ⠙ Shell: host=10.90.2.138, sudo=false, command=`uname -m`
     - Detecting node 10.90.2.242 Arch info ... ⠙ Shell: host=10.90.2.242, sudo=false, command=`uname -m`
   ...
   10.90.2.97   sysctl        Fail    fs.file-max = 376482, should be greater than 1000000
   10.90.2.97   sysctl        Fail    net.core.somaxconn = 4096, should be greater than 32768
   10.90.2.97   sysctl        Fail    net.ipv4.tcp_syncookies = 1, should be 0
   10.90.2.97   sysctl        Fail    vm.swappiness = 60, should be 0
   10.90.2.97   service       Pass    service firewalld not found, ignore
   10.90.2.97   command       Pass    numactl: policy: default
   10.90.2.97   timezone      Pass    time zone is the same as the first PD machine: UTC
   10.90.2.97   os-version    Pass    OS is Amazon Linux 2 
   10.90.2.97   memory        Pass    memory size is 4096MB
   10.90.2.97   limits        Fail    soft limit of 'nofile' for user 'ec2-user' is not set or too low
   10.90.2.97   limits        Fail    hard limit of 'nofile' for user 'ec2-user' is not set or too low
   10.90.2.97   limits        Fail    soft limit of 'stack' for user 'ec2-user' is not set or too low
   10.90.2.97   selinux       Pass    SELinux is disabled
   10.90.2.97   thp           Fail    THP is enabled, please disable it for best performance
   ```

### 步骤 4. 对未通过的检查运行自动修复

+ 在终端提示符下输入以下命令并查看显示的结果。
   
   ```bash
   tiup cluster check ./solution-topology-ten-nodes.yaml \
    --user ec2-user \
    -i /home/ec2-user/.ssh/pe-class-key-oregon.pem \
    --apply
   ```
   ```output
   ...
   10.90.3.79   cpu-governor  Warn    Unable to determine current CPU frequency governor policy, auto fixing not supported
   10.90.3.79   sysctl        Fail    will try to set 'fs.file-max = 1000000'
   10.90.3.79   sysctl        Fail    will try to set 'net.core.somaxconn = 32768'
   10.90.3.79   sysctl        Fail    will try to set 'net.ipv4.tcp_syncookies = 0'
   10.90.3.79   sysctl        Fail    will try to set 'vm.swappiness = 0'
   + Try to apply changes to fix failed checks
     - Applying changes on 10.90.2.231 ... Done
     - Applying changes on 10.90.2.138 ... Done
     - Applying changes on 10.90.2.242 ... Done
     - Applying changes on 10.90.2.158 ... Done
     - Applying changes on 10.90.1.56 ... Done
     - Applying changes on 10.90.4.138 ... Done
     - Applying changes on 10.90.2.90 ... Done
     - Applying changes on 10.90.1.169 ... Done
     - Applying changes on 10.90.4.126 ... Done
     - Applying changes on 10.90.2.95 ... Done
   ```

### 步骤 5. 部署一个名为 `tidb-demo` 的 TiDB 集群

+ 在终端提示符下输入以下命令并查看显示的结果。我们在命令中包含 `--yes`。如果不使用`--yes`，在部署过程中会提示你检查集群拓扑。
   
   ```bash
   tiup cluster deploy tidb-demo tidb_version ./solution-topology-ten-nodes.yaml \
    --user ec2-user \
    -i /home/ec2-user/.ssh/pe-class-key-oregon.pem \
    --yes
   ```
   ```output
   + Detect CPU Arch Name
     - Detecting node 10.90.2.90 Arch info ... ⠙ Shell: host=10.90.2.90, sudo=false, command=`uname -m`
     - Detecting node 10.90.2.231 Arch info ... ⠙ Shell: host=10.90.2.231, sudo=false, command=`uname -m`
     - Detecting node 10.90.2.138 Arch info ... ⠙ Shell: host=10.90.2.138, sudo=false, command=`uname -m`
     - Detecting node 10.90.2.242 Arch info ... ⠙ Shell: host=10.90.2.242, sudo=false, command=`uname -m`
   ...
   Cluster `tidb-demo` deployed successfully, you can start it with command: `tiup cluster start tidb-demo --init`
   ```

## 模块 3：检查已部署的 TiDB 集群的状态

### 步骤 1. 使用 TiUP 检查 TiDB 集群状态

+ 在 TiUP 控制机上，检查由 TiUP 管理的集群。
   
   ```bash
   tiup cluster list
   ```
   ```output
   Name       User      Version  Path                                                     PrivateKey
   ----       ----      -------  ----                                                     ----------
   tidb-demo  ec2-user  vtidb_version   /home/ec2-user/.tiup/storage/cluster/clusters/tidb-demo  /home/ec2-user/.tiup/storage/cluster/clusters/tidb-demo/ssh/id_rsa
   ```

+ Check the status of the deployed TiDB cluster, all components are not running yet.
   
   ```bash
   tiup cluster display tidb-demo
   ```
   ```output
   Cluster type:       tidb
   Cluster name:       tidb-demo
   Cluster version:    vtidb_version
   Deploy user:        ec2-user
   SSH type:           builtin
   Grafana URL:        http://10.90.4.138:3000
   ID                 Role          Host         Ports                            OS/Arch       Status  Data Dir                      Deploy Dir
   --                 ----          ----         -----                            -------       ------  --------                      ----------
   10.90.4.138:9093   alertmanager  10.90.4.138  9093/9094                        linux/x86_64  Down    /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
   10.90.4.138:3000   grafana       10.90.4.138  3000                             linux/x86_64  Down    -                             /tidb-deploy/grafana-3000
   10.90.2.138:2379   pd            10.90.2.138  2379/2380                        linux/x86_64  Down    /tidb-data/pd-2379            /tidb-deploy/pd-2379
   10.90.2.231:2379   pd            10.90.2.231  2379/2380                        linux/x86_64  Down    /tidb-data/pd-2379            /tidb-deploy/pd-2379
   10.90.2.90:2379    pd            10.90.2.90   2379/2380                        linux/x86_64  Down    /tidb-data/pd-2379            /tidb-deploy/pd-2379
   10.90.4.138:9090   prometheus    10.90.4.138  9090/12020                       linux/x86_64  Down    /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
   10.90.1.169:4000   tidb          10.90.1.169  4000/10080                       linux/x86_64  Down    -                             /tidb-deploy/tidb-4000
   10.90.1.56:4000    tidb          10.90.1.56   4000/10080                       linux/x86_64  Down    -                             /tidb-deploy/tidb-4000
   10.90.4.126:9000   tiflash       10.90.4.126  9000/8123/3930/20170/20292/8234  linux/x86_64  N/A     /tidb-data/tiflash-9000       /tidb-deploy/tiflash-9000
   10.90.2.158:20160  tikv          10.90.2.158  20160/20180                      linux/x86_64  N/A     /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.2.242:20160  tikv          10.90.2.242  20160/20180                      linux/x86_64  N/A     /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.2.95:20160   tikv          10.90.2.95   20160/20180                      linux/x86_64  N/A     /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   Total nodes: 12
   ```

### 步骤 2. 启动 TiDB 集群

+ 以标准启动方式启动 TiDB 集群（数据库 root 用户密码为空）。在这一步中，我们不使用 `--init` 选项为数据库 root 用户生成随机密码。

   ```bash
   tiup cluster start tidb-demo
   ```
   ```output
   ...
   Started cluster `tidb-demo` successfully
   ```

+ 检查已启动的 TiDB 集群的状态。TiDB Dashboard 和 Grafana 的 URL 也会显示。继续下一步。

   ```bash
   tiup cluster display tidb-demo
   ```
   ```output
   Cluster type:       tidb
   Cluster name:       tidb-demo
   Cluster version:    vtidb_version
   Deploy user:        ec2-user
   SSH type:           builtin
   Dashboard URL:      http://34.222.39.121:2379/dashboard
   Grafana URL:        http://10.90.4.138:3000
   ID                 Role          Host         Ports                            OS/Arch       Status  Data Dir                      Deploy Dir
   --                 ----          ----         -----                            -------       ------  --------                      ----------
   10.90.4.138:9093   alertmanager  10.90.4.138  9093/9094                        linux/x86_64  Up      /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
   10.90.4.138:3000   grafana       10.90.4.138  3000                             linux/x86_64  Up      -                             /tidb-deploy/grafana-3000
   10.90.2.138:2379   pd            10.90.2.138  2379/2380                        linux/x86_64  Up|L    /tidb-data/pd-2379            /tidb-deploy/pd-2379
   10.90.2.231:2379   pd            10.90.2.231  2379/2380                        linux/x86_64  Up      /tidb-data/pd-2379            /tidb-deploy/pd-2379
   10.90.2.90:2379    pd            10.90.2.90   2379/2380                        linux/x86_64  Up      /tidb-data/pd-2379            /tidb-deploy/pd-2379
   10.90.4.138:9090   prometheus    10.90.4.138  9090/12020                       linux/x86_64  Up      /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
   10.90.1.169:4000   tidb          10.90.1.169  4000/10080                       linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
   10.90.1.56:4000    tidb          10.90.1.56   4000/10080                       linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
   10.90.4.126:9000   tiflash       10.90.4.126  9000/8123/3930/20170/20292/8234  linux/x86_64  Up      /tidb-data/tiflash-9000       /tidb-deploy/tiflash-9000
   10.90.2.158:20160  tikv          10.90.2.158  20160/20180                      linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.2.242:20160  tikv          10.90.2.242  20160/20180                      linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.2.95:20160   tikv          10.90.2.95   20160/20180                      linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   Total nodes: 12
   ```

### 步骤 3. 使用 TiDB Dashboard 和 Grafana Dashboard 检查集群状态

+ 使用浏览器登录 TiDB Dashboard，用户名和密码与 TiDB `root` 用户的相同。如果你按照实验指南选择标准启动，密码为空。主页显示 TiDB 集群的节点信息。

+ 你也可以使用 Grafana Dashboard 检查集群状态。步骤 2 中显示的 Grafana URL 包含 `monitor` 节点上的私有 IP 地址。将其更改为 `monitor` 节点对应的公网 IP 地址，然后将 URL 输入浏览器。你可以通过点击<kbd>资源</kbd>按钮找到 `monitor` 地址信息。Grafana 的默认用户名和密码均为 `admin`。

  ![40](https://lab-static.pingcap.com/quick-demo/Grafana_aa_001_20220615.png)

## 模块 4：连接到 TiDB 实例

### 步骤 1. 从远程 Linux 连接到 TiDB 实例

+ 从连接到远程 Linux 的 SSH 会话中，连接到 TiDB 实例并修改密码。如果 `root` 有密码，你应提供 `-p` 选项，否则可以省略 `-p` 选项。

   ```bash
   mysql -h ${HOST_DB1_PRIVATE_IP} --port 4000 -u root
   ```
   ```output
   mysql> 
   ```

### 步骤 2. 执行 SQL 查询 

+ 检查 TiDB 集群的版本。

  ```sql
  SELECT TIDB_VERSION(), VERSION(), NOW()\G
  ```
  ```output
  *************************** 1. row ***************************
  TIDB_VERSION(): Release Version: v8.1.1
  Edition: Community
  Git Commit Hash: a7df4f9845d5d6a590c5d45dad0dcc9f21aa8765
  Git Branch: HEAD
  UTC Build Time: 2024-08-22 05:50:03
  GoVersion: go1.21.13
  Race Enabled: false
  Check Table Before Drop: false
  Store: tikv
       VERSION(): 8.0.11-TiDB-v8.1.1
           NOW(): 2025-01-19 14:20:05
  1 row in set (0.00 sec)
  ```

### 步骤 3. 从 mysql 客户端退出到 Linux 提示符

   ```sql
   EXIT;
   ```
   ```output
   Bye
   ```

## 模块 5：查看 TiDB 配置文件

### 步骤 1. 查看拓扑配置 YAML

+ 在 TiUP 控制机上，查看集群拓扑和配置文件的位置。完成后，输入 `:q!` 退出到 Linux 的操作系统提示符。

   ```bash
   tiup cluster edit-config tidb-demo
   ```
   ```output
   global:
     user: ec2-user
     ssh_port: 22
     ssh_type: builtin
     deploy_dir: /tidb-deploy
     data_dir: /tidb-data
     os: linux
     arch: amd64
     systemd_mode: system
   ...
   alertmanager_servers:
   - host: 10.90.4.138
     ssh_port: 22
     web_port: 9093
     cluster_port: 9094
     deploy_dir: /tidb-deploy/alertmanager-9093
     data_dir: /tidb-data/alertmanager-9093
     log_dir: /tidb-deploy/alertmanager-9093/log
     arch: amd64
     os: linux
   ```

### 步骤 2. 检查 TiDB server 1 上的 TiDB server 配置目录

+ 连接到 TiDB server 1 并列出配置目录。如果`ssh` 提示，输入 `yes`。
   
   ```bash
   ssh -i ~/.ssh/pe-class-key-oregon.pem ec2-user@${HOST_DB1_PRIVATE_IP} ls -l /tidb-deploy/tidb-4000/
   ```
   ```output
   total 0
   drwxr-xr-x 2 ec2-user ec2-user 25 May 25 04:16 bin
   drwxr-xr-x 2 ec2-user ec2-user 23 May 25 04:16 conf
   drwxr-xr-x 3 ec2-user ec2-user 90 May 25 04:21 log
   drwxr-xr-x 2 ec2-user ec2-user 25 May 25 04:16 scripts
   ```

### 步骤 3. 检查 TiKV server 1 上的 TiKV  server 1服务器配置目录

+ 连接到 TiKV server 1 并查看 `TiKV server` 部署目录的内容。如果 `ssh` 提示，输入 `yes`:
   
   ```bash
   ssh -i ~/.ssh/pe-class-key-oregon.pem ec2-user@${HOST_KV1_PRIVATE_IP} ls -l /tidb-deploy/
   ```
   ```output
   total 0
   drwxr-xr-x 6 ec2-user ec2-user 55 May 25 04:16 monitor-9100
   drwxr-xr-x 6 ec2-user ec2-user 55 May 25 04:16 tikv-20160
   ```

+ 连接到 TiKV server 1 并查看 `TiKV server` 配置目录的内容。

   ```bash
   ssh -i ~/.ssh/pe-class-key-oregon.pem ec2-user@${HOST_KV1_PRIVATE_IP} ls -l /tidb-deploy/tikv-20160/
   ```
   ```output
   total 0
   drwxr-xr-x 2 ec2-user ec2-user 25 May 25 04:16 bin
   drwxr-xr-x 2 ec2-user ec2-user 23 May 25 04:16 conf
   drwxr-xr-x 2 ec2-user ec2-user 45 May 25 04:20 log
   drwxr-xr-x 2 ec2-user ec2-user 25 May 25 04:16 scripts
   ```

+ 连接到 TiKV server 1 并查看 `TiKV server` 数据目录的内容。 

   ```bash
   ssh -i ~/.ssh/pe-class-key-oregon.pem ec2-user@${HOST_KV1_PRIVATE_IP} ls -l /tidb-data/
   ```
   ```output
   total 0
   drwxr-xr-x 2 ec2-user ec2-user   6 May 25 04:16 monitor-9100
   drwxr-xr-x 6 ec2-user ec2-user 164 May 25 04:20 tikv-20160
   ```

   ```bash
   ssh -i ~/.ssh/pe-class-key-oregon.pem ec2-user@${HOST_KV1_PRIVATE_IP} ls -l /tidb-data/tikv-20160/
   ```
   ```output
   total 1048672
   drwxr-xr-x 2 ec2-user ec2-user        134 May 25 04:20 db
   drwxr-xr-x 4 ec2-user ec2-user         33 May 25 04:20 import
   -rw-r--r-- 1 ec2-user ec2-user      20330 May 25 04:20 last_tikv.toml
   -rw-r--r-- 1 ec2-user ec2-user          0 May 25 04:20 LOCK
   -rw-r--r-- 1 ec2-user ec2-user          0 May 25 04:20 raftdb.info
   drwxr-xr-x 2 ec2-user ec2-user         82 May 25 04:20 raft-engine
   -rw-r--r-- 1 ec2-user ec2-user      75538 May 25 04:20 rocksdb.info
   drwxr-xr-x 2 ec2-user ec2-user          6 May 25 04:21 snap
   -rw-r--r-- 1 ec2-user ec2-user 1073741824 May 25 04:20 space_placeholder_file
   ```

### 步骤 4. 检查 PD server 1 上的 Placement Driver server 配置目录

+ 连接到 Placement Driver server 1，查看 `PD server` 配置文件目录的内容。如果 `ssh` 提示，输入 `yes`。
   
   ```bash
   ssh -i ~/.ssh/pe-class-key-oregon.pem ec2-user@${HOST_PD1_PRIVATE_IP} ls -l /tidb-deploy/
   ```
   ```output
   total 0
   drwxr-xr-x 6 ec2-user ec2-user 55 May 25 04:16 monitor-9100
   drwxr-xr-x 6 ec2-user ec2-user 55 May 25 04:16 pd-2379
   ```

   ```bash
   ssh -i ~/.ssh/pe-class-key-oregon.pem ec2-user@${HOST_PD1_PRIVATE_IP} ls -l /tidb-deploy/pd-2379/
   ```
   ```output
   total 0
   drwxr-xr-x 2 ec2-user ec2-user 23 May 25 04:16 bin
   drwxr-xr-x 2 ec2-user ec2-user 21 May 25 04:16 conf
   drwxr-xr-x 2 ec2-user ec2-user 41 May 25 04:20 log
   drwxr-xr-x 2 ec2-user ec2-user 23 May 25 04:16 scripts
   ```

+ 连接到 Placement Driver server 1，查看 `PD server` 数据文件目录的内容。
   
   ```bash
   ssh -i ~/.ssh/pe-class-key-oregon.pem ec2-user@${HOST_PD1_PRIVATE_IP} ls -l /tidb-data/
   ```
   ```output
   total 0
   drwxr-xr-x 2 ec2-user ec2-user  6 May 25 04:16 monitor-9100
   drwxr-xr-x 5 ec2-user ec2-user 57 May 25 04:20 pd-2379
   ```

   ```bash
   ssh -i ~/.ssh/pe-class-key-oregon.pem ec2-user@${HOST_PD1_PRIVATE_IP} ls -l /tidb-data/pd-2379/
   ```
   ```output  
   total 0
   drwxr-xr-x 2 ec2-user ec2-user 85 May 25 04:20 hot-region
   drwx------ 4 ec2-user ec2-user 29 May 25 04:20 member
   drwxr-xr-x 2 ec2-user ec2-user 85 May 25 04:20 region-meta
   ```

### 步骤 5. 停止 TiDB 集群
   
   ```bash
   tiup cluster stop tidb-demo --yes
   ```
   ```output
   + [ Serial ] - SSHKeySet: privateKey=/home/ec2-user/.tiup/storage/cluster/clusters/tidb-demo/ssh/id_rsa, publicKey=/home/ec2-user/.tiup/storage/cluster/clusters/tidb-demo/ssh/id_rsa.pub
   + [Parallel] - UserSSH: user=ec2-user, host=10.90.2.95
   + [Parallel] - UserSSH: user=ec2-user, host=10.90.2.90
   + [Parallel] - UserSSH: user=ec2-user, host=10.90.2.158
   + [Parallel] - UserSSH: user=ec2-user, host=10.90.1.169
   + [Parallel] - UserSSH: user=ec2-user, host=10.90.1.56
   + [Parallel] - UserSSH: user=ec2-user, host=10.90.4.126
   + [Parallel] - UserSSH: user=ec2-user, host=10.90.4.138
   + [Parallel] - UserSSH: user=ec2-user, host=10.90.4.138
   + [Parallel] - UserSSH: user=ec2-user, host=10.90.4.138
   + [Parallel] - UserSSH: user=ec2-user, host=10.90.2.138
   + [Parallel] - UserSSH: user=ec2-user, host=10.90.2.242
   + [Parallel] - UserSSH: user=ec2-user, host=10.90.2.231
   + [ Serial ] - StopCluster
   ...
   Stopped cluster `tidb-demo` successfully
   ```

+ 退出远程 Linux

  ```bash
  exit
  ```

## 结论

在分布式 SQL 数据库管理领域，高效处理集群组件至关重要。TiUP 是 TiDB 生态系统的重要组成部分，旨在为集群管理任务带来简便性和灵活性。自 TiDB 4.0 引入以来，TiUP 彻底改变了用户与 TiDB 集群操作的交互方式，使用户能够以极少的精力管理和执行任务。

TiUP 的主要吸引力之一在于其简单的安装过程。只需一行命令，你就可以在 Darwin 和 Linux 操作系统上安装 TiUP。此命令通过在指定的 `$HOME/.tiup` 目录中设置 TiUP 并配置系统环境变量以确保无缝访问，从而简化了安装过程。 

欢迎使用 `Ask AI` 按钮咨询有关 TiUP 的任何问题。下一个实验见，再见。