Version: 1.0.0
Description: MySQL to Cloudflare D1 Database migration in Node.js
- Clone the repository:
git clone https://github.com/oliwebd/mysql-sqlite-to-d1.git .
- Install dependencies:
npm installCreate a .env file in the project root to store your credentials.
# MySQL connection URL
MYSQL_URL="mysql://username:password@localhost:3306/mysqldbname"
# Cloudflare account details
CLOUDFLARE_ACCOUNT_ID="xxxoxxxxxxxxxx"
CLOUDFLARE_API_TOKEN="xxxox-xxxx-xxxx-xxxx"
# Cloudflare D1 database IDs
D1_DATABASE_ID="xxxox-xxxxx-xxxx"| Script | Description |
|---|---|
npm run migrate |
Runs full SQLite → D1 migration. |
npm run test-d1-conn |
Test connection to Cloudflare D1 / Check API Troken Ok or But you Run migrate Directly with first option. |
npm run start |
Generate SQL from MySQL and create local SQLite test DB |
This Node.js project allows you to migrate MySQL databases to Cloudflare D1, using SQLite as an intermediate step for testing. The workflow ensures:
- Schema generation from MySQL / auto dump file for simple migration
- Data transfer verification
- Clean D1 data before migration without conflicts
- Batch insertion for performance / 4k row in 30sec
- Foreign key safety during migration / if Foreign key Error during migrations Just delete Key from MySQL then again migrate then make sql query to add in d1/sqlite manually
- Connects to MySQL and generates a complete SQL dump.
- Creates a test SQLite database to verify migration.
- Migrates data into Cloudflare D1, skipping the
_cf_KVtable. - Batch processing for large datasets.
- Verifies migration by comparing row counts.
npm run start- Connects to MySQL
- Generates
temp/migration.sql - Creates
localsqlite.dbfor testing
npm run migrate- Cleans D1 database (skipping
_cf_KV) - Creates table schemas
- Inserts data in batches
- Verifies row counts for accuracy
🚀 Starting SQLite file → D1 migration...
📖 Reading migration file...
Found 4 CREATE TABLE statements
Found 1739 INSERT statements
...
🎉 Perfect migration! All 4 tables match.
- SQLite timestamps are stored in ISO format.
- Foreign keys are temporarily disabled during migration for safety. Still d1 check foreign key by default.
_cf_KVtable in D1 is never dropped / Default System Table for D1.- Migration is verified by comparing row counts; mismatched tables are flagged.
GPLv3
Oli Miah
PayPal: Buy 💰