## 模块 1: TiDB 集群和数据初始化

### 步骤 1. 连接到 TiUP 控制机

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

### 步骤 2. 创建一个新 TiDB 集群

+ 以下代码块中的命令将按顺序执行一系列操作：1. 更改 [TiUP mirror](https://docs.pingcap.com/tidb/stable/production-deployment-using-tiup) 设置，2. 检查系统设置，3. 修复失败的检查，4. 部署 TiDB 集群，最后 5. 启动 TiDB 集群。大约需要 3 分钟完成。

  ```bash
  export PATH="$PATH:/home/ec2-user/.tiup/bin"
  tiup mirror set tidb-community-server-${LAB:TIDB_VERSION}-linux-amd64
  tiup cluster check ./topology-with-tiproxy.yaml \
   --user ec2-user \
   -i /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem \
   --apply
  tiup cluster deploy tidb-demo v${LAB:TIDB_VERSION} ./topology-with-tiproxy.yaml \
   --user ec2-user \
   -i /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem \
   --yes
  tiup cluster start tidb-demo
  sleep 5;
  tiup cluster display tidb-demo
  ```

  ```output
  Successfully set mirror to /home/ec2-user/tidb-community-server-${LAB:TIDB_VERSION}-linux-amd64
  /home/ec2-user/tidb-community-server-${LAB:TIDB_VERSION}-linux-amd64
  ...
  Cluster type:       tidb
  Cluster name:       tidb-demo
  Cluster version:    v${LAB:TIDB_VERSION}
  Deploy user:        ec2-user
  SSH type:           builtin
  Dashboard URL:      http://52.27.45.10:2379/dashboard
  Grafana URL:        http://10.90.4.40:3000
  ID                 Role          Host         Ports                            OS/Arch       Status  Data Dir                      Deploy Dir
  --                 ----          ----         -----                            -------       ------  --------                      ----------
  10.90.4.40:9093    alertmanager  10.90.4.40   9093/9094                        linux/x86_64  Up      /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
  10.90.4.40:3000    grafana       10.90.4.40   3000                             linux/x86_64  Up      -                             /tidb-deploy/grafana-3000
  10.90.2.51:2379    pd            10.90.2.51   2379/2380                        linux/x86_64  Up|L    /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.4.40:9090    prometheus    10.90.4.40   9090/12020                       linux/x86_64  Up      /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
  10.90.2.14:4000    tidb          10.90.2.14   4000/10080                       linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
  10.90.2.43:4000    tidb          10.90.2.43   4000/10080                       linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
  10.90.4.243:9000   tiflash       10.90.4.243  9000/3930/20170/20292/8234/8123  linux/x86_64  Down    /tidb-data/tiflash-9000       /tidb-deploy/tiflash-9000
  10.90.1.208:20160  tikv          10.90.1.208  20160/20180                      linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.1.30:20160   tikv          10.90.1.30   20160/20180                      linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.1.93:20160   tikv          10.90.1.93   20160/20180                      linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.4.154:6000   tiproxy       10.90.4.154  6000/3080                        linux/x86_64  Up      -                             /tidb-deploy/tiproxy-6000
  Total nodes: 11
  ```

### 步骤 3. 创建表和数据导入

+ 连接到 TiDB。

  ```bash
  mysql -h ${HOST_TIPROXY1_PRIVATE_IP} --port 6000 -u root
  ```

  ```output
  mysql> 
  ```

+ 设置四个名为 `ddl_test1` 和 `ddl_test2` 和 `ddl_test3` 和 `ddl_test4` 的测试表。

  ```sql
  DROP TABLE IF EXISTS test.ddl_test1;
  DROP TABLE IF EXISTS test.ddl_test2;
  DROP TABLE IF EXISTS test.ddl_test3;
  DROP TABLE IF EXISTS test.ddl_test4;
  CREATE TABLE test.ddl_test1 (
      `id` BIGINT,
      `content` INT,
      `created_at` DATETIME,
      `updated_at` DATETIME,
      `version` INT
  );
  CREATE TABLE test.ddl_test2 (
      `id` BIGINT,
      `content` INT,
      `created_at` DATETIME,
      `updated_at` DATETIME,
      `version` INT
  );
  CREATE TABLE test.ddl_test3 (
      `id` BIGINT,
      `content` INT,
      `created_at` DATETIME,
      `updated_at` DATETIME,
      `version` INT
  );
  CREATE TABLE test.ddl_test4 (
      `id` BIGINT,
      `content` INT,
      `created_at` DATETIME,
      `updated_at` DATETIME,
      `version` INT
  );
  ```

+ 向 4 张表中各插入一行数据。

  ```sql
  INSERT INTO test.ddl_test1 (
      id, content, created_at, updated_at, version
  ) VALUES (
      FLOOR(1 + (RAND() * 99999999)), FLOOR(RAND() * 100) + 1 , NOW(), NOW(), 1
  );
  INSERT INTO test.ddl_test2 (
      id, content, created_at, updated_at, version
  ) VALUES (
      FLOOR(1 + (RAND() * 99999999)), FLOOR(RAND() * 100) + 1 , NOW(), NOW(), 1
  );
  INSERT INTO test.ddl_test3 (
      id, content, created_at, updated_at, version
  ) VALUES (
      FLOOR(1 + (RAND() * 99999999)), FLOOR(RAND() * 100) + 1 , NOW(), NOW(), 1
  );
  INSERT INTO test.ddl_test4 (
      id, content, created_at, updated_at, version
  ) VALUES (
      FLOOR(1 + (RAND() * 99999999)), FLOOR(RAND() * 100) + 1 , NOW(), NOW(), 1
  );
  SELECT * FROM test.ddl_test1;
  SELECT * FROM test.ddl_test2;
  SELECT * FROM test.ddl_test3;
  SELECT * FROM test.ddl_test4;
  EXIT
  ```

+ 扩展 4 张表的数据量各自到 4,194,304 行。大约需要 2 分钟完成。

  ```bash
  cat > double.sql <<EOF
  INSERT INTO test.ddl_test1 (
      id, content, created_at, updated_at, version
  )
  SELECT FLOOR(1 + (RAND() * 99999999)), FLOOR(RAND() * 100) + 1, NOW(), NOW(), 1 FROM test.ddl_test1;
  INSERT INTO test.ddl_test2 (
      id, content, created_at, updated_at, version
  )
  SELECT FLOOR(1 + (RAND() * 99999999)), FLOOR(RAND() * 100) + 1, NOW(), NOW(), 1 FROM test.ddl_test2;
  INSERT INTO test.ddl_test3 (
      id, content, created_at, updated_at, version
  )
  SELECT FLOOR(1 + (RAND() * 99999999)), FLOOR(RAND() * 100) + 1, NOW(), NOW(), 1 FROM test.ddl_test3;
  INSERT INTO test.ddl_test4 (
      id, content, created_at, updated_at, version
  )
  SELECT FLOOR(1 + (RAND() * 99999999)), FLOOR(RAND() * 100) + 1, NOW(), NOW(), 1 FROM test.ddl_test4;
  EOF
  cat > double.sh <<EOF
  mysql -h ${HOST_TIPROXY1_PRIVATE_IP} --port 6000 -u root < double.sql
  EOF
  chmod +x double.sh
  for c in {1..22}; do
  ./double.sh
  done;
  ```

  ```bash
  mysql -h ${HOST_TIPROXY1_PRIVATE_IP} --port 6000 -u root
  ```

  ```sql
  SELECT count(*) FROM test.ddl_test1;
  SELECT count(*) FROM test.ddl_test2;
  SELECT count(*) FROM test.ddl_test3;
  SELECT count(*) FROM test.ddl_test4;
  EXIT
  ```

### 步骤 4. 使用 TiUP bench 组件准备压测数据

+ 使用 TiUP bench 组件准备压测数据，大约需要 1 分钟完成。 关于 TiUP bench 组件的使用请参考：<https://docs.pingcap.com/zh/tidb/stable/tiup-bench/>

   ```bash
   export PATH="$PATH:/home/ec2-user/.tiup/bin"
   tiup bench tpcc -H ${HOST_DB1_PRIVATE_IP},${HOST_DB2_PRIVATE_IP} -P 4000 -D tpcc --warehouses 4 --parts 4  --threads 20 prepare
   ```

   ```output
   A new version of bench is available:  -> v1.12.0

      To update this component:   tiup update bench
      To update all components:   tiup update --all

   The component `bench` version  is not installed; downloading from repository.
   Starting component bench: /home/ec2-user/.tiup/components/bench/v1.12.0/tiup-bench tpcc -H 10.90.2.91,10.90.2.63 -P 4000 -D tpcc --warehouses 4 --parts 4 --threads 20 prepare
   creating table warehouse
   creating table district
   creating table customer
   creating table history
   creating table new_order
   creating table orders
   creating table order_line
   creating table stock
   creating table item
   load to item
   load to warehouse in warehouse 3
   load to customer in warehouse 1 district 10
   ...
   begin to check warehouse 1 at condition 3.3.2.6
   begin to check warehouse 2 at condition 3.3.2.9
   begin to check warehouse 2 at condition 3.3.2.10
   begin to check warehouse 4 at condition 3.3.2.11
   begin to check warehouse 4 at condition 3.3.2.12
   Finished
   ```

+ 到此为止，TiDB 集群和数据已经准备完毕，可以开始进行 ADMIN PAUSE DDL 和 ADMIN RESUE DDL 的学习了。
