MySQL数据脱敏(Data masking plugin functions)

2023-11-05 08:20

本文主要是介绍MySQL数据脱敏(Data masking plugin functions),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

对于企业而言,数据脱敏可以在数据共享或测试时用于保护敏感数据(如信用卡,社保卡,地址等)。通过对敏感数据进行脱敏处理,组织可以最大限度地降低数据泄露和未经授权访问的风险,同时仍能够使用真实的开发,测试和分析目的所需的数据。

file

有很多方法进行数据脱敏,比如遮挡,替换,洗牌和加密,等等,它们适用于不同场景。本文主要聚焦「遮挡」,用特定符号 (比如 X 或) 遮挡敏感数据,这种方法可以在脱敏的同时保持原有数据感观。

MySQL 企业级数据脱敏插件

MySQL 官方这边,数据脱敏只作为插件在 MySQL 企业版中提供 。 MySQL 数据脱敏插件的工作原理是插件中包含了用于进行数据脱敏的语法,例如 mask_innermask_outermask_ssn 等。

file

组织里有权限的人员(通常来说是数据库管理员)会首先定义一个显示脱敏数据的视图 (VIEW)。即使用户对敏感数据的访问受限,他们也可以将该视图视为一张表。因此,要访问数据,用户不是直接使用脱敏语法进行直接查询,而是从视图中查询即可。

这种方法很直接,但也有一定限制:

  • 依赖于细粒度的 MySQL 用户账户 / 角色。实际上,大多数 MySQL 实例只有少数几个用户。要采用此插件,需要重新设计 MySQL 中的账户设置。
  • 不同的脱敏规则需要定义不同的视图。随着底层表和变体数量增加,这会越来越难管理。
  • 没有专门的模块来管理脱敏(毕竟只是普通的 MySQL VIEW)。

Percona 数据脱敏插件

Percona是前述 MySQL 插件的免费开源实现。它也提供了一组用于脱敏数据的函数。

file

同样,保护原始数据的方法是使用视图 (VIEW)。 然而,Percona 数据脱敏仅适用于 Percona Server for MySQL。如果你使用更主流的 MySQL,那就需要另寻他路了。

General purpose¶通用脱敏

The general purpose data masking functions are the following:

.

FunctionDescription
mask_inner(string, margin1, margin2 [, character])Returns a result where only the inner part of a string is masked. A different masking character can be specified.
mask_outer(string, margin1, margin2 [, character])Masks the outer part of the string. The inner section is not masked. A different masking character can be specified.

Examples¶

An example of mask_inner:

mysql> SELECT mask_inner('123456789', 1, 2);

Expected output

+-----------------------------------+
| mask_inner('123456789', 1, 2)     |
+-----------------------------------+
|1XXXXXX89                          |
+-----------------------------------+

An example of mask_outer:

mysql> SELECT mask_outer('123456789', 2, 2); 

Expected output

+------------------------------------+
| mask_outer('123456789', 2, 2).     |
+------------------------------------+
| XX34567XX                          |
+------------------------------------+

Special Purpose¶特殊脱敏

The special purpose data masking functions are as follows:

ParameterDescription
mask_pan(string)Masks the Primary Account Number (PAN) by replacing the string with an “X” except for the last four characters. The PAN string must be 15 characters or 16 characters in length.
mask_pan_relaxed(string)Returns the first six numbers and the last four numbers. The rest of the string is replaced by “X”.
mask_ssn(string)Returns a string with only the last four numbers visible. The rest of the string is replaced by “X”.

Examples¶

An example of mask_pan.

mysql> SELECT mask_pan (gen_rnd_pan());

Expected output

+------------------------------------+
| mask_pan(gen_rnd_pan())            |
+------------------------------------+
| XXXXXXXXXXX2345                    |
+------------------------------------+

An example of mask_pan_relaxed:

mysql> SELECT mask_pan_relaxed(gen_rnd_pan());

Expected output

+------------------------------------------+
| mask_pan_relaxed(gen_rnd_pan())          |
+------------------------------------------+
| 520754XXXXXX4848                         |
+------------------------------------------+

An example of mask_ssn:

mysql> SELECT mask_ssn('555-55-5555');

Expected output

+-------------------------+
| mask_ssn('555-55-5555') |
+-------------------------+
| XXX-XX-5555             |
+-------------------------+

Generate random data for specific requirements¶随机脱敏

These functions generate random values for specific requirements.

ParameterDescription
gen_range(lower, upper)Generates a random number based on a selected range and supports negative numbers.
gen_rnd_email()Generates a random email address. The domain is example.com.
gen_rnd_pan([size in integer])Generates a random primary account number. This function should only be used for test purposes.
gen_rnd_us_phone()Generates a random U.S. phone number. The generated number adds the 1 dialing code and is in the 555 area code. The 555 area code is not valid for any U.S. phone number.
gen_rnd_ssn()Generates a random, non-legitimate US Social Security Number in an AAA-BBB-CCCC format. This function should only be used for test purposes.

Examples¶

An example of gen_range(lower, upper):

mysql> SELECT gen_range(10, 100);

Expected output

+--------------------------------------+
| gen_range(10,100)                    |
+--------------------------------------+
| 56                                   |
+--------------------------------------+

An example of gen_range(lower, upper) with negative numbers:

mysql> SELECT gen_range(-100,-80);

Expected output

+--------------------------------------+
| gen_range(-100,-80)                  |
+--------------------------------------+
| -91                                  |
+--------------------------------------+

An example of gen_rnd_email():

mysql> SELECT gen_rnd_email();

Expected output

+---------------------------------------+
| gen_rnd_email()                       |
+---------------------------------------+
| sma.jrts@example.com                  |
+---------------------------------------+

