配置socket属性后,创建 root@localhost用户并且drop user root@%,远程依然可以通过root登录

为提高效率,请提供以下信息,问题描述清晰能够更快得到解决:

【TiDB 版本】
v4.0.9

【问题描述】

集群部署启动后,在tidb-server节点上, shell脚本中执行初始化,例如:

#!/bin/sh

TIDB_PORT=4000
ROOT_PASSWORD='xxx'

mysql -uroot -P $TIDB_PORT -v -e "set password for root@'%' = '$ROOT_PASSWORD' ;CREATE USER IF NOT EXISTS root@'localhost' identified by '$ROOT_PASSWORD' ;GRANT ALL PRIVILEGES ON *.* TO root@'localhost' WITH GRANT OPTION; FLUSH PRIVILEGES;"

sleep 1
mysql -uroot -P $TIDB_PORT -p"$ROOT_PASSWORD" -v -e " drop user  'root'@'%';FLUSH PRIVILEGES "

期待结果:

执行完成后,在其他非集群节点用root用户登录tidb,会失败。

实际结果:
可以登录成功。
mysql> select user();
±---------------+
| user() |
±---------------+
| root@localhost |
±---------------+

mysql> select host,user from mysql.user where user=‘root’;
±----------±-----+
| host | user |
±----------±-----+
| localhost | root |
±----------±-----+
1 row in set (0.00 sec)


若提问为性能优化、故障排查类问题,请下载脚本运行。终端输出的打印结果,请务必全选并复制粘贴上传。

root@’%’ 个 root@‘localhost’ 是两个账号。他们之间没有包含关系。
MySQL 和 PostgreSQL 也是这样设计的。

是两个账号呀,我把root@’%’ 这个用户drop了之后,仅保留root@localhost。理论上应该就是只能通过unix socket连上tidb才对,也就是部署了tidb-server的节点。 那么没有部署tidb-server的节点,就不应该能通过root访问了。但是现在的结果是可以。

从上面提供的登录账号和你的描述有出入。上面登录的账号是 root@localhost,建议你这边再确认下登录机器是否正确。

嗯,我特地截图出来的,这个地方的输出我个人觉得也是个bug。

下面是我在另外一台机器10.18.216.84(未部署任何tidb服务)上的登录tidb 10.18.210.160:4000操作输出。

[root@admy-B ~]# mysql -uroot  -h 10.18.210.160 -P 4000 -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 85
Server version: 5.7.25-TiDB-v4.0.9 TiDB Server (Apache License 2.0) Community Edition, MySQL 5.7 compatible

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql> exit
Bye


[root@admy-B ~]# ps -aux|grep tidb-4000
root     403628  0.0  0.0  10636   952 pts/0    S+   02:44   0:00 grep --color=auto tidb-4000

