1. 首页 > 国产数据库教程 > TiDB教程 > 正文

tidb教程FG147-TiDB安全最佳实践

fgedu.net.cn

目录

一、基础概念

1.1 安全定义

安全是指保护系统和数据不受未授权访问、使用、披露、修改或破坏的能力。TiDB的安全涉及多个层面,包括认证、授权、加密、审计等。

1.2 安全威胁

  • 未授权访问:未经授权的用户访问系统或数据。
  • SQL注入:通过构造恶意SQL语句,执行未授权操作。
  • 密码泄露:用户密码被窃取或破解。
  • 数据泄露:敏感数据被未授权获取或披露。
  • 拒绝服务攻击:通过大量请求使系统无法正常服务。
  • 内部威胁:内部人员的恶意操作或误操作。

1.3 安全机制

  • 认证:验证用户身份的过程。
  • 授权:控制用户对资源的访问权限。
  • 加密:保护数据传输和存储的安全性。
  • 审计:记录系统操作,便于追踪和分析。
  • 防火墙:限制网络访问,防止未授权访问。
  • 入侵检测:检测和防范入侵行为。

1.4 安全合规

安全合规是指系统符合相关的安全标准和法规要求,如GDPR、PCI DSS、等保等。

二、规划建议

2.1 认证与授权规划

  • 用户管理:创建和管理用户账户,确保用户身份的真实性。
  • 角色管理:创建和管理角色,简化权限管理。
  • 权限管理:根据最小权限原则,授予用户必要的权限。
  • 密码策略:制定严格的密码策略,确保密码安全。

2.2 加密规划

  • 传输加密:使用SSL/TLS加密数据传输。
  • 存储加密:加密存储敏感数据。
  • 密钥管理:安全管理加密密钥。

2.3 审计规划

  • 审计日志:配置审计日志,记录系统操作。
  • 日志存储:安全存储审计日志,防止篡改。
  • 日志分析:定期分析审计日志,发现安全问题。

2.4 网络安全规划

  • 网络隔离:隔离业务网络和管理网络。
  • 防火墙:配置防火墙,限制网络访问。
  • 入侵检测:部署入侵检测系统,检测和防范入侵行为。
  • VPN:使用VPN进行远程访问,确保网络安全。

三、实施方案

3.1 认证与授权

用户管理

-- 创建用户
CREATE USER 'app'@'192.168.1.%' IDENTIFIED BY 'App@123456';
CREATE USER 'readonly'@'192.168.1.%' IDENTIFIED BY 'Readonly@123456';

-- 查看用户
SELECT user, host FROM mysql.user;

-- 删除用户
DROP USER 'app'@'192.168.1.%';
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| app              | 192.168.1.% |
| readonly         | 192.168.1.% |风哥提示:
| root             | %         |
+------------------+-----------+

角色管理

-- 创建角色
CREATE ROLE 'app_role';
CREATE ROLE 'readonly_role';

-- 授予权限
GRANT SELECT, INSERT, UPDATE, DELETE ON fgedudb.* TO 'app_role';
GRANT SELECT ON fgedudb.* TO 'readonly_role';

-- 分配角色
GRANT 'app_role' TO 'app'@'192.168.1.%';
GRANT 'readonly_role' TO 'readonly'@'192.168.1.%';

-- 查看角色
SELECT * FROM mysql.role_edges;
+-----------+-----------+---------+---------------+-------------------+
| FROM_HOST | FROM_USER | TO_HOST | TO_USER       | WITH_ADMIN_OPTION |
+-----------+-----------+---------+---------------+-------------------+
| %         | app_role  | %       | app@192.168.1.% | N                 |
| %         | readonly_role | %     | readonly@192.168.1.% | N                 |
+-----------+-----------+---------+---------------+-------------------+

权限管理

-- 查看权限
SHOW GRANTS FOR 'app'@'192.168.1.%';
SHOW GRANTS FOR 'readonly'@'192.168.1.%';

-- 收回权限
REVOKE DELETE ON fgedudb.* FROM 'app_role';

-- 刷新权限
FLUSH PRIVILEGES;
-- app用户权限
+---------------------------------------------------------------------+
| Grants for app@192.168.1.%                                          |
+---------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `app`@`192.168.1.%`                           |
| GRANT `app_role`@`%` TO `app`@`192.168.1.%`                        |
+---------------------------------------------------------------------+

-- readonly用户权限
+---------------------------------------------------------------------------+
| Grants for readonly@192.168.1.%                                           |
+---------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `readonly`@`192.168.1.%`                            |
| GRANT `readonly_role`@`%` TO `readonly`@`192.168.1.%`                     |
+---------------------------------------------------------------------------+

