External MySQL Configuration
Overview
The docker-mailserver can be configured to use an external MySQL database instead of the built-in database service. This is particularly useful for production deployments, Kubernetes environments, or when you want to use an existing MySQL infrastructure.
Environment Variables
To use an external MySQL database, configure the following environment variables in your .env file:
# External MySQL Configuration
MYSQL_HOST=your-mysql-host.example.com
MYSQL_DATABASE=mailserver
MYSQL_USER=mailserver_user
MYSQL_PASSWORD=your_secure_password
Variable Descriptions
- MYSQL_HOST: The hostname or IP address of your MySQL server
- MYSQL_DATABASE: The database name to use (will be created if it doesn't exist)
- MYSQL_USER: The MySQL username for database access
- MYSQL_PASSWORD: The password for the MySQL user
Database Initialization
Required SQL Files
The mailserver requires two SQL initialization files to be imported into your external MySQL database:
target/db/rootfs/docker-entrypoint-initdb.d/001_mailserver.sql- Core mailserver schematarget/db/rootfs/docker-entrypoint-initdb.d/002_webmail.sql- Webmail-specific schema
Import Process
Option 1: Manual Import
- Connect to your MySQL server:
mysql -h your-mysql-host.example.com -u root -p
- Create the database (if it doesn't exist):
CREATE DATABASE mailserver CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
- Import the SQL files:
mysql -h your-mysql-host.example.com -u root -p mailserver < target/db/rootfs/docker-entrypoint-initdb.d/001_mailserver.sql mysql -h your-mysql-host.example.com -u root -p mailserver < target/db/rootfs/docker-entrypoint-initdb.d/002_webmail.sql
Option 2: Using Docker
If you have the SQL files locally, you can use Docker to import them:
# Import mailserver schema
docker run --rm -v $(pwd)/target/db/rootfs/docker-entrypoint-initdb.d:/sql mysql:lts mysql -h your-mysql-host.example.com -u root -p mailserver < /sql/001_mailserver.sql
# Import webmail schema
docker run --rm -v $(pwd)/target/db/rootfs/docker-entrypoint-initdb.d:/sql mysql:lts mysql -h your-mysql-host.example.com -u root -p mailserver < /sql/002_webmail.sql
Docker Compose Configuration
Removing the Built-in Database
To use an external MySQL database with Docker Compose, remove the database service:
- Edit
deploy/compose/db.yaml:
# Comment out or remove the entire db service
# services:
# db:
# image: mysql:lts
# restart: on-failure:5
# env_file: ../../.env
# volumes:
# - ../../target/db/rootfs/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d:ro
# - data-db:/var/lib/mysql
- Update service dependencies:
- Remove
depends_on: - dbfrom services that depend on the database - The services will connect to the external MySQL host instead
Example Configuration
# In your docker-compose.yml or compose override
services:
web:
environment:
- MYSQL_HOST=your-mysql-host.example.com
- MYSQL_DATABASE=mailserver
- MYSQL_USER=mailserver_user
- MYSQL_PASSWORD=your_secure_password
# Remove depends_on: db if present
Kubernetes Configuration
External MySQL is required for Kubernetes deployments, as the provided kustomization does not include a database service.
Prerequisites
- Existing MySQL Server: Ensure you have a MySQL-compatible database (MySQL, Percona XtraDB, MariaDB)
- Database Access: Verify network connectivity from your Kubernetes cluster to the MySQL server
- Credentials: Create a MySQL user with appropriate permissions
Database Migrations
After importing the initial SQL files, you need to restart the web service to apply any database migrations:
Docker Compose
# Restart the web service to apply migrations
docker-compose restart web
# Or if using the production script
bin/production.sh restart web
Kubernetes
# Restart the web deployment
kubectl rollout restart deployment/web -n mail