MySQL / MariaDB
Deploy MySQL or MariaDB database with persistent storage
MySQL / MariaDB
Deploy a MySQL or MariaDB database with persistent storage.
Quick Deploy
Create service
Go to Services → New Service
Enter service name: mysql and click Create
Configure source
- Go to the Source tab
- Select Docker Image as source type
- Enter image:
mysql:8ormariadb:11 - Click Save
Add environment variables
MYSQL_ROOT_PASSWORD=your-secure-password
MYSQL_DATABASE=myapp
MYSQL_USER=appuser
MYSQL_PASSWORD=app-secure-passwordAdd storage
| Mount Path | Volume |
|---|---|
/var/lib/mysql | mysql-data |
Deploy
Click Create to deploy.
Configuration Options
MySQL 8
Image: mysql:8Environment variables:
| Variable | Description | Required |
|---|---|---|
MYSQL_ROOT_PASSWORD | Root user password | Yes |
MYSQL_DATABASE | Database to create | No |
MYSQL_USER | Additional user | No |
MYSQL_PASSWORD | User password | No |
MariaDB
Image: mariadb:11Same environment variables as MySQL.
Connecting
From other services
Use Docker networking:
mysql://appuser:app-secure-password@mysql:3306/myappOr with the full container name:
mysql://appuser:password@senate-mysql-xxx:3306/myappExternal access
Exposing databases publicly is a security risk. Use SSH tunnels or VPN instead.
If you must expose externally:
- Add a port mapping:
3306:3306 - Configure firewall rules
Custom Configuration
Using a config file
- Create
my.cnf:
[mysqld]
max_connections = 200
innodb_buffer_pool_size = 1G
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log- Mount as volume:
/path/to/my.cnf:/etc/mysql/conf.d/custom.cnf:roBackups
Manual backup
docker exec senate-mysql-xxx mysqldump -u root -p myapp > backup.sqlRestore
docker exec -i senate-mysql-xxx mysql -u root -p myapp < backup.sqlAutomated backups
Use a cron job or backup service:
#!/bin/bash
DATE=$(date +%Y%m%d)
docker exec senate-mysql-xxx mysqldump -u root -p$MYSQL_ROOT_PASSWORD myapp | gzip > /backups/myapp-$DATE.sql.gzPerformance Tips
- Tune buffer pool: Set
innodb_buffer_pool_sizeto 70-80% of available RAM - Use SSDs: For database storage volumes
- Monitor slow queries: Enable slow query log
- Regular maintenance: Run
OPTIMIZE TABLEperiodically
Related
- Storage - Volume management
- Environment Variables - Configuration