## 模块 3: 在负载运行时升级 TiDB 集群

### 步骤 1. 设置 TiUP 镜像位置

+ 返回 [终端 1](ssh://monitor1-ssh#1)，运行 tiup 命令更改 TiUP 本地镜像至 v8.1.1。

  ```bash
  tiup mirror set tidb-community-server-8.1.1-linux-amd64
  ```

  ```output
  Successfully set mirror to /home/ec2-user/tidb-community-server-8.1.1-linux-amd64
  ```

### 步骤 2. 升级你的 TiDB 集群

+ 运行 `tiup` 命令将 TiDB 集群从 v7.5.4 升级至 v8.1.1。在升级进行时，切换到 [终端 2](ssh://monitor1-ssh#2) 和 [终端 3](ssh://monitor1-ssh#3)。

  ```bash
  tiup cluster upgrade tidb-demo 8.1.1 --yes
  ```

  ```output
  Before the upgrade, it is recommended to read the upgrade guide at https://docs.tidb.io/tidb/stable/upgrade-tidb-using-tiup and finish the preparation steps.
  This operation will upgrade tidb v7.5.4 cluster tidb-demo to v8.1.1:
  will upgrade and restart component "                 pd" to "v8.1.1",
  will upgrade and restart component "               tikv" to "v8.1.1",
  will upgrade and restart component "               tidb" to "v8.1.1",
  ...
  + [ Serial ] - Download: component=grafana, version=v8.1.1, os=linux, arch=amd64
  + [ Serial ] - Download: component=tikv, version=v8.1.1, os=linux, arch=amd64
  + [ Serial ] - Download: component=tidb, version=v8.1.1, os=linux, arch=amd64
  + [ Serial ] - Download: component=prometheus, version=v8.1.1, os=linux, arch=amd64
  + [ Serial ] - Download: component=pd, version=v8.1.1, os=linux, arch=amd64
  ...
  Upgrading component pd
  	Restarting instance 10.90.1.135:2379
  	Restart instance 10.90.1.135:2379 success
  	Restarting instance 10.90.2.182:2379
  	Restart instance 10.90.2.182:2379 success
  	Restarting instance 10.90.3.231:2379
  	Restart instance 10.90.3.231:2379 success
  Upgrading component tikv
  	Evicting 11 leaders from store 10.90.1.55:20160...
  	  Still waiting for 11 store leaders to transfer...
  	  Still waiting for 11 store leaders to transfer...
  ...
  Upgrading component tidb
  	Restarting instance 10.90.1.124:4000
  	Restart instance 10.90.1.124:4000 success
  	Restarting instance 10.90.2.122:4000
  	Restart instance 10.90.2.122:4000 success
  ...
  ```

### 步骤 3. 验证插入与查询负载未中断

+ 观察 [终端 2](ssh://monitor1-ssh#2) 和 [终端 3](ssh://monitor1-ssh#3)，确保示例应用程序连接和事务在升级期间从未中断。

+ 等待 [终端 1](ssh://monitor1-ssh#1) 中的升级命令完成，再进行下一步。

  ```output
  ...
  Upgraded cluster `tidb-demo` successfully
  ```

### 步骤 4. 验证升级结果

+ 运行命令获取集群状态并确认集群版本是 8.1.1。

  ```bash
  tiup cluster display tidb-demo
  ```

  ```output
  Cluster type:       tidb
  Cluster name:       tidb-demo
  Cluster version:    v8.1.1
  Deploy user:        ec2-user
  SSH type:           builtin
  Dashboard URL:      http://18.138.239.33:2379/dashboard
  Grafana URL:        http://10.90.4.8:3000
  ID                Role          Host         Ports        OS/Arch       Status  Data Dir                      Deploy Dir
  --                ----          ----         -----        -------       ------  --------                      ----------
  10.90.4.8:9093    alertmanager  10.90.4.8    9093/9094    linux/x86_64  Up      /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
  10.90.4.8:3000    grafana       10.90.4.8    3000         linux/x86_64  Up      -                             /tidb-deploy/grafana-3000
  10.90.1.152:2379  pd            10.90.1.152  2379/2380    linux/x86_64  Up|L    /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.2.73:2379   pd            10.90.2.73   2379/2380    linux/x86_64  Up      /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.3.163:2379  pd            10.90.3.163  2379/2380    linux/x86_64  Up      /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.4.8:9090    prometheus    10.90.4.8    9090/12020   linux/x86_64  Up      /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
  10.90.2.180:4000  tidb          10.90.2.180  4000/10080   linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
  10.90.3.89:4000   tidb          10.90.3.89   4000/10080   linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
  10.90.1.88:20160  tikv          10.90.1.88   20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.2.20:20160  tikv          10.90.2.20   20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.3.69:20160  tikv          10.90.3.69   20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.1.60:6000   tiproxy       10.90.1.60   6000/3080    linux/x86_64  Up      -                             /tidb-deploy/tiproxy-6000
  Total nodes: 12
  ```

+ 按 <kbd>Ctrl</kbd>+<kbd>C</kbd> 停止 [终端 2](ssh://monitor1-ssh#2) 和 [终端 3](ssh://monitor1-ssh#3) 中的工作负载。
