Skip to main content
This appendix details the implementation of column-level encryption within MariaDB databases using its built-in encryption functions like AES_ENCRYPT and AES_DECRYPT. When paired with Transparent Data Protection (TDP), this approach enables granular security controls where specific database directories can be selectively encrypted. By combining these complementary encryption methodologies, you can establish a multi-layered defense strategy that protects sensitive data both at the application level and within the physical storage architecture.

Connect to MariaDB

Connect to MariaDB by using one of the following techniques:
  1. Use HeidiSQL.
  2. Use the command line.

Use HeidiSQL (GUI)

Perform the following steps to connect to MariaDB by using HeidiSQL (GUI):
1
Launch the HeidiSQL application.
2
Select [ New ] in the Session Manager window to create a new session.
3
Enter the following Connection settings:
  • Network type: Select MariaDB or MySQL (TCP/IP).
  • Hostname / IP: Enter your server IP address or hostname (use 127.0.0.1 or localhost for local connections).
  • User: Enter your MariaDB username (such as root or a specific user).
  • Password: Enter your password.
  • Port: 3306 (default for MariaDB/MySQL).
4
Select [ Open ].

Use the command line

Perform the following steps to connect to MariaDB by using the command line:
1
Open your terminal or command prompt.
2
Execute the following command to connect by using the mariadb (or mysql) client:
PowerShell
  • Replace your_host_or_ip and your_username.
  • When prompted, enter your password.
For local connections, you might use the following command instead:
PowerShell

Create a new database

Create a new database by using one of the following techniques:
  1. Use HeidiSQL.
  2. Use the command line.

Use HeidiSQL

1
Right-click on your server connection name in the left panel.
2
Select Create new > Database.
3
Enter a name (such as TestDB).
4
Select [ OK ].

Use the command line

1
Execute the following SQL command:
SQL
2
Switch to the new database:
SQL

Create a sample table and apply AES_ENCRYPT at the column level

The following SQL query creates a table and inserts data, encrypting the ssn column by using AES_ENCRYPT.
AES_ENCRYPT returns a binary string (VARBINARY or BLOB). We define the ssn column accordingly.
Execute this SQL code in your HeidiSQL Query tab or your command-line client.

View unencrypted data

To decrypt the data when querying, use the AES_DECRYPT function. You likely need to CAST the result back to a readable character type (like CHAR) by using the following command:

View column-level encrypted data

To see the raw encrypted data stored in the table (which appears as binary or unreadable characters in most clients), use the following command to select the column directly:

View column-level encrypted data in hexadecimal format

To view the encrypted binary data as a more readable hexadecimal string, use the following HEX() function:

Find the physical DB files location

MariaDB typically stores each database in a subdirectory within its main data directory. This section explores locating the data directory and database folder.

Find the MariaDB data directory

Run the following SQL query to find the directory:
Sql
Note the path returned in the Value column (such as C:\Program Files\MariaDB X.Y\data\).

Locate the database folder

  • Go to the datadir path on your server file system.
  • That directory contains a folder named after your database (such as TestDB). This folder contains the physical files for your tables, such as .frm, .ibd files if using InnoDB.

Summary of the Symmetric Key used for an encryption operation

  • Function: AES_ENCRYPT / AES_DECRYPT in MariaDB.
  • Algorithm: Uses the Advanced Encryption Standard (AES). By default, it uses AES-128. You can configure the block encryption mode by using the block_encryption_mode system variable (the default often depends on the MariaDB version, but it might be aes-128-ecb).
  • Key: You directly provide the key_str (passphrase) to the functions. MariaDB uses this string to perform the encryption or decryption.
  • Key Management: Crucially, you are responsible for managing this key_str. MariaDB does not store it in relation to these functions. In real applications, avoid hardcoding keys directly in SQL or application code. Store them securely (such as in a secrets manager, environment variables, or a secure configuration file) and pass them to the application logic that constructs the SQL query.

Encryption example

We recommend you handle key loading in your application code:
  1. Your application reads the encryption key from the text file, such as C:\MariaDBKeys\DEK.txt.
  2. The application then passes this key as a parameter or variable when constructing the SQL query that uses AES_ENCRYPT or AES_DECRYPT.
The following conceptual example is the SQL part only, assuming the application provides the key: