课程名称:2.2 How to connect TiDB platform(如何连接到 TiDB)

学习时长:1h

课程收获:

本节课程主要介绍了如何使用 TiDB,通过实例介绍了如何使用相关工具连接 TiDB 。

课程内容:

学习目标: 能够使用相关工具连接 TiDB

关键知识点: MySQL 协议;客户端工具;应用程序工具; ORM

如何连接到TiDB?
成功部署 TiDB 集群之后,便可以在 TiDB 中执行 SQL 语句了。因为 TiDB 兼容 MySQL,你可以使用 MySQL 客户端连接 TiDB,并且大多数情况下可以直接执行 MySQL 语句。
image

连接方法
image

Driver
image

ORM(ORM全称Object Relational Mapping,即对象关系映射)
image

DEMO
1.使用TiUP部署本地集群
第一种:使用 TiUP Playground 快速部署本地测试环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
[root@qinxi ~]# curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 8608k 100 8608k 0 0 6586k 0 0:00:01 0:00:01 --:--:-- 6591k
WARN: adding root certificate via internet: https://tiup-mirrors.pingcap.com/root.json
You can revoke this by remove /root/.tiup/bin/7b8e153f2e2d0928.root.json
Set mirror to https://tiup-mirrors.pingcap.com success
Detected shell: bash
Shell profile: /root/.bash_profile
/root/.bash_profile has been modified to add tiup to PATH
open a new terminal or source /root/.bash_profile to use it
Installed path: /root/.tiup/bin/tiup
===============================================
Have a try: tiup playground
===============================================

[root@qinxi ~]# source .bash_profile

[root@qinxi ~]# tiup playground --db 2 --kv 3
The component `playground` is not installed; downloading from repository.
download https://tiup-mirrors.pingcap.com/playground-v1.2.5-linux-amd64.tar.gz 8.28 MiB / 8.28 MiB 100.00% ? p/s
Starting component `playground`: /root/.tiup/components/playground/v1.2.5/tiup-playground --db 2 --kv 3
Use the latest stable version: v4.0.8

Specify version manually: tiup playground <version>
The stable version: tiup playground v4.0.0
The nightly version: tiup playground nightly

Playground Bootstrapping...
The component `prometheus` is not installed; downloading from repository.
download https://tiup-mirrors.pingcap.com/prometheus-v4.0.8-linux-amd64.tar.gz 39.84 MiB / 39.84 MiB 100.00% 17.11 MiB p/s
download https://tiup-mirrors.pingcap.com/grafana-v4.0.8-linux-amd64.tar.gz 54.25 MiB / 54.25 MiB 100.00% 15.26 MiB p/s
Start pd instance
The component `pd` is not installed; downloading from repository.
download https://tiup-mirrors.pingcap.com/pd-v4.0.8-linux-amd64.tar.gz 38.85 MiB / 38.85 MiB 100.00% 17.67 MiB p/s
Start tikv instance
The component `tikv` is not installed; downloading from repository.
download https://tiup-mirrors.pingcap.com/tikv-v4.0.8-linux-amd64.tar.gz 135.46 MiB / 135.46 MiB 100.00% 12.91 MiB p/s
Start tikv instance
Start tikv instance
Start tidb instance
The component `tidb` is not installed; downloading from repository.
download https://tiup-mirrors.pingcap.com/tidb-v4.0.8-linux-amd64.tar.gz 39.16 MiB / 39.16 MiB 100.00% 17.60 MiB p/s
Start tidb instance
Waiting for tidb 127.0.0.1:4000 ready ... ⠏
Waiting for tidb 127.0.0.1:4000 ready ... Done
Waiting for tidb 127.0.0.1:4001 ready ... Done
Waiting for tikv 127.0.0.1:20160 ready ... Done
Waiting for tikv 127.0.0.1:20161 ready ... Done
Waiting for tikv 127.0.0.1:20162 ready ... Done
Start tiflash instance
The component `tiflash` is not installed; downloading from repository.
download https://tiup-mirrors.pingcap.com/tiflash-v4.0.8-linux-amd64.tar.gz 360.61 MiB / 360.61 MiB 100.00% 12.09 MiB p/s
Waiting for tiflash 127.0.0.1:3930 ready ... ⠴
Waiting for tiflash 127.0.0.1:3930 ready ... ⠏
Logging debug to /root/.tiup/data/SJVO2Hp/tiflash-0/log/tiflash.log
Logging errors to /root/.tiup/data/SJVO2Hp/tiflash-0/log/tiflash_error.log
...
Waiting for tiflash 127.0.0.1:3930 ready ... Error
TiFlash failed to start: store 127.0.0.1:3930 failed to up after timeout(180s)
CLUSTER START SUCCESSFULLY, Enjoy it ^-^
To connect TiDB: mysql --host 127.0.0.1 --port 4000 -u root
To connect TiDB: mysql --host 127.0.0.1 --port 4001 -u root
To view the dashboard: http://127.0.0.1:2379/dashboard
To view the Prometheus: http://127.0.0.1:9090
To view the Grafana: http://127.0.0.1:3000

2.MySQL官方客户端连接TiDB
image
3.PhpMyAdmin本地图形化管理TiDB
image
image
4.通过Django+TiDB例子管理TiDB

学习过程中参考的其他资料