猿创征文 | 国产数据库实战之TiDB 数据库快速入门

Posted 江湖有缘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了猿创征文 | 国产数据库实战之TiDB 数据库快速入门相关的知识,希望对你有一定的参考价值。

猿创征文 | 国产数据库实战之TiDB 数据库快速入门

一、系统检查

1.检查系统版本

[root@TiDB ~]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core) 

2.查看本地IP地址

[root@TiDB ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.241  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::2a6e:d4ff:fe8a:1bd9  prefixlen 64  scopeid 0x20<link>
        ether 28:6e:d4:8a:1b:d9  txqueuelen 1000  (Ethernet)
        RX packets 54547  bytes 7063009 (6.7 MiB)
        RX errors 0  dropped 27  overruns 0  frame 0
        TX packets 203  bytes 26090 (25.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

3.TiDB集群介绍

TiDB 是一个分布式系统。最基础的 TiDB 测试集群通常由 2 个 TiDB 实例、3 个 TiKV 实例、3 个 PD 实例和可选的 TiFlash 实例构成。通过 TiUP Playground,可以快速搭建出上述的一套基础测试集群。

二、快速部署本地测试集群

1.安装 TiUP工具

[root@TiDB ~]# 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 7085k  100 7085k    0     0   947k      0  0:00:07  0:00:07 --:--:-- 1784k
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
Successfully set mirror to https://tiup-mirrors.pingcap.com
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
===============================================

2.声明全局环境变量

[root@TiDB ~]# source  /root/.bash_profile
[root@TiDB ~]# 

3.快速部署TiDB 集群

tiup playground v6.1.0 --db 2 --pd 3 --kv 3
CLUSTER START SUCCESSFULLY, Enjoy it ^-^
To connect TiDB: mysql --comments --host 127.0.0.1 --port 4001 -u root -p (no password)
To connect TiDB: mysql --comments --host 127.0.0.1 --port 4000 -u root -p (no password)
To view the dashboard: http://127.0.0.1:2379/dashboard
PD client endpoints: [127.0.0.1:2379 127.0.0.1:2382 127.0.0.1:2384]
To view the Prometheus: http://127.0.0.1:9090
To view the Grafana: http://127.0.0.1:3000

三、连接 TiDB 数据库

1.新开一个session 以访问 TiDB 数据库

Connecting to 192.168.3.241:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Tue Oct 11 23:28:56 2022 from 192.168.3.11
[root@TiDB ~]# tiup client
tiup is checking updates for component client ...timeout!
The component `client` version  is not installed; downloading from repository.
download https://tiup-mirrors.pingcap.com/client-v1.11.0-linux-amd64.tar.gz 4.49 MiB / 4.49 MiB 100.00% 9.81 MiB/s                                            
Starting component `client`: /root/.tiup/components/client/v1.11.0/tiup-client
Connected with driver mysql (5.7.25-TiDB-v6.1.0)
Type "help" for help.

my:root@127.0.0.1:4000=> show databases;
      Database      
--------------------
 INFORMATION_SCHEMA 
 METRICS_SCHEMA 
 PERFORMANCE_SCHEMA 
 mysql 
 test 
(5 rows)

my:root@127.0.0.1:4000=>  

2.通过Mysql客户端连接TiDB 数据库

mysql --host 127.0.0.1 --port 4000 -u root

四、清理TiDB 集群环境

tiup clean --all

五、TiDB数据库的用户管理

1.创建用户

my:root@127.0.0.1:4000=> CREATE USER 'admin'@'%' IDENTIFIED BY '123456';
CREATE USER

2.删除用户

drop user  'admin'@'%';

3.修改用户密码

my:root@127.0.0.1:4000=> SET PASSWORD FOR 'admin'@'%'  = PASSWORD('admin');
SET

4.给新建用户授权

my:root@127.0.0.1:4000=> grant all on *.* to 'admin'@'%' with grant option;
GRANT
my:root@127.0.0.1:4000=> flush privileges;
FLUSH

5.查看用户列表

my:root@127.0.0.1:4000=> select user,host  from mysql.user;
 user  | host 
-------+------
 admin | % 
 root  | % 
(2 rows)


六、配置外部访问

1.修改端口绑定IP

重新启动测试TiDB 数据库集群

[root@TiDB ~]# tiup playground v6.1.0 --db 2 --pd 3 --kv 3 --host 192.168.3.241
tiup is checking updates for component playground ...timeout!
Starting component `playground`: /root/.tiup/components/playground/v1.11.0/tiup-playground v6.1.0 --db 2 --pd 3 --kv 3 --host 192.168.3.241
Playground Bootstrapping...
Start pd instance:v6.1.0
Start pd instance:v6.1.0
Start pd instance:v6.1.0
Start tikv instance:v6.1.0
Start tikv instance:v6.1.0
Start tikv instance:v6.1.0
Start tidb instance:v6.1.0
Start tidb instance:v6.1.0
Waiting for tidb instances ready
192.168.3.241:4000 ... Done
192.168.3.241:4001 ... Done
Start tiflash instance:v6.1.0
Waiting for tiflash instances ready
192.168.3.241:3930 ... Done
CLUSTER START SUCCESSFULLY, Enjoy it ^-^
To connect TiDB: mysql --comments --host 192.168.3.241 --port 4001 -u root -p (no password)
To connect TiDB: mysql --comments --host 192.168.3.241 --port 4000 -u root -p (no password)
To view the dashboard: http://192.168.3.241:2379/dashboard
PD client endpoints: [192.168.3.241:2379 192.168.3.241:2382 192.168.3.241:2384]
To view the Prometheus: http://192.168.3.241:9090
To view the Grafana: http://192.168.3.241:3000

2.测试远程访问TiDB数据库

[root@node ~]# mysql -h192.168.3.241 -P4000 -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \\g.
Your MySQL connection id is 413
Server version: 5.7.25-TiDB-v6.1.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)]> 

3.访问TiDB的Prometheus管理界面

4.访问TiDB的Grafana 界面


5.访问TiDB Dashboard页面

七、创建测试数据表

1.创建数据库

MySQL [(none)]> create database mydata;
Query OK, 0 rows affected (0.13 sec)

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


2.新建数据表

MySQL [(none)]> use mydata;
Database changed
MySQL [mydata]> CREATE TABLE IF NOT EXISTS `student`(
    ->    `id` INT UNSIGNED AUTO_INCREMENT,
    ->    `name` VARCHAR(100) NOT NULL,
    ->    `gender` TINYINT NOT NULL,
    ->    `age` INT UNSIGNED,
    ->    `class` INT UNSIGNED,
    ->    PRIMARY KEY ( `id` )
    -> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.07 sec)

MySQL [mydata]> 

3.查询表中字段

MySQL [mydata]>  show columns from student;
+--------+------------------+------+------+---------+----------------+
| Field  | Type             | Null | Key  | Default | Extra          |
+--------+------------------+------+------+---------+----------------+
| id     | int(10) unsigned | NO   | PRI  | NULL    | auto_increment |
| name   | varchar(100)     | NO   |      | NULL    |                |
| gender | tinyint(4)       | NO   |      | NULL    |                |
| age    | int(10) unsigned | YES  |      | NULL    |                |
| class  | int(10) unsigned | YES  |      | NULL    |                |
+--------+------------------+------+------+---------+----------------+

4.新增字段

MySQL [mydata]> alter table student add course tinyint;
Query OK, 0 rows affected (0.25 sec)

5.修改字段类型

alter table student modify   course VARCHAR(100);

6.向数据表插入数据

MySQL [mydata]> insert into student ( name, gender, age, class, course  )  values ( "陈静", "0", "22", "4",  "英语"  ),  ( "王成", "1", "15", "5", "化学"   ), ( "李一天", "1",  "18",  "5", "物理"    );
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

7.查看数据表

MySQL [mydata]> select * from student;
+----+-----------+--------+------+-------+--------+
| id | name      | gender | age  | class | course |
+----+-----------+--------+------+-------+--------+
|  1 | 陈静      |      0 |   22 |     4 | 英语   |
|  2 | 王成      |      1 |   15 |     5 | 化学   |
|  3 | 李一天    |      1 |   18 |     5 | 物理   |
+----+-----------+--------+------+-------+--------+
3 rows in set (0.00 sec)

八、在TiDB Dashboard界面查询信息

1.查询集群实例状态

2.查询存储拓扑

3.查询流量可视化

4.查看SQL语句分析

5.生成集群诊断报告

九、使用SQLyog查看数据表

以上是关于猿创征文 | 国产数据库实战之TiDB 数据库快速入门的主要内容,如果未能解决你的问题,请参考以下文章

猿创征文 | 国产数据库实战之使用Docker部署TiDB集群

猿创征文 | 国产数据库实战之使用Docker部署TiDB集群

猿创征文|分布式国产数据库 TiDB 从入门到实战

猿创征文 | 国产数据库TiDB架构特性

猿创征文 | 国产数据库实战之使用Docker部署OceanBase 数据库

猿创征文 | 国产数据实战之docker部署MyWebSQL数据库管理工具