启动 TiDB 数据库服务器的方法

我已经在Arch Linux上做了与这里相同的事情,尝试轻松启动了TiDB。

安装Docker

sudo pacman -S docker
sudo pacman -S docker-compose

启动

sudo systemctl start docker

安装代码

克隆

git clone https://github.com/pingcap/tidb-docker-compose.git
cd tidb-docker-compose
sudo docker-compose pull

确认图像

$ 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

服务器启动

$ 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

登录

$ 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)]>

确认数据库

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

数据库操作 de

创建如下:
用户: scott
密码: tiger123
数据库: city
允许 scott 访问 city。

执行以下的SQL语句

create user 'scott'@'%' identified by 'tiger123';
create schema city;
grant all on city.* to 'scott'@'%';
flush privileges;
exit
$ 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

确认用户已被创建

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

确认数据库已创建

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

请使用已创建的用户登录连接

mysql -h 127.0.0.1 -P 4000 -uscott -ptiger123

或者,

mysql -h localhost -P 4000 -uscott -ptiger123

执行SQL语句

执行结果

$ 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
use city;
drop table if exists cities;
create table cities (id varchar(10) primary key, name text, population int, date_mod date);
quit
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
use city;
select * from cities;
quit

停止服务器

$ 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

确认停止发生了

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

确认的版本

$ docker-compose --version
Docker Compose version 2.16.0
广告
将在 10 秒后关闭
bannerAds