An example of mask_pan(gen_rnd_pan()):

mysql> SELECT mask_pan(gen_rnd_pan());

Expected output

+-------------------------------------+
| mask_pan(gen_rnd_pan())             |
+-------------------------------------+
| XXXXXXXXXXXX4444                    |
+-------------------------------------+

An example of gen_rnd_us_phone():

mysql> SELECT gen_rnd_us_phone();

Expected output

+-------------------------------+
| gen_rnd_us_phone()            |
+-------------------------------+
| 1-555-635-5709                |
+-------------------------------+

An example of gen_rnd_ssn():

mysql> SELECT gen_rnd_ssn()

Expected output

+-----------------------------+
| gen_rnd_ssn()               |
+-----------------------------+
| 995-33-5656                 |
+-----------------------------+

Use dictionaries to generate random terms¶字典脱敏

Use a selected dictionary to generate random terms. The dictionary must be loaded from a file with the following characteristics:

  • Plain text

  • One term per line

  • Must contain at least one entry

Copy the dictionary files to a directory accessible to MySQL. Percona Server for MySQL* 8.0.21-12 enabled using the secure-file-priv option for gen_dictionary_load(). The secure-file-priv option defines the directories where gen_dictionary_load() loads the dictionary files.

Note

Percona Server for MySQL 8.0.34 deprecates the gen_blacklist() function. Use gen_blocklist() instead.

ParameterDescriptionReturns
gen_blacklist(str, dictionary_name, replacement_dictionary_name)Replaces a term with a term from a second dictionary. Deprecated in Percona Server for MySQL 8.0.34.A dictionary term
gen_blocklist(str, dictionary_name, replacement_dictionary_name)Replaces a term with a term from a second dictionary.A dictionary term
gen_dictionary(dictionary_name)Randomizes the dictionary termsA random term from the selected dictionary.
gen_dictionary_drop(dictionary_name)Removes the selected dictionary from the dictionary registry.Either success or failure
gen_dictionary_load(dictionary path, dictionary name)Loads a file into the dictionary registry and configures the dictionary name. The name can be used with any function. If the dictionary is edited, you must drop and then reload the dictionary to view the changes.Either success or failure

Example¶

An example of gen_blocklist():

mysql> SELECT gen_blocklist('apple', 'fruit', 'nut');

Expected output

+-----------------------------------------+
| gen_blocklist('apple', 'fruit', 'nut')  |
+-----------------------------------------+
| walnut                                  |
+-----------------------------------------+

An example of gen_dictionary():

mysql> SELECT gen_dictionary('trees');

Expected output

+--------------------------------------------------+
| gen_dictionary('trees')                          |
+--------------------------------------------------+
| Norway spruce                                    |
+--------------------------------------------------+

An example of gen_dictionary_drop():

mysql> SELECT gen_dictionary_drop('mytestdict')

Expected output

+-------------------------------------+
| gen_dictionary_drop('mytestdict')   |
+-------------------------------------+
| Dictionary removed                  |
+-------------------------------------+

An example of gen_dictionary_load(path, name):

mysql> SELECT gen_dictionary_load('/usr/local/mysql/dict-files/testdict', 'testdict');

Expected output

+-------------------------------------------------------------------------------+
| gen_dictionary_load('/usr/local/mysql/mysql/dict-files/testdict', 'testdict') |
+-------------------------------------------------------------------------------+
| Dictionary load successfully                                                  |
+-------------------------------------------------------------------------------+

这篇关于MySQL数据脱敏(Data masking plugin functions)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/348565

相关文章

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

MySQL中的锁机制详解之全局锁,表级锁,行级锁

《MySQL中的锁机制详解之全局锁,表级锁,行级锁》MySQL锁机制通过全局、表级、行级锁控制并发,保障数据一致性与隔离性,全局锁适用于全库备份,表级锁适合读多写少场景,行级锁(InnoDB)实现高并... 目录一、锁机制基础:从并发问题到锁分类1.1 并发访问的三大问题1.2 锁的核心作用1.3 锁粒度分

MySQL数据库中ENUM的用法是什么详解

《MySQL数据库中ENUM的用法是什么详解》ENUM是一个字符串对象,用于指定一组预定义的值,并可在创建表时使用,下面:本文主要介绍MySQL数据库中ENUM的用法是什么的相关资料,文中通过代码... 目录mysql 中 ENUM 的用法一、ENUM 的定义与语法二、ENUM 的特点三、ENUM 的用法1

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT

MyBatisPlus如何优化千万级数据的CRUD

《MyBatisPlus如何优化千万级数据的CRUD》最近负责的一个项目,数据库表量级破千万,每次执行CRUD都像走钢丝,稍有不慎就引起数据库报警,本文就结合这个项目的实战经验,聊聊MyBatisPl... 目录背景一、MyBATis Plus 简介二、千万级数据的挑战三、优化 CRUD 的关键策略1. 查

mysql中的服务器架构详解

《mysql中的服务器架构详解》:本文主要介绍mysql中的服务器架构,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、mysql服务器架构解释3、总结1、背景简单理解一下mysqphpl的服务器架构。2、mysjsql服务器架构解释mysql的架

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

MySQL之InnoDB存储引擎中的索引用法及说明

《MySQL之InnoDB存储引擎中的索引用法及说明》:本文主要介绍MySQL之InnoDB存储引擎中的索引用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录1、背景2、准备3、正篇【1】存储用户记录的数据页【2】存储目录项记录的数据页【3】聚簇索引【4】二

mysql中的数据目录用法及说明

《mysql中的数据目录用法及说明》:本文主要介绍mysql中的数据目录用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、版本3、数据目录4、总结1、背景安装mysql之后,在安装目录下会有一个data目录,我们创建的数据库、创建的表、插入的