密码策略

-- 查看密码策略
SHOW VARIABLES LIKE 'validate_password%';

-- 修改密码策略
SET GLOBAL validate_password_length = 8;
SET GLOBAL validate_password_mixed_case_count = 1;
SET GLOBAL validate_password_number_count = 1;
SET GLOBAL validate_password_special_char_count = 1;

-- 修改密码
ALTER USER 'app'@'192.168.1.%' IDENTIFIED BY 'NewApp@123456';
+--------------------------------------+-------+
| Variable_name                        | Value |
+--------------------------------------+-------+
| validate_password_check_user_name    | OFF   |
| validate_password_dictionary_file    |       |
| validate_password_length             | 8     |
| validate_password_mixed_case_count   | 1     |
| validate_password_number_count       | 1     |
| validate_password_policy             | MEDIUM|
| validate_password_special_char_count | 1     |
+--------------------------------------+-------+

3.2 加密

传输加密

# 生成SSL证书
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server-key.pem -out server-cert.pem

# 编辑TiDB配置文件
tiup cluster edit-config tidb-cluster

# 在TiDB配置中添加以下内容
# tidb:学习交流加群风哥QQ113257174
#   config:
#     security:
#       ssl-ca: /tidb/ssl/server-cert.pem
#       ssl-cert: /tidb/ssl/server-cert.pem
#       ssl-key: /tidb/ssl/server-key.pem

# 重载配置
tiup cluster reload tidb-cluster -R tidb

# 测试SSL连接
mysql -u root -p -h 192.168.1.13 -P 4000 --ssl-mode=REQUIRED
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.7.25-TiDB-v7.5.0 TiDB Server (Apache License 2.0) Community Edition, MySQL 5.7 compatible

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> \s
--------------
mysql  Ver 8.0.33 for Linux on x86_64 (MySQL Community Server - GPL)

Connection id:          1
Current database:       
Current user:           root@192.168.1.1
SSL:                    Cipher in use is TLS_AES_256_GCM_SHA384
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.7.25-TiDB-v7.5.0 TiDB Server (Apache License 2.0) Community Edition, MySQL 5.7 compatible
Protocol version:       10
Connection:             192.168.1.13 via TCP/IP
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    utf8mb4
Conn.  characterset:    utf8mb4
TCP port:               4000
Binary data as:         Hexadecimal
--------------

存储加密

# 编辑TiKV配置文件
tiup cluster edit-config tidb-cluster

# 在TiKV配置中添加以下内容
# tikv:
#   config:
#     security:
#       data-encryption-method: "aes-256-ctr"
#       data-encryption-key: "your-encryption-key"

# 重载配置
tiup cluster reload tidb-cluster -R tikv

3.3 审计

审计日志配置

# 编辑TiDB配置文件
tiup cluster edit-config tidb-cluster

# 在TiDB配置中添加以下内容
# tidb:
#   config:
#     security:
#       audit-log:
#         enable: true
#         format: "json"
#         log-file: "/tidb/log/tidb-audit.log"
#         log-rotate: true
#         max-size: "100MB"
#         max-days: 7

# 重载配置
tiup cluster reload tidb-cluster -R tidb

# 查看审计日志
cat /tidb/log/tidb-audit.log
{"time":"2024-04-09T10:00:00+08:00","type":"general","event":"query","sql":"SELECT * FROM fgedu_users","user":"app","host":"192.168.1.1","ip":"192.168.1.1","port":54321,"status":0,"duration":10,"session_id":"1234567890"}
{"time":"2024-04-09T10:00:01+08:00","type":"general","event":"query","sql":"INSERT INTO fgedu_users (id, username, email, age, created_at) VALUES (13, 'user13', 'user13@example.com', 35, NOW())","user":"app","host":"192.168.1.1","ip":"192.168.1.1","port":54321,"status":0,"duration":15,"session_id":"1234567890"}

3.4 网络安全

防火墙配置

# 配置防火墙
# 允许TiDB端口
ufw allow 4000/tcp
ufw allow 10080/tcp

# 允许TiKV端口
ufw allow 20160/tcp
ufw allow 20180/tcp

# 允许PD端口
ufw allow 2379/tcp
ufw allow 2380/tcp

# 查看防火墙规则
ufw status
Status: active

