Encrypting MySQL data at rest and in transit protects against storage theft and network interception. This guide covers InnoDB tablespace encryption, TLS setup, and binary log encryption.
Encryption at Rest: InnoDB Tablespace Encryption
-- Enable keyring plugin (MySQL 8.0+)
-- Add to my.cnf:
-- early-plugin-load=keyring_file.so
-- keyring_file_data=/var/lib/mysql-keyring/keyring
-- Encrypt a table
CREATE TABLE sensitive_data (
id INT PRIMARY KEY,
payload TEXT
) ENCRYPTION='Y';
-- Encrypt existing table (online DDL)
ALTER TABLE existing_table ENCRYPTION='Y';
-- Check encryption status
SELECT name, encryption
FROM information_schema.innodb_tablespaces
WHERE encryption = 'Y';Encrypt the MySQL System Tablespace
[mysqld]
innodb_encrypt_tables = ON
innodb_encrypt_log = ON
default_table_encryption = ONTLS for Client Connections
# Generate CA and server certs
mysql_ssl_rsa_setup --datadir=/var/lib/mysql
# This creates: ca.pem, server-cert.pem, server-key.pem, client-cert.pem, client-key.pem[mysqld]
ssl_ca = /var/lib/mysql/ca.pem
ssl_cert = /var/lib/mysql/server-cert.pem
ssl_key = /var/lib/mysql/server-key.pem
require_secure_transport = ON-- Require TLS per user
ALTER USER 'app_user'@'%' REQUIRE SSL;
-- Or require specific cipher
ALTER USER 'app_user'@'%' REQUIRE CIPHER 'ECDHE-RSA-AES256-GCM-SHA384';
-- Verify TLS is active on a connection
SHOW STATUS LIKE 'Ssl_cipher';Binary Log Encryption
[mysqld]
binlog_encryption = ON
-- Requires keyring plugin to be loadedEncrypt Data in MySQL with AES_ENCRYPT
-- Application-level encryption for specific columns
SET @key = SHA2('my-secret-key', 256);
INSERT INTO users (email, ssn_encrypted)
VALUES ('user@example.com', AES_ENCRYPT('123-45-6789', @key));
SELECT AES_DECRYPT(ssn_encrypted, @key) AS ssn
FROM users WHERE email = 'user@example.com';Key Takeaways
- Enable InnoDB tablespace encryption with the keyring plugin for at-rest protection
- Set
require_secure_transport = ONto reject unencrypted connections - Encrypt binary logs when they contain sensitive data
- Use application-level
AES_ENCRYPTfor column-level sensitivity, but manage keys externally
JusDB Can Help
Database encryption must be layered across storage, transport, and application. JusDB security engineers can audit and harden your MySQL deployment.