【 SOP 系列 42 】Tiup 常用的操作命令

感谢:@清风明月 的贡献

TiUP 是 TiDB 4.0 版本引入的集群运维工具,TiUP cluster 是 TiUP 提供的使用 Golang 编写的集群管理组件,通过 TiUP cluster 组件就可以进行日常的运维工作,包括部署、启动、关闭、销毁、弹性扩缩容、升级 TiDB 集群,以及管理 TiDB 集群参数。

目前 TiUP 可以支持部署 TiDB、TiFlash、TiDB Binlog、TiCDC 以及监控系统等相关工具和组件的部署。

1.tiup安装

1.安装
curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
2.声明全局变量
cat ~/.bash_profile
source ~/.bash_profile
​
3.查看tiup版本
tiup --binary cluster

2.tiup安装TiDB

1.编辑配置文件
tiup cluster template > top.yaml
2.检查配置是否ok
tiup cluster check ./top.yaml --apply --user root -p
3.部署tidb集群
tiup cluster deploy tidb v6.1.0 ./top.yaml --user root -p
4.查看tiup管理集群情况
 tiup cluster list
5.查看tidb集群状态
 tiup cluster display tidb-test
6.启动机器
tiup cluster start tidb-test
7.链接tidb的数据库集群
mysql -uroot -p -h 172.16.5.146 -P4001
​
8.查看tikv版本组件列表
tiup list tikv
9.查看已经安装的所有组件
tiup list --installed
​
​

3.tiup扩缩容

1、tiup扩容
 tiup cluster  scale-out tidb-test  cdc.yaml -uroot -p
​
2、缩容
tiup cluster scale-in tidb-test -N 172.16.4.122:8300 --force
​
3、清理节点信息
 tiup cluster prune tidb-test
​
4、重命名
tiup cluster rename tidb-test tidb-prod
​
5、tidb 集群升级
tiup cluster upgrade tidb-test v6.2.0
​
6、tidb集群降级
​
​
10.cdc查看任务
tiup ctl:v6.1.0 cdc changefeed list --pd=http://172.16.5.146:2399
11.cdc 删除任务
tiup ctl:v6.1.0 cdc changefeed remove --pd=http://172.16.5.146:2399 -c kafka-2
​
12、创建任务
第一步创建配置文件
cat kafka.conf
[sink]
dispatchers = {matcher = ['*.*'], topic = "tidb_{schema}_{table}", partition="index-value"},
{matcher = ['yz0920.*'], topic = "tidb_{schema}_{table}", partition="index-value"},
第二步通过tiup创建命令
tiup ctl:v6.1.0 cdc changefeed create --pd="http://172.16.5.146:2399" --sink-uri="kafka://172.16.6.132:9092/tidb-kafka-3?protocol=avro&partition-num=1&max-message-bytes=67108864&replication-factor=1" --changefeed-id="kafka-2" --config="/home/kafka/kafka.conf" --schema-registry="http://172.16.6.132:8081"

4.TiDB数据库的基本操作

1、进入数据库
mysql -uroot -p -h 172.16.5.146 -P4001
2、查看数据库的版本
select tidb_version();
3、查看数据库列表
show databases;
4、创建数据库
create database tidb;
5、进入数据库
use tidb;
6、查看数据库的表
show tables;
​
7、创建表
create table `tidb_tab` 
(`id` int(11) not null auto_increment, 
`name` varchar(20) not null default '', 
`age` int(11) not null default 0, 
`version` varchar(20) not null default '', 
primary key (`id`), key `idx_age` (`age`));
​
8、表中插入数据
insert into `tidb_tab` values (1,'tidb',5,'v5.0');
9、查看数据
select * from tidb_tab;
10、查看数据库连接状态:
##创建测试表t1
 CREATE TABLE t1 (a int not null primary key auto_increment);
 