To                         Action      From
--                         ------      ----
4000/tcp                   ALLOW       Anywhere
10080/tcp                  ALLOW       Anywhere
20160/tcp                  ALLOW       Anywhere
20180/tcp                  ALLOW       Anywhere
2379/tcp                   ALLOW       Anywhere
2380/tcp                   ALLOW       Anywhere
4000/tcp (v6)              ALLOW       Anywhere (v6)
10080/tcp (v6)             ALLOW       Anywhere (v6)
20160/tcp (v6)             ALLOW       Anywhere (v6)
20180/tcp (v6)             ALLOW       Anywhere (v6)
2379/tcp (v6)              ALLOW       Anywhere (v6)
2380/tcp (v6)              ALLOW       Anywhere (v6)

网络隔离

# 创建VLAN
# 业务网络:VLAN 100
# 管理网络:VLAN 200

# 配置网络接口
# /etc/network/interfaces
auto eth0
iface eth0 inet static
    address 192.168.1.10
    netmask 255.255.255.0
    gateway 192.168.1.1

auto eth0.100
iface eth0.100 inet static
    address 192.168.100.10
    netmask 255.255.255.0

auto eth0.200
iface eth0.200 inet static
    address 192.168.200.10
    netmask 255.255.255.0

四、实战案例

4.1 企业级安全加固

场景:企业需要对TiDB集群进行安全加固,提高系统的安全性。

步骤1:用户与权限管理

-- 创建管理员用户
CREATE USER 'admin'@'192.168.1.%' IDENTIFIED BY 'Admin@123456';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'192.168.1.%' WITH GRANT OPTION;

-- 创建应用用户
CREATE USER 'app'@'192.168.1.%' IDENTIFIED BY 'App@123456';
GRANT SELECT, INSERT, UPDATE, DELETE ON fgedudb.* TO 'app'@'192.168.1.%';

-- 创建只读用户
CREATE USER 'readonly'@'192.168.1.%' IDENTIFIED BY 'Readonly@123456';
GRANT SELECT ON fgedudb.* TO 'readonly'@'192.168.1.%';

-- 删除默认用户
DROP USER 'root'@'%';

步骤2:传输加密

# 生成SSL证书
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server-key.pem -out server-cert.pem

# 编辑TiDB配置文件
tiup cluster edit-config tidb-cluster

# 在TiDB配置中添加以下内容
# tidb:
#   config:
#     security:
#       ssl-ca: /tidb/ssl/server-cert.pem
#       ssl-cert: /tidb/ssl/server-cert.pem
#       ssl-key: /tidb/ssl/server-key.pem

# 重载配置
tiup cluster reload tidb-cluster -R tidb

步骤3:审计配置

# 编辑TiDB配置文件
tiup cluster edit-config tidb-cluster

# 在TiDB配置中添加以下内容
# tidb:
#   config:
#     security:
#       audit-log:
#         enable: true
#         format: "json"
#         log-file: "/tidb/log/tidb-audit.log"
#         log-rotate: true
#         max-size: "100MB"
#         max-days: 7

# 重载配置
tiup cluster reload tidb-cluster -R tidb

步骤4:网络安全

# 配置防火墙
ufw allow 4000/tcp
ufw allow 10080/tcp
ufw allow 20160/tcp
ufw allow 20180/tcp
ufw allow 2379/tcp
ufw allow 2380/tcp

# 配置网络隔离
# 业务网络:192.168.1.0/24
# 管理网络:192.168.2.0/24

步骤5:安全验证

# 测试SSL连接
mysql -u admin -p -h 192.168.1.13 -P 4000 --ssl-mode=REQUIRED

# 测试权限
mysql -u app -p -h 192.168.1.13 -P 4000
USE fgedudb;
SELECT * FROM fgedu_users;
INSERT INTO fgedu_users (id, username, email, age, created_at) VALUES (14, 'user14', 'user14@example.com', 36, NOW());
DELETE FROM fgedu_users WHERE id = 14;

# 测试只读用户
mysql -u readonly -p -h 192.168.1.13 -P 4000
USE fgedudb;
SELECT * FROM fgedu_users;
INSERT INTO fgedu_users (id, username, email, age, created_at) VALUES (15, 'user15', 'user15@example.com', 37, NOW());

# 查看审计日志
cat /tidb/log/tidb-audit.log
# 测试SSL连接
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25-TiDB-v7.5.0 TiDB Server (Apache License 2.0) Community Edition, MySQL 5.7 compatible

# 测试app用户权限
mysql> SELECT * FROM fgedu_users;
+----+----------+---------------------+-----+---------------------+
| id | username | email               | age | created_at          |
+----+----------+---------------------+-----+---------------------+
|  1 | admin    | admin@example.com   |  34 | 2024-04-09 10:00:00 |
|  2 | user1    | user1@example.com   |  25 | 2024-04-09 10:00:00 |
|  3 | user2    | user2@example.com   |  26 | 2024-04-09 10:00:00 |
+----+----------+---------------------+-----+---------------------+
3 rows in set (0.01 sec)

