CREATE ENCRYPTKEY

Description

Syntax

  1. CREATE ENCRYPTKEY key_name
  2. AS "key_string"

Parameters

key_name: The name of the key to be created, which can include the name of the database. For example: db1.my_key.

key_string: The string to create the key

This statement creates a custom key. Executing this command requires the user to have the ADMIN privileges.

If the database name is included in key_name, then this custom key will be created in the corresponding database, otherwise this function will be created in the database where the current session is located. The name of the new key cannot be the same as the key that already exists in the corresponding database, otherwise the creation will fail.

Example

  1. Create a custom key

    1. CREATE ENCRYPTKEY my_key as "ABCD123456789";
  2. Using a custom key

    To use a custom key, add the keyword KEY/key before the key name, separated from key_name by a space.

    1. mysql> SELECT HEX(AES_ENCRYPT("Doris is Great", KEY my_key));
    2. +------------------------------------------------+
    3. | hex(aes_encrypt('Doris is Great', key my_key)) |
    4. +------------------------------------------------+
    5. | D26DB38579D6A343350EDDC6F2AD47C6 |
    6. +------------------------------------------------+
    7. 1 row in set (0.02 sec)
    8. mysql> SELECT AES_DECRYPT(UNHEX('D26DB38579D6A343350EDDC6F2AD47C6'), KEY my_key);
    9. +--------------------------------------------------------------------+
    10. | aes_decrypt(unhex('D26DB38579D6A343350EDDC6F2AD47C6'), key my_key) |
    11. +--------------------------------------------------------------------+
    12. | Doris is Great |
    13. +--------------------------------------------------------------------+
    14. 1 row in set (0.01 sec)

Keyword

  1. CREATE,ENCRYPTKEY