show processlist;
+---------------------+------+--------------------+------+---------+------+------------+------------------+
| Id                  | User | Host               | db   | Command | Time | State      | Info             |
+---------------------+------+--------------------+------+---------+------+------------+------------------+
| 3006706905130266271 | root | 172.16.5.146:35600 | tidb | Query   |    0 | autocommit | show processlist |
+---------------------+------+--------------------+------+---------+------+------------+------------------+
1 row in set (0.00 sec)
​
11、查看系统参数auto_increment_increment
show session variables like 'auto_increment_increment';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
+--------------------------+-------+
1 row in set (0.00 sec)
​
12、设置auto_increment_increment=10,插入数据
set auto_increment_increment=10;
​
MySQL [tidb]> insert into t1 values ();
Query OK, 1 row affected (0.00 sec)
​
MySQL [tidb]> select * from t1;
+----+
| a  |
+----+
|  1 |
|  2 |
| 11 |
+----+
3 rows in set (0.00 sec)
​
MySQL [tidb]> show session variables like 'auto_increment_increment';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 10    |
+--------------------------+-------+
1 row in set (0.00 sec)
​
session级别的修改参数只影响当前会话,对于其它会话或本地会话修改的参数是无效的,只对当前有效。
​
13、在global进行修改
set global auto_increment_increment=10;
当前的会话还是1,重启一个客户端auto_increment_increment=10
重启后,发现还是10
​

5.TiDB用户的管理与安全

1. 创建用户
create user 'lqb'@'%' identified by '123456';
2.创建角色
create role r_manager,r_staff;
3.查看用户表情况
MySQL [tidb]> select user,host,authentication_string from mysql.user;
+-----------+------+-------------------------------------------+
| user      | host | authentication_string                     |
+-----------+------+-------------------------------------------+
| root      | %    | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| lqb       | %    | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| r_manager | %    |                                           |
| r_staff   | %    |                                           |
+-----------+------+-------------------------------------------+
4 rows in set (0.00 sec)
​
###用户和角色都被存储在mysql.user表中。角色是没有密码的。
​
4.授权权限:
 GRANT ALL ON *.* TO 'lqb'@'%';
4.查看角色详情
​
MySQL [tidb]> select * from mysql.user where user='r_staff'\G;
*************************** 1. row ***************************
                  Host: %
                  User: r_staff
 authentication_string:
                plugin: mysql_native_password
           Select_priv: N
           Insert_priv: N
           Update_priv: N
           Delete_priv: N
           Create_priv: N
             Drop_priv: N
          Process_priv: N
            Grant_priv: N
       References_priv: N
            Alter_priv: N
          Show_db_priv: N
            Super_priv: N
 Create_tmp_table_priv: N
      Lock_tables_priv: N
          Execute_priv: N
      Create_view_priv: N
        Show_view_priv: N
   Create_routine_priv: N
    Alter_routine_priv: N
            Index_priv: N
      Create_user_priv: N
            Event_priv: N
       Repl_slave_priv: N
      Repl_client_priv: N
          Trigger_priv: N
      Create_role_priv: N
        Drop_role_priv: N
        Account_locked: Y
         Shutdown_priv: N
           Reload_priv: N
             FILE_priv: N
           Config_priv: N
Create_Tablespace_Priv: N
1 row in set (0.01 sec)
​
ERROR: No query specified
​
###
角色是被锁的:Account_locked: Y
角色是没有密码: authentication_string: 为null
​
5.修改用户密码
 alter user  'lqb'@'%' identified by 'tidb';
 
6.删除用户和角色
drop role r_staff;
drop user lqb;
​
7.创建角色并授权
create role r_mgr,r_emp;
grant select on tidb.* to r_emp;
grant insert,update,delete on tidb.* to r_mgr;
##授权r_emp角色的权限给角色r_mgr和用户lqb;
grant r_emp to r_mgr, 'lqb'@'%';
​
开启角色并查看角色:
 set role all;
 MySQL [(none)]> select current_role();
+-------------------------+
| current_role()          |
+-------------------------+
| `r_emp`@`%`,`r_mgr`@`%` |
+-------------------------+
​
查看授权
​
MySQL [(none)]> show grants;
+----------------------------------------------------------+
| Grants for User                                          |
+----------------------------------------------------------+
| GRANT USAGE ON *.* TO 'lqb'@'%'                          |
| GRANT SELECT,INSERT,UPDATE,DELETE ON tidb.* TO 'lqb'@'%' |
| GRANT 'r_emp'@'%', 'r_mgr'@'%' TO 'lqb'@'%'              |
2 个赞

集群支持降级不?好像不行?

以后继续分享

3.1.0就使用tiup的路过