Overview
Dub uses MySQL 8.0+ as its primary database, managed through Prisma ORM. This guide covers database setup for both local development and production environments.Database Requirements
- MySQL 8.0 or higher
- Minimum 1GB RAM allocated to database
- Support for at least 1000 concurrent connections
- InnoDB storage engine (default)
Supported Platforms
Recommended Platforms
- PlanetScale - Serverless MySQL platform (recommended for production)
- Amazon RDS for MySQL - Managed MySQL on AWS
- Google Cloud SQL for MySQL - Managed MySQL on GCP
- Azure Database for MySQL - Managed MySQL on Azure
- Self-managed MySQL 8.0+ - On your own infrastructure
PlanetScale (Recommended)
PlanetScale provides a serverless MySQL platform with branching, non-blocking schema changes, and automatic backups.Create Database
- Sign up at planetscale.com
- Create a new database
- Note your database name
Generate Connection String
- Go to your database settings
- Click “Connect”
- Select “Prisma” as the connection method
- Copy the connection string
Amazon RDS for MySQL
Create RDS Instance
- Open AWS RDS Console
- Click “Create database”
- Choose MySQL 8.0 or higher
- Select instance size (minimum
db.t3.microfor testing) - Configure VPC and security groups
Configure Security Group
Allow inbound traffic on port 3306 from your application servers:
- Type: MySQL/Aurora
- Port: 3306
- Source: Your application’s security group or IP range
Self-Managed MySQL
Local Development Setup
For local development, use Docker Compose to run MySQL and PlanetScale proxy.Using Docker Compose
Dub includes adocker-compose.yml file for local development:
docker-compose.yml
Start Services
From the This starts:
apps/web directory:- MySQL 8.0 on port
3306 - PlanetScale HTTP proxy on port
3900
Prisma Configuration
Dub uses Prisma for database management. The schema is located atpackages/prisma/schema/schema.prisma.
Prisma Schema Overview
relationMode = "prisma" setting enables Prisma to handle foreign key constraints at the application level, which is required for PlanetScale compatibility.
Prisma Commands
Dub includes several Prisma commands inpackage.json:
Initial Database Setup
Generate Prisma Client
Generate the Prisma client from the schema:This creates the Prisma client in
node_modules/@prisma/client.Push Schema to Database
Create all tables in the database:This command:
- Reads the Prisma schema
- Creates all tables and columns
- Sets up indexes
- Configures relationships
Verify Schema
Open Prisma Studio to verify the database structure:This opens a web interface at
http://localhost:5555 where you can:- Browse all tables
- View and edit data
- Test queries
Database Schema
Dub’s database includes the following core tables:- User - User accounts and authentication
- Account - OAuth provider accounts
- Session - User sessions for NextAuth.js
- Project - Workspaces/organizations
- ProjectUsers - User memberships in projects
- Link - Short links
- Domain - Custom domains
- Tag - Link tags and labels
- Token - API tokens
- RestrictedToken - Restricted API tokens with scoped permissions
- Integration - Third-party integrations
- Partner - Affiliate program partners
- Program - Affiliate programs
- Payout - Partner payouts
- Commission - Affiliate commissions
Connection Pooling
For production deployments, configure connection pooling to handle high traffic:PlanetScale Connection Pooling
PlanetScale automatically handles connection pooling. Use the connection string provided:RDS Proxy (AWS)
For Amazon RDS, use RDS Proxy for connection pooling:- Create an RDS Proxy in the AWS Console
- Configure target database
- Update connection string to use proxy endpoint:
External Pooling (PgBouncer Alternative)
For MySQL connection pooling outside of managed services, consider:- ProxySQL - MySQL-specific connection pooler
- MaxScale - MariaDB connection pooler (MySQL compatible)
Performance Optimization
Indexes
Dub’s Prisma schema includes optimized indexes for common queries. Key indexes:Query Performance
Monitor slow queries using:Database Size Management
For analytics data, Dub uses Tinybird instead of storing click events in MySQL, keeping the database size manageable.Backup & Recovery
PlanetScale Backups
PlanetScale provides automatic daily backups with point-in-time recovery:- Backups retained for 7-30 days depending on plan
- Restore through PlanetScale dashboard
- Test restores regularly
Manual Backups
For self-managed databases:Automated Backup Script
backup.sh
Troubleshooting
Connection Issues
Error: “Can’t connect to MySQL server”Schema Sync Issues
Error: “The table does not exist in the current database”Authentication Issues
Error: “Access denied for user”Max Connections Exceeded
Error: “Too many connections”Migration from Other Databases
Dub is designed for MySQL. Migration from PostgreSQL or other databases requires schema conversion.Production Checklist
- Database is MySQL 8.0 or higher
- Connection pooling is configured
- Backups are automated and tested
- Monitoring is set up for performance and errors
- Connection string uses SSL/TLS
- Database credentials are stored securely
-
max_connectionsis set appropriately (minimum 1000) - Slow query log is enabled
- Regular maintenance windows are scheduled
- Disaster recovery plan is documented