[root@admy-B ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN
    link/ipip 0.0.0.0 brd 0.0.0.0
46: eth0@if47: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 2a:c4:4a:71:77:12 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 10.18.216.84/24 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::28c4:4aff:fe71:7712/64 scope link
       valid_lft forever preferred_lft forever

追加一下在这个机器上执行select mysql.user执行结果。

mysql> select * from mysql.user;
+-------------+-------------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+--------------+------------+-----------------+------------+--------------+------------+-----------------------+------------------+--------------+------------------+----------------+---------------------+--------------------+------------+------------------+------------+--------------+------------------+----------------+----------------+---------------+-------------+-----------+-------------+
| Host        | User        | authentication_string                     | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Process_priv | Grant_priv | References_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Index_priv | Create_user_priv | Event_priv | Trigger_priv | Create_role_priv | Drop_role_priv | Account_locked | Shutdown_priv | Reload_priv | FILE_priv | Config_priv |
+-------------+-------------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+--------------+------------+-----------------+------------+--------------+------------+-----------------------+------------------+--------------+------------------+----------------+---------------------+--------------------+------------+------------------+------------+--------------+------------------+----------------+----------------+---------------+-------------+-----------+-------------+
| localhost   | root        | *B12289EEF8752AD620294A64A37CD586223AB454 | Y           | Y           | Y           | Y           | Y           | Y         | Y            | Y          | Y               | Y          | Y            | Y          | Y                     | Y              | Y            | Y                | Y              | Y                   | Y                  | Y          | Y                | Y          | Y            | Y                | Y              | N              | Y             | Y           | Y         | Y       |
| 10.18.210.% | hitv        | *B12289EEF8752AD620294A64A37CD586223AB454 | Y           | Y           | Y           | Y           | Y           | Y         | Y            | N          | Y               | Y          | Y            | N          | Y                     | Y              | Y            | Y                | Y              | N                   | N                  | Y          | N                | Y          | Y            | N                | N              | N              | N             | Y           | N         | N       |
| 10.18.215.% | hitv        | *B12289EEF8752AD620294A64A37CD586223AB454 | Y           | Y           | Y           | Y           | Y           | Y         | Y            | N          | Y               | Y          | Y            | N          | Y                     | Y              | Y            | Y                | Y              | N                   | N                  | Y          | N                | Y          | Y            | N                | N              | N              | N             | Y           | N         | N       |
| localhost   | hitv        | *B12289EEF8752AD620294A64A37CD586223AB454 | Y           | Y           | Y           | Y           | Y           | Y         | Y            | N          | Y               | Y          | Y            | N          | Y                     | Y              | Y            | Y                | Y              | N                   | N                  | Y          | N                | Y          | Y            | N                | N              | N              | N             | Y           | N         | N       |
| 10.18.210.% | backupadmin | *B12289EEF8752AD620294A64A37CD586223AB454 | Y           | N           | N           | N           | N           | N         | N            | N          | N               | N          | N            | Y          | N                     | Y              | N            | N                | N              | N                   | N                  | N          | N                | N          | N            | N                | N              | N              | N             | Y           | N         | N       |
| 10.18.215.% | backupadmin | *B12289EEF8752AD620294A64A37CD586223AB454 | Y           | N           | N           | N           | N           | N         | N            | N          | N               | N          | N            | Y          | N                     | Y              | N            | N                | N              | N                   | N                  | N          | N                | N          | N            | N                | N              | N              | N             | Y           | N         | N       |
| localhost   | backupadmin | *B12289EEF8752AD620294A64A37CD586223AB454 | Y           | N           | N           | N           | N           | N         | N            | N          | N               | N          | N            | Y          | N                     | Y              | N            | N                | N              | N                   | N                  | N          | N                | N          | N            | N                | N              | N              | N             | Y           | N         | N       |
+-------------+-------------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+--------------+------------+-----------------+------------+--------------+------------+-----------------------+------------------+--------------+------------------+----------------+---------------------+--------------------+------------+------------------+------------+--------------+------------------+----------------+----------------+---------------+-------------+-----------+-------------+
7 rows in set (0.00 sec)

补充下机器部署的拓扑信息

   global:
  user: "tidb"
  ssh_port: 22
  deploy_dir: "/usr/local/fountain/3rdparty/tidb/deploy"
  data_dir: "/usr/local/fountain/3rdparty/tidb/data"
server_configs:
  pd:
    replication.enable-placement-rules: true
  tidb:
    socket: "/tmp/mysql.sock"
    log.slow-threshold: 300
pd_servers:
  - host: 10.18.210.160
tidb_servers:
  - host: 10.18.210.160
    port: 4000
tikv_servers:
  - host: 10.18.210.160
grafana_servers:
  - host: 10.18.210.160
monitoring_servers:
  - host: 10.18.210.160
monitored:
  node_exporter_port: 9100
  blackbox_exporter_port: 9115

根据你那边提供的信息,登录后 select user() 查看的是 root@localhost 用户,因此删除 root@% 用户不会影响登录。理论上 登录后 select user() 查看到的用户是 root@xx.xx.xx.xx 具体 IP。怀疑是你这边配置了环境变量或者是有别名,导致登录默认的是 localhost 方式。

建议你这边检查下环境变量吧。同版本我这边没有问题。

分别对比了下机器登录mysql和tidb的抓包数据,没有看到协议数据上有区别会导致上报的ip数据异常。所以排除了远程登录机器的环境变量设置。

注意到,配置了socket时,在tidb 启动日志中可以看到

[2021/03/12 09:26:19.800 +00:00] [INFO] [server.go:241] [“server is running MySQL protocol”] [addr=0.0.0.0:4000]
[2021/03/12 09:26:19.801 +00:00] [INFO] [server.go:244] [“server redirecting”] [from=/tmp/tidb.sock] [to=0.0.0.0:4000]

建联时,tidb日志输出:

[2021/03/12 09:39:30.564 +00:00] [DEBUG] [server.go:412] [“new connection”] [conn=1] [remoteAddr=10.18.216.84:59471]

怀疑是跟这个有关系。

后来实际验证了下,应该是

tidb:
socket: “/tmp/mysql.sock”

该配置项导致的。配置了该属性后,使用任何用户从任意机器登录,select user返回结果均是 xx@localhost。去掉该属性后再次启动tidb-server,行为就正常了。

配置该属性本意是为了跟以往的登录行为一致,我个人觉得这个应该是个比较严重的bug。因为客户在配置了该属性之后且配置了localhost用户权限,实际却会导致访问权限全部放开。

好的,我们内部测试下,你这边就先不要配置 socket 先饶过吧。