TiDB データベースサーバーの立ち上げ方

こちらと同じことを、Arch Linux で行いました。
[TiDB をお手軽に立ち上げてみる](https://qiita.com/okaru/items/4234ea64a902d4a1d36c)

## Docker のインストール

```bash
sudo pacman -S docker
sudo pacman -S docker-compose
```

起動

```bash
sudo systemctl start docker
```

## コードのインストール

クローン

```bash
git clone https://github.com/pingcap/tidb-docker-compose.git
```

```bash
cd tidb-docker-compose
sudo docker-compose pull
```

イメージの確認

```text
$ sudo docker images
REPOSITORY            TAG       IMAGE ID       CREATED        SIZE
pingcap/tikv          latest    9621b51b1282   2 months ago   549MB
pingcap/pd            latest    69c043a19b5d   2 months ago   163MB
pingcap/tidb          latest    500953de794e   2 months ago   198MB
pingcap/tispark       v2.1.1    501543755826   3 years ago    894MB
grafana/grafana       6.0.1     ffd9c905f698   3 years ago    241MB
pingcap/tidb-vision   latest    e9b25d9f7bdb   4 years ago    47.5MB
prom/prometheus       v2.2.1    cc866859f8df   4 years ago    113MB
prom/pushgateway      v0.3.1    434efa6ed9db   6 years ago    13.2MB
```

## サーバーの起動

```text
$ sudo docker-compose up -d
[+] Running 14/14
 ⠿ Network tidb-docker-compose_default             Created                 0.1s
 ⠿ Container tidb-docker-compose-pushgateway-1     Started                 1.4s
 ⠿ Container tidb-docker-compose-grafana-1         Started                 1.0s
 ⠿ Container tidb-docker-compose-pd1-1             Started                 1.6s
 ⠿ Container tidb-docker-compose-pd2-1             Started                 1.6s
 ⠿ Container tidb-docker-compose-tidb-vision-1     Started                 1.0s
 ⠿ Container tidb-docker-compose-pd0-1             Started                 1.6s
 ⠿ Container tidb-docker-compose-prometheus-1      Started                 1.6s
 ⠿ Container tidb-docker-compose-tikv0-1           Started                 2.3s
 ⠿ Container tidb-docker-compose-tikv2-1           Started                 2.2s
 ⠿ Container tidb-docker-compose-tikv1-1           Started                 2.4s
 ⠿ Container tidb-docker-compose-tispark-master-1  Started                 2.9s
 ⠿ Container tidb-docker-compose-tidb-1            Started                 2.9s
 ⠿ Container tidb-docker-compose-tispark-slave0-1  Started                 3.3s
```

## ログイン

```bash
$ mysql -h 127.0.0.1 -P 4000 -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 409
Server version: 5.7.25-TiDB-v6.5.0 TiDB Server (Apache License 2.0) Community Edition, MySQL 5.7 compatible

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> select version();
+--------------------+
| version()          |
+--------------------+
| 5.7.25-TiDB-v6.5.0 |
+--------------------+
1 row in set (0.001 sec)

MySQL [(none)]>
```

データベースの確認

```text
MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| INFORMATION_SCHEMA |
| METRICS_SCHEMA     |
| PERFORMANCE_SCHEMA |
| mysql              |
| test               |
+--------------------+
5 rows in set (0.001 sec)
```

## データベースの操作

次を作成します。
ユーザー　scott
パスワード　tiger123
データベース　city
scott が city にアクセスできるようにする。

次の SQL を実行

```text
create user 'scott'@'%' identified by 'tiger123';
create schema city;
grant all on city.* to 'scott'@'%';
flush privileges;
exit
```

```text
$ mysql -h 127.0.0.1 -P 4000 -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 511
Server version: 5.7.25-TiDB-v6.5.0 TiDB Server (Apache License 2.0) Community Edition, MySQL 5.7 compatible

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> create user 'scott'@'%' identified by 'tiger123';
Query OK, 0 rows affected (0.050 sec)

MySQL [(none)]> create schema city;
Query OK, 0 rows affected (0.198 sec)

MySQL [(none)]> grant all on city.* to 'scott'@'%';
Query OK, 0 rows affected (0.039 sec)

MySQL [(none)]> flush privileges;
Query OK, 0 rows affected (0.017 sec)

MySQL [(none)]> exit
Bye
```

ユーザーが作成されたことを確認

```text
MySQL [(none)]> select user,host from mysql.user;
+-------+-----------+
| user  | host      |
+-------+-----------+
| root  | %         |
| scott | %         |
+-------+-----------+
2 rows in set (0.003 sec)
```

データベースが作成されたことを確認

```text
MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| INFORMATION_SCHEMA |
| METRICS_SCHEMA     |
| PERFORMANCE_SCHEMA |
| city               |
| mysql              |
| test               |
+--------------------+
6 rows in set (0.000 sec)
```

## 作成したユーザーで接続

```bash
mysql -h 127.0.0.1 -P 4000 -uscott -ptiger123
```

又は、

```bash
mysql -h localhost -P 4000 -uscott -ptiger123
```

## SQL文 を実行

実行結果

```text
$ mysql -h 127.0.0.1 -P 4000 -uscott -ptiger123 < create_table.sql 
$ mysql -h 127.0.0.1 -P 4000 -uscott -ptiger123 < insert_data.sql 
$ mysql -h 127.0.0.1 -P 4000 -uscott -ptiger123 < read_data.sql 
id	name	population	date_mod
t3321	岡山	691734	2001-02-15
t3322	倉敷	923657	2001-08-01
t3323	津山	279358	2001-07-29
t3324	玉野	284615	2001-05-18
t3325	笠岡	741256	2001-01-17
t3326	井原	497521	2001-04-21
t3327	総社	217348	2001-07-15
t3328	高梁	152978	2001-09-11
t3329	新見	823495	2001-10-02
```

```text:create_table.sql
use city;
drop table if exists cities;
create table cities (id varchar(10) primary key, name text, population int, date_mod date);
quit
```

```text:insert_data.sql
use city;
insert into cities set id='t3321',name='岡山',population=691734,date_mod='2001-2-15';
insert into cities set id='t3322',name='倉敷',population=923657,date_mod='2001-8-1';
insert into cities set id='t3323',name='津山',population=279358,date_mod='2001-7-29';
insert into cities set id='t3324',name='玉野',population=284615,date_mod='2001-5-18';
insert into cities set id='t3325',name='笠岡',population=741256,date_mod='2001-1-17';
insert into cities set id='t3326',name='井原',population=497521,date_mod='2001-4-21';
insert into cities set id='t3327',name='総社',population=217348,date_mod='2001-7-15';
insert into cities set id='t3328',name='高梁',population=152978,date_mod='2001-9-11';
insert into cities set id='t3329',name='新見',population=823495,date_mod='2001-10-2';
quit
```

```text:read_data.sql
use city;
select * from cities;
quit
```

## サーバーの停止

```text
$ sudo docker-compose stop
[+] Running 13/13
 ⠿ Container tidb-docker-compose-prometheus-1      Stopped                 0.4s
 ⠿ Container tidb-docker-compose-tidb-vision-1     Stopped                10.4s
 ⠿ Container tidb-docker-compose-grafana-1         Stopped                 0.5s
 ⠿ Container tidb-docker-compose-tidb-1            Stopped                 2.0s
 ⠿ Container tidb-docker-compose-pushgateway-1     Stopped                 1.3s
 ⠿ Container tidb-docker-compose-tispark-slave0-1  Stopped                 0.0s
 ⠿ Container tidb-docker-compose-tispark-master-1  Stopped                 0.0s
 ⠿ Container tidb-docker-compose-tikv0-1           Stopped                 0.6s
 ⠿ Container tidb-docker-compose-tikv1-1           Stopped                 0.6s
 ⠿ Container tidb-docker-compose-tikv2-1           Stopped                 0.7s
 ⠿ Container tidb-docker-compose-pd2-1             Stopped                10.4s
 ⠿ Container tidb-docker-compose-pd0-1             Stopped                 2.1s
 ⠿ Container tidb-docker-compose-pd1-1             Stopped                 0.9s
```

停止したことの確認

```text
$ docker-compose ps
NAME                IMAGE               COMMAND             SERVICE             CREATED             STATUS              PORTS
```

## 確認したバージョン

```text
$ docker-compose --version
Docker Compose version 2.16.0
```