## Module 1: Creating Users and Roles

### Step 1. Log into the remote Linux

+ Click here - [Terminal 1](ssh://monitor1-ssh#1) to open a new terminal connected to the virtual machine assigned for your hands-on lab.

### Step 2. Connect to TiDB

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

### Step 3. Create user `jack`

+ Create a new user called `'jack'@'${LAB:HOST_MONITOR1_PRIVATE_IP}'`.

  ```sql
  CREATE USER 'jack'@'${LAB:HOST_MONITOR1_PRIVATE_IP}' IDENTIFIED BY 'pingcap';
  ```

  ```output
  tidb> CREATE USER 'jack'@'${LAB:HOST_MONITOR1_PRIVATE_IP}' IDENTIFIED BY 'pingcap';
  Query OK, 0 rows affected (0.03 sec)
  ```

### Step 4. Create new roles

+ Create two new roles called `r_manager` and `r_staff`

  ```sql
  CREATE ROLE r_manager, r_staff;
  ```

  ```output
  tidb> CREATE ROLE r_manager, r_staff;
  Query OK, 0 rows affected (0.02 sec)
  ```

### Step 5. Check the users and roles

  ```sql
  SELECT user, host, authentication_string FROM mysql.user\G
  ```

  ```output
  tidb> SELECT user, host, authentication_string FROM mysql.user\G
  *************************** 1. row ***************************
                    user: root
                    host: %
  authentication_string: *7451D85E369BF796CD15900AF9BFE0D5D920716E
  *************************** 2. row ***************************
                    user: jack
                    host: 172.31.0.106
  authentication_string: *926E4B88EB93FD344DF0870EE025D6EB153C02DE
  *************************** 3. row ***************************
                    user: r_manager
                    host: %
  authentication_string:
  *************************** 4. row ***************************
                    user: r_staff
                    host: %
  authentication_string:
  4 rows in set (0.00 sec)
  ```

+ Both the users and roles are stored in the `mysql.user` table.  Roles have no password.

### Step 6. Check `r_staff`

  ```sql
  SELECT * FROM mysql.user WHERE user='r_staff'\G
  ```

  ```output
  tidb> SELECT * FROM mysql.user WHERE user='r_staff'\G
  *************************** 1. row ***************************
                    Host: %
                    User: r_staff
   authentication_string:
                  plugin: mysql_native_password
             Select_priv: N
             Insert_priv: N
             Update_priv: N
             Delete_priv: N
             Create_priv: N
               Drop_priv: N
               ...
              Event_priv: N
         Repl_slave_priv: N
        Repl_client_priv: N
            Trigger_priv: N
        Create_role_priv: N
          Drop_role_priv: N
          Account_locked: Y
           Shutdown_priv: N
             Reload_priv: N
               FILE_priv: N
             Config_priv: N
  Create_Tablespace_Priv: N
  1 row in set (0.00 sec)
  ```

### Step 7. Change the password for `jack`

+ Change the password for `'jack'@'${LAB:HOST_MONITOR1_PRIVATE_IP}'` to `tidb` by using the `ALTER USER` statement and then exit the session.

  ```sql
  ALTER USER 'jack'@'${LAB:HOST_MONITOR1_PRIVATE_IP}' IDENTIFIED BY 'tidb';
  ```

  ```output
  tidb> ALTER USER 'jack'@'${LAB:HOST_MONITOR1_PRIVATE_IP}' IDENTIFIED BY 'tidb';
  Query OK, 0 rows affected (0.02 sec)
  ```

  ```sql
  EXIT;
  ```

  ```output
  tidb> EXIT
  Bye
  ```

### Step 8. Connect to TiDB as `jack`

+ Connect to TiDB as the `'jack'@'${LAB:HOST_MONITOR1_PRIVATE_IP}’` user with the new password, and then exit the session.

  ```bash
  mysql -h ${HOST_DB1_PRIVATE_IP} -P 4000 -ujack -ptidb
  ```

  ```sql
  EXIT
  ```

  ```output
  tidb> EXIT
  Bye
  ```

### Step 9. Connect to TiDB as `root`

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

### Step 10. Remove `r_staff`

  ```sql
  DROP ROLE r_staff;
  ```

  ```output
  tidb> DROP ROLE r_staff;
  Query OK, 0 rows affected (0.03 sec)
  ```

  ```sql
  SELECT user, host FROM mysql.user WHERE user='r_staff';
  ```

  ```output
  tidb> SELECT user, host FROM mysql.user WHERE user='r_staff';
  Empty set (0.01 sec)
  ```

### Step 11. Remove `r_manager`

  ```sql
  DROP ROLE r_manager;
  ```

  ```output
  tidb> DROP ROLE r_manager;
  Query OK, 0 rows affected (0.03 sec)
  ```

  ```sql
  SELECT user, host FROM mysql.user WHERE user='r_manager';
  ```

  ```output
  tidb> SELECT user, host FROM mysql.user WHERE user='r_manager';
  Empty set (0.01 sec)
  ```

### Step 12. Remove user `jack`

+ Remove the `'jack'@'${LAB:HOST_MONITOR1_PRIVATE_IP}’` user using `DROP USER` statement.

  ```sql
  DROP USER 'jack'@'${LAB:HOST_MONITOR1_PRIVATE_IP}';
  ```

  ```output
  tidb> DROP USER 'jack'@'${LAB:HOST_MONITOR1_PRIVATE_IP}';
  Query OK, 0 rows affected (0.02 sec)
  ```

  ```sql
  SELECT user, host FROM mysql.user WHERE user='jack';
  ```

  ```output
  tidb> SELECT user, host FROM mysql.user WHERE user='jack';
  Empty set (0.00 sec)
  ```

  ```sql
  EXIT
  ```

  ```output
  tidb> EXIT
  Bye
  ```
