## Module 1: Build a TiDB Cluster and Setup Testing Schema

In this module, we build a TiDB cluster and setup the testing schema. We use sysbench as our workload in this lab.

### Step 1. Connect to the TiUP control machine with one terminal

+ Open one SSH connection to your lab virtual machine, by clicking:
  + [Terminal 1](ssh://monitor1-ssh#1)

### Step 2. Deploy and start a TiDB cluster

+ In [Terminal 1](ssh://monitor1-ssh#1), review the provisioned cluster topology YAML.

  ```bash
  cat nine-nodes.yaml
  ```

  ```output
  global:
   user: "ec2-user"
   ssh_port: 22
   deploy_dir: "/tidb-deploy"
   data_dir: "/tidb-data"
   arch: "amd64"
  server_configs:
   pd:
     replication.max-replicas: 3
  pd_servers:
   - host: 10.90.2.245
     client_port: 2379
     peer_port: 2380
     advertise_client_addr: "http://13.212.249.174:2379"
   - host: 10.90.2.19
     client_port: 2379
     peer_port: 2380
     advertise_client_addr: "http://13.212.113.189:2379"
   - host: 10.90.2.213
     client_port: 2379
     peer_port: 2380
     advertise_client_addr: "http://13.229.88.9:2379"
  tidb_servers:
   - host: 10.90.1.92
     port: 4000
     status_port: 10080
     deploy_dir: /tidb-deploy/tidb-4000
     log_dir: /tidb-deploy/tidb-4000/log
   - host: 10.90.1.139
     port: 4000
     status_port: 10080
     deploy_dir: /tidb-deploy/tidb-4000
     log_dir: /tidb-deploy/tidb-4000/log
  tikv_servers:
   - host: 10.90.3.14
     port: 20160
     status_port: 20180
   - host: 10.90.3.196
     port: 20160
     status_port: 20180
   - host: 10.90.3.96
     port: 20160
     status_port: 20180
  monitoring_servers:
   - host: 10.90.4.181
  grafana_servers:
   - host: 10.90.4.181
  alertmanager_servers:
   - host: 10.90.4.181
  ```

+ Fix system configurations by executing `tiup cluster check` with `--apply` option.

> [!TIP]
> In the output of the following command, you will notice some system check failures, but don't worry - TiUP will automatically resolve them for you later.

  ```bash
  tiup mirror set tidb-community-server-${LAB:TIDB_VERSION}-linux-amd64
  tiup cluster check nine-nodes.yaml \
    --user ec2-user \
    -i /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem \
    --apply
  ```

  ```output
  ...
  + Try to apply changes to fix failed checks
  + Try to apply changes to fix failed checks
  + Try to apply changes to fix failed checks
    - Applying changes on 10.90.1.139 ... ⠧ Sysctl: host=10.90.1.139 vm.swappiness = 0
    - Applying changes on 10.90.4.181 ... ⠧ Shell: host=10.90.4.181, sudo=true, command=`if [ -d /sys/kernel/mm/transparent_hugepage ]; then echo never > /sys/kernel/mm/transparent_hugepage/enabled; fi`
  + Try to apply changes to fix failed checks
  + Try to apply changes to fix failed checks
    - Applying changes on 10.90.1.139 ... Done
  + Try to apply changes to fix failed checks
  + Try to apply changes to fix failed checks
    - Applying changes on 10.90.1.139 ... Done
    - Applying changes on 10.90.4.181 ... Done
    - Applying changes on 10.90.2.245 ... Done
    - Applying changes on 10.90.2.19 ... Done
    - Applying changes on 10.90.3.14 ... Done
    - Applying changes on 10.90.3.196 ... Done
    - Applying changes on 10.90.1.92 ... Done
    - Applying changes on 10.90.2.213 ... Done
    - Applying changes on 10.90.3.96 ... Done
  ```

+ Deploy a TiDB cluster.

  ```bash
  tiup cluster deploy tidb-demo ${LAB:TIDB_VERSION} ./nine-nodes.yaml \
    --user ec2-user \
    -i /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem \
    --yes
  ```

  ```output
  ...
  Cluster `tidb-demo` deployed successfully, you can start it with command: `tiup cluster start tidb-demo --init`
  ```

+ Start the deployed TiDB cluster.

  ```bash
  tiup cluster start tidb-demo
  ```

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

+ Run the command to get the cluster status. There are 3 PD servers, 3 TiKV servers, and 2 TiDB servers. Your environment and the sample output will differ in the details of the IP addresses, but the subnets are the same.

> [!TIP]
> In the output of the following command, if you find any components in a `Down` state, wait a few seconds and run the display command again.

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

  ```output
  Cluster type:       tidb
  Cluster name:       tidb-demo
  Cluster version:    v${LAB:TIDB_VERSION}
  Deploy user:        ec2-user
  SSH type:           builtin
  Dashboard URL:      http://35.92.83.178:2379/dashboard
  Grafana URL:        http://10.90.4.99:3000
  ID                 Role          Host         Ports        OS/Arch       Status  Data Dir                      Deploy Dir
  --                 ----          ----         -----        -------       ------  --------                      ----------
  10.90.4.99:9093    alertmanager  10.90.4.99   9093/9094    linux/x86_64  Up      /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
  10.90.4.99:3000    grafana       10.90.4.99   3000         linux/x86_64  Up      -                             /tidb-deploy/grafana-3000
  10.90.3.142:2379   pd            10.90.3.142  2379/2380    linux/x86_64  Up      /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.3.218:2379   pd            10.90.3.218  2379/2380    linux/x86_64  Up|L    /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.3.78:2379    pd            10.90.3.78   2379/2380    linux/x86_64  Up      /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.4.99:9090    prometheus    10.90.4.99   9090/12020   linux/x86_64  Up      /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
  10.90.1.152:4000   tidb          10.90.1.152  4000/10080   linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
  10.90.1.192:4000   tidb          10.90.1.192  4000/10080   linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
  10.90.2.183:20160  tikv          10.90.2.183  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.93:20160   tikv          10.90.2.93   20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  Total nodes: 11
  ```

+ Review and run the script to prepare the `sysbench` schema. Wait for approximately 10 minutes until the setup process is complete.

  ```bash
  source ./hosts-env.sh

  mysql -h ${HOST_DB1_PRIVATE_IP} -uroot -P4000 --verbose << 'EOFX'
  CREATE DATABASE sbtest;
  EOFX

  sysbench '/usr/local/share/sysbench/oltp_read_only.lua' \
  --mysql-host=${HOST_DB1_PRIVATE_IP} \
  --mysql-port=4000 \
  --mysql-user=root \
  --mysql-db=sbtest \
  --percentile=99 \
  --threads=10 \
  --tables=1 \
  --table-size=10000000 \
  --report-interval=2 \
  --db-driver=mysql prepare
  ```

  ```output
  sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)

  Initializing worker threads...

  Creating table 'sbtest1'...
  Inserting 10000000 records into 'sbtest1'
  Creating a secondary index on 'sbtest1'...
  ```