mysql> INSERT INTO fgedu_users (id, username, email, age, created_at) VALUES (14, 'user14', 'user14@example.com', 36, NOW());
Query OK, 1 row affected (0.01 sec)

mysql> DELETE FROM fgedu_users WHERE id = 14;
Query OK, 1 row affected (0.01 sec)

# 测试readonly用户权限
mysql> SELECT * FROM fgedu_users;
+----+----------+---------------------+-----+---------------------+
| id | username | email               | age | created_at          |
+----+----------+---------------------+-----+---------------------+
|  1 | admin    | admin@example.com   |  34 | 2024-04-09 10:00:00 |
|  2 | user1    | user1@example.com   |  25 | 2024-04-09 10:00:00 |
|  3 | user2    | user2@example.com   |  26 | 2024-04-09 10:00:00 |
+----+----------+---------------------+-----+---------------------+
3 rows in set (0.01 sec)

mysql> INSERT INTO fgedu_users (id, username, email, age, created_at) VALUES (15, 'user15', 'user15@example.com', 37, NOW());
ERROR 1142 (42000): INSERT command denied to user 'readonly'@'192.168.1.1' for table 'fgedu_users'

# 查看审计日志
{"time":"2024-04-09T10:00:00+08:00","type":"general","event":"query","sql":"SELECT * FROM fgedu_users","user":"app","host":"192.168.1.1","ip":"192.168.1.1","port":54321,"status":0,"duration":10,"session_id":"1234567890"}
{"time":"2024-04-09T10:00:01+08:00","type":"general","event":"query","sql":"INSERT INTO fgedu_users (id, username, email, age, created_at) VALUES (14, 'user14', 'user14@example.com', 36, NOW())","user":"app","host":"192.168.1.1","ip":"192.168.1.1","port":54321,"status":0,"duration":15,"session_id":"1234567890"}
{"time":"2024-04-09T10:00:02+08:00","type":"general","event":"query","sql":"DELETE FROM fgedu_users WHERE id = 14","user":"app","host":"192.168.1.1","ip":"192.168.1.1","port":54321,"status":0,"duration":12,"session_id":"1234567890"}
{"time":"2024-04-09T10:00:03+08:00","type":"general","event":"query","sql":"SELECT * FROM fgedu_users","user":"readonly","host":"192.168.1.1","ip":"192.168.1.1","port":54322,"status":0,"duration":9,"session_id":"0987654321"}
{"time":"2024-04-09T10:00:04+08:00","type":"general","event":"query","sql":"INSERT INTO fgedu_users (id, username, email, age, created_at) VALUES (15, 'user15', 'user15@example.com', 37, NOW())","user":"readonly","host":"192.168.1.1","ip":"192.168.1.1","port":54322,"status":1142,"duration":5,"session_id":"0987654321"}

五、经验总结

5.1 安全最佳实践

  • 最小权限原则:只授予用户必要的权限,避免权限过大。
  • 强密码策略:使用复杂的密码,定期更换密码。
  • 传输加密:使用SSL/TLS加密数据传输。
  • 存储加密:加密存储敏感数据。
  • 审计日志:配置审计日志,记录系统操作。
  • 网络安全:配置防火墙,隔离网络。
  • 定期安全审计:定期进行安全审计,发现和解决安全问题。
  • 安全培训:对用户和管理员进行安全培训,提高安全意识。

5.2 常见安全问题与解决方案

问题 原因 解决方案
未授权访问 用户密码泄露,权限配置不当 使用强密码,配置合理的权限
SQL注入 使用字符串拼接SQL语句 使用参数化查询,防止SQL注入
数据泄露 传输未加密,存储未加密 使用SSL/TLS加密传输,加密存储敏感数据
拒绝服务攻击 系统未配置防护措施 配置防火墙,限制连接数
内部威胁 内部人员的恶意操作或误操作 实施最小权限原则,配置审计日志

5.3 安全检查清单

检查项 配置要求 状态
用户管理 创建和管理用户账户,确保用户身份的真实性
权限管理 根据最小权限原则,授予用户必要的权限
密码策略 使用复杂的密码,定期更换密码
传输加密 使用SSL/TLS加密数据传输
存储加密 加密存储敏感数据
审计日志 配置审计日志,记录系统操作
网络安全 配置防火墙,隔离网络
定期安全审计 定期进行安全审计,发现和解决安全问题

更多视频教程www.fgedu.net.cn

© 2024 TiDB数据库培训文档

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息