Skip to content
logo
Percona Server for MySQL
Data masking
Initializing search
    percona/psmysql-docs
    percona/psmysql-docs
    • Home
      • The Percona XtraDB storage engine
      • List of features available in Percona Server for MySQL releases
      • Percona Server for MySQL feature comparison
      • Changed in Percona Server 8.0
      • Understand version numbers
      • Install Percona Server for MySQL
        • Install using APT repository
        • Files in DEB package
        • Build APT packages
        • Install from downloaded DEB packages
        • Apt pinning
        • Run Percona Server for MySQL
        • Uninstall
        • Install using a RPM repository
        • Files in RPM package
        • Install from downloaded RPM packages
        • Run Percona Server for MySQL
        • Uninstall
        • Install with binary tarballs
        • Binary tarballs available
        • Install Percona Server for MySQL from a source tarball
        • Compile Percona Server for MySQL 8.0 from source
      • Post-installation
      • Percona Server for MySQL in-place upgrading guide: from 5.7 to 8.0
      • Upgrade using the Percona repositories
      • Upgrade from systems that use the MyRocks or TokuDB storage engine and partitioned tables
      • Upgrade using Standalone Packages
      • Downgrade Percona Server for MySQL
      • Running Percona Server for MySQL in a Docker Container
      • Docker environment variables
      • Improved InnoDB I/O scalability
      • Adaptive network buffers
      • Multiple page asynchronous I/O requests
      • Thread pool
      • XtraDB performance improvements for I/O-bound highly-concurrent workloads
      • Prefix index queries optimization
      • Limit the estimation of records in a Query
      • Jemalloc memory allocation profiling
      • The ProcFS plugin
      • Binary logs and replication improvements
      • Compressed columns with dictionaries
      • Extended SELECT INTO OUTFILE/DUMPFILE
      • Extended SET VAR optimizer hint
      • Improved MEMORY storage engine
      • Suppress warning messages
      • Limiting the disk space used by binary log files
      • Support for PROXY protocol
      • SEQUENCE_TABLE(n) function
      • Slow query log rotation and expiration
      • Trigger updates
      • Extended mysqlbinlog
      • Extended mysqldump
      • InnoDB full-text search improvements
      • Too many connections warning
      • Handle corrupted tables
      • Percona Toolkit UDFs
      • Kill idle transactions
      • XtraDB changed page tracking
      • Enforcing storage engine
      • Expanded fast index creation
      • Backup locks
      • Audit log plugin
      • Start transaction with consistent snapshot
      • Extended SHOW GRANTS
      • Utility user
      • Working with SELinux
      • Working with AppArmor
      • PAM authentication plugin
      • Server variables
      • SSL improvements
      • Data masking
        • Installing the plugin
        • Data masking functions
        • General purpose
        • Special Purpose
        • Generate random data for specific requirements
        • Use dictionaries to generate random terms
        • Uninstalling the plugin
        • Using LDAP authentication plugins
        • LDAP authentication plugin system variables
        • Data at Rest Encryption
        • Use the keyring component or keyring plugin
          • Using the Key Management Interoperability Protocol (KMIP)
          • Use the Amazon Key Management Service (AWS KMS)
          • FIDO authentication plugin
        • Encryption functions
        • Encrypt File-Per-Table Tablespace
        • Encrypt schema or general tablespace
        • Encrypt system tablespace
        • Encrypt temporary files
        • Encrypt Binary Log Files and Relay Log Files
        • Encrypting the Redo Log data
        • Encrypt the undo tablespace
        • Rotate the master key
        • Advanced encryption key rotation
        • Encrypt doublewrite buffers
        • Verify the encryption for tables, tablespaces, and schemas
      • User statistics
      • Slow query log
      • Extended show engine InnoDB status
      • Show storage engines
      • Process list
      • Misc. INFORMATION_SCHEMA tables
      • Thread based profiling
      • InnoDB page fragmentation counters
      • Stacktrace
      • Libcoredumper
      • Manage group replication flow control
      • Group replication system variables
      • Percona MyRocks introduction
      • Percona MyRocks installation guide
      • Updated supported features
      • MyRocks limitations
      • Differences between Percona MyRocks and Facebook MyRocks
      • MyRocks Information Schema tables
      • MyRocks server variables
      • MyRocks status variables
      • Gap locks detection
      • Data loading
      • Installing and configuring Percona Server for MySQL with ZenFS support
      • TokuDB introduction
      • TokuDB installation
      • Use TokuDB
      • Fast updates with TokuDB
      • TokuDB files and file types
      • TokuDB file management
      • TokuDB background ANALYZE TABLE
      • TokuDB variables
      • TokuDB status variables
      • TokuDB fractal tree indexing
      • TokuDB troubleshooting
      • TokuDB Performance Schema integration
      • Frequently asked questions
      • Migrate and removing the TokuDB storage engine
      • Percona TokuBackup
      • Release notes index
      • Percona Server for MySQL 8.0.32-24 (2023-03-20)
      • Percona Server for MySQL 8.0.31-23 (2023-02-09)
      • Percona Server for MySQL 8.0.30-22 Update (2022-11-21)
      • Percona Server for MySQL 8.0.30-22 (2022-11-21)
      • Percona Server for MySQL 8.0.29-21 (2022-08-08)
      • Percona Server for MySQL 8.0.28-20 (2022-06-20)
      • Percona Server for MySQL 8.0.28-19 (2022-05-12)
      • Percona Server for MySQL 8.0.27-18 (2022-03-02)
      • Percona Server for MySQL 8.0.26-17 (2022-01-26)
      • Percona Server for MySQL 8.0.26-16 (2021-10-20)
      • Percona Server for MySQL 8.0.25-15 (2021-07-13)
      • Percona Server for MySQL 8.0.23-14 (2021-05-12)
      • Percona Server for MySQL 8.0.22-13 (2020-12-14)
      • Percona Server for MySQL 8.0.21-12 (2020-10-13)
      • Percona Server for MySQL 8.0.20-11 (2020-07-21)
      • Percona Server for MySQL 8.0.19-10 (2020-03-23)
      • Percona Server for MySQL 8.0.18-9
      • Percona Server for MySQL 8.0.17-8
      • Percona Server for MySQL 8.0.16-7
      • Percona Server for MySQL 8.0.15-6
      • Percona Server for MySQL 8.0.15-5
      • Percona Server for MySQL 8.0.14
      • Percona Server for MySQL 8.0.13-4
      • Percona Server for MySQL 8.0.13-3
      • Percona Server for MySQL 8.0.12-2rc1
      • List of variables introduced in Percona Server for MySQL 8.0
      • Development of Percona Server for MySQL
      • Trademark policy
      • Index of INFORMATION_SCHEMA tables
      • Frequently asked questions
      • Copyright and licensing information
      • Glossary

    • Installing the plugin
    • Data masking functions
    • General purpose
    • Special Purpose
    • Generate random data for specific requirements
    • Use dictionaries to generate random terms
    • Uninstalling the plugin

    Data masking¶

    This feature was implemented in Percona Server for MySQL version Percona Server for MySQL 8.0.17-8.

    The Percona Data Masking plugin is a free and Open Source implementation of the MySQL’s data masking plugin. Data Masking provides a set of functions to hide sensitive data with modified content.

    Data masking can have either of the characteristics:

    • Generation of random data, such as an email address

    • De-identify data by transforming the data to hide content

    Installing the plugin¶

    The following command installs the plugin:

    $ INSTALL PLUGIN data_masking SONAME 'data_masking.so';
    

    Data masking functions¶

    The data masking functions have the following categories:

    • General purpose

    • Special purpose

    • Generating Random Data with Defined characteristics

    • Using Dictionaries to Generate Random Data

    General purpose¶

    The general purpose data masking functions are the following:

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

    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:

    Parameter Description
    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.

    Parameter Description
    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. The secure-file-priv option defines the directories where gen_dictionary_load() loads the dictionary files.

    Note

    Percona Server for MySQL 8.0.21-12 enabled using the secure-file-priv option for gen_dictionary_load().

    Parameter Description Returns
    gen_blacklist(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 terms A 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_blacklist():

    mysql> SELECT gen_blacklist('apple', 'fruit', 'nut');
    
    Expected output
    +-----------------------------------------+
    | gen_blacklist('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                                                  |
    +-------------------------------------------------------------------------------+
    

    Uninstalling the plugin¶

    The UNINSTALL PLUGIN statement disables and uninstalls the plugin.

    Contact us

    For free technical help, visit the Percona Community Forum.

    To report bugs or submit feature requests, open a JIRA ticket.

    For paid support and managed or consulting services , contact Percona Sales.


    Last update: 2023-01-12
    Percona LLC and/or its affiliates, © 2023
    Made with Material for MkDocs

    Cookie consent

    We use cookies to recognize your repeated visits and preferences, as well as to measure the effectiveness of our documentation and whether users find what they're searching for. With your consent, you're helping us to make our documentation better.