TiDB介绍

TiDB概述

https://docs.pingcap.com/zh/tidb/stable TiDB 简介

安装部署

主机:192.168.56.101
集群:tidb_test
版本:4.0.9

部署集群:
tiup cluster deploy ccc v4.0.9 ./topo.yaml –user root -p

启动集群:tiup cluster start tidb_test
停止集群:tiup cluster stop tidb_test
查看已经部署的集群:tiup cluster list
查看集群拓扑结构和状态:tiup cluster display tidb_test

访问TiDB的Grafana监控:http://{grafana-ip}:3000,用户名/密码:admin/admin
访问TiDB的Dashboard:http://{pd-ip}:2379/dashboard,用户名/密码:root/空

SQL基本操作

DDL

数据库

数据库:表和索引的集合。

查看数据库:show databases;
使用数据库:use db_name;
查看数据表:show tables from db_name;

创建数据库:create database db_name [options]; 例如:create database if not exists test;
删除数据库:drop database db_name;

数据表

新建数据表:create table table_name column_name datatype constraint;
例如:
create table person (
id INT(11),
name varchar(125),
birth DATE
);

查看创建表语句:show create table table_name;
查看表结构:desc table_name;
删除表:drop table table_name;

索引

创建索引,2种方式:
方式1:create index index_name on table_name(column_name);
方式2:alter table table_name add index index_name(column_name);

例如:
create index idx_id on person(id);
或者
alter table person add index idx_id(id);

查看索引:show index from table_name
例如:
show index from person;

删除索引,跟创建索引类似,删除索引也有2种方式:
方式1:drop index index_name on table_name;
方式2:alter table table_name drop index index_name;

例如:
drop index idx_id on person;
或者
alter table person drop index idx_id;

DML

查询记录

查询记录时,可以查询全部字段,也可以查询部分字段。
方式1:查询全部字段
select * from table_name where conditions;
例如:
select * from person where id < 10;

方式2:查询指定列表
select column1,column2,… fro table_name where conditions;
例如:
select id,name from person where id < 10;

通常来讲,在查询语句中指定列名,另外应该通过条件限制查询的记录数。

添加记录

添加记录有2种方式,不指定列表或者指定部分列表。
方式1:不明确指定列表,默认插入所有列
insert into table_name values(col_value);
例如:
insert into person values(1,’zhangsan’,”20201220”);

方式2:明确指定列表,插入指定列的值
insert into table_name(column1,column2) values(val1,val2);
例如:
insert into person(id,name,birth) values(2,’lisi’,’20201221’);

更新记录

通过where条件更新指定记录,否则更新全部记录。

update table_name set column_name = value where conditions;

例如:
update person set name = ‘wangwu’ where id = 2;

在update语句中带where条件是安全的操作方式。

删除记录

通过where条件删除指定记录,否则删除全部记录。

delete from table_name where conditions;

例如:
delete from person where id = 2;

在删除语句中带where条件是安全的操作方式。

DCL

常见的DCL操作是对用户进行的,如:添加/删除用户,授权用户权限。

创建用户

创建用户时可以只是单纯地创建用户,或者在创建用户时设置访问授权信息。

方式1;单纯地创建用户
create user ‘user_name’ indentified by ‘password’;

例如:
create user ‘demo’ identified by ‘123456’;

方式2:创建用户时设置只能通过指定IP访问。

create user ‘user_name‘@’host’ identified by ‘password’;

例如:
create user ‘demo‘@’localhost’ identified by ‘123456’;

用户demo只能通过localhost地址访问TiDB。

给用户授权

给用户授权包含3个方面的信息:

1.授权指定用户可以访问自定数据库的哪些表
2.授权指定用户可以对指定数据库表执行的操作类型
3.设置指定用户可以访问的主机地址

GRANT operations ON database_name.* TO ‘user_name‘@’host’;

例如:
– 授权用户demo可以在数据库test的所有表中执行所有的操作
grant all on test.* to ‘demo‘@’localhost’;

– 授权用户demo只能在数据库test的所有表中执行查询操作
grant select on test.* to ‘demo‘@’localhost’;

查看用户权限:
show grants for ‘user_name‘@’host’;

例如:
show grants for ‘demo‘@’localhost’;

用户必须先创建,才能执行授权操作;如果不存在,执行授权操作的时候报错。

删除用户

drop user ‘user_name‘@’host’;

例如:

drop user ‘demo‘@’localhost’

特别地,在给用户授权和删除用户时,必须指定创建用户时设置的主机信息,否则操作将会失败。

参考资源

https://www.cnblogs.com/klb561/p/10513620.html 使用sysbench对mysql压力测试
https://blog.51cto.com/xjsunjie/1616347 numa架构与性能解析
https://tech.meituan.com/2018/11/22/mysql-pingcap-practice.html 新一代数据库TiDB在美团的实践
https://github.com/Meituan-Dianping/Zebra Zebra是一个基于JDBC API协议上开发出的高可用、高性能的数据库访问层解决方案
https://zhuanlan.zhihu.com/p/71073707 我们听到的TiDB到底是什么?!
https://pingcap.com/zh/
https://docs.pingcap.com/zh/tidb/stable