A MySQL proxy that automatically creates databases when clients connect to them. This is designed for development and testing environments where you need to automatically provision databases for multiple services.
This proxy is intended for automated deployments with Docker Swarm stacks where each service needs its own MySQL database. It's NOT suitable for production use.
- Automatic database creation
- Environment-based configuration
- Robust error handling
- Connection timeouts
- Structured logging with Logrus
- MySQL 8.3 compatibility
- The proxy listens on a configurable port (default: 3308)
- When a client connects, it intercepts the MySQL handshake
- Extracts the requested database name from the connection
- Validates the database name for security
- Creates the database if it doesn't exist
- Forwards the connection to the real MySQL server
The proxy can be configured using environment variables:
| Variable | Default | Description |
|---|---|---|
PROXY_PORT |
3308 |
Port for the proxy to listen on |
MYSQL_HOST |
localhost |
MySQL server hostname |
MYSQL_PORT |
3306 |
MySQL server port |
MYSQL_USER |
root |
MySQL username for database creation |
MYSQL_PASSWORD |
test |
MySQL password for database creation |
LOG_LEVEL |
info |
Logging level (debug, info, warn, error, fatal, panic) |
CONNECTION_TIMEOUT |
10s |
Timeout for initial MySQL connection |
READ_TIMEOUT |
30s |
Timeout for read operations |
WRITE_TIMEOUT |
30s |
Timeout for write operations |
MIGRATION_TIMEOUT |
5m |
Timeout for long-running database operations (e.g., migrations) |
KEEPALIVE_PERIOD |
30s |
TCP keepalive period for connection management |
# Pull the latest image
docker pull ghcr.io/ibmurai/mysql-auto-db-proxy:latest
# Run with default configuration
docker run -d \
--name mysql-proxy \
-p 3308:3308 \
ghcr.io/ibmurai/mysql-auto-db-proxy:latest
# Run with custom configuration
docker run -d \
--name mysql-proxy \
-p 3308:3308 \
-e MYSQL_HOST=mysql-server \
-e MYSQL_PORT=3306 \
-e MYSQL_USER=admin \
-e MYSQL_PASSWORD=secure_password \
-e LOG_LEVEL=debug \
ghcr.io/ibmurai/mysql-auto-db-proxy:latest# Start the proxy with default configuration
go run main.go
# Connect to a database (will be created automatically)
mysql -h localhost -P 3308 -u root -p -D myapp_dbservices:
mysql:
image: mysql:8.3
environment:
MYSQL_ROOT_PASSWORD: password
ports:
- "3306:3306"
mysql-auto-db-proxy:
image: ghcr.io/ibmurai/mysql-auto-db-proxy:latest
ports:
- "3308:3308"
environment:
MYSQL_HOST: mysql
MYSQL_PORT: 3306
MYSQL_USER: root
MYSQL_PASSWORD: password
LOG_LEVEL: info
depends_on:
- mysql
networks:
- app-network
myapp:
image: myapp:latest
environment:
ConnectionStrings__DefaultConnection: "Server=mysql-auto-db-proxy;Port=3308;Database=myapp;User Id=root;Password=password;SslMode=none;AllowPublicKeyRetrieval=true;"
depends_on:
- mysql-auto-db-proxy
networks:
- app-network
networks:
app-network:
driver: bridgeThe MySQL Auto DB Proxy is fully compatible with Entity Framework Core and supports multiple MySQL client libraries.
- Pomelo.EntityFrameworkCore.MySql
- MySql.EntityFrameworkCore
- MySQL CLI
// In appsettings.json or environment variable
"ConnectionStrings__DefaultConnection": "Server=mysql-auto-db-proxy;Port=3308;Database=myapp;User Id=root;Password=password;SslMode=none;AllowPublicKeyRetrieval=true;"| Parameter | Value | Description |
|---|---|---|
Server |
mysql-auto-db-proxy |
Proxy hostname |
Port |
3308 |
Proxy port (default) |
Database |
your_database_name |
Database name (auto-created) |
User Id |
root |
MySQL username |
Password |
your_password |
MySQL password |
SslMode |
none |
Required - SSL must be disabled |
AllowPublicKeyRetrieval |
true |
Required - Allows client to request public key from server |
When your EF Core application connects:
- Database specified in connection string is automatically created
- EF Core migrations can run normally
- Multiple services can use different database names
- No manual database setup required
# Connect to proxy (SSL disabled)
mysql -h mysql-auto-db-proxy -P 3308 -u root -ppassword --ssl-mode=DISABLED
# Connect to specific database (auto-creates if doesn't exist)
mysql -h mysql-auto-db-proxy -P 3308 -u root -ppassword --ssl-mode=DISABLED -D my_new_database
# Execute commands directly
mysql -h mysql-auto-db-proxy -P 3308 -u root -ppassword --ssl-mode=DISABLED -e "SHOW DATABASES;"# Build locally
go build -o mysql-auto-db-proxy main.go
# Build Docker image
docker build -t mysql-auto-db-proxy .- Not for production
- No connection pooling
- No SSL support
This project is licensed under the MIT License.