The SYNC command allows manual control over snapshot synchronization from MySQL to MygramDB. This prevents unexpected load on the MySQL master server during startup.
Starting from version 1.X.X, MygramDB no longer automatically builds snapshots on startup by default:
replication:
enable: true
auto_initial_snapshot: false # Default: false (safe by default)
server_id: 12345
start_from: "snapshot"To restore the previous automatic snapshot behavior:
replication:
enable: true
auto_initial_snapshot: true # Restore legacy behavior
server_id: 12345
start_from: "snapshot"Manually trigger snapshot synchronization for a specific table.
Syntax:
SYNC <table_name>
Example:
# Using CLI
mygram-cli SYNC articles
# Using telnet/nc
echo "SYNC articles" | nc localhost 11016Response (Success):
OK SYNC STARTED table=articles job_id=1
Response (Error):
ERROR SYNC already in progress for table 'articles'
ERROR Memory critically low. Cannot start SYNC. Check system memory.
ERROR Table 'products' not found in configuration
Behavior:
- Runs asynchronously in the background
- Returns immediately after starting
- Builds snapshot from MySQL SELECT query
- Captures GTID at snapshot time
- Automatically starts binlog replication with captured GTID when complete
Check the progress and status of SYNC operations.
Syntax:
SYNC STATUS
Example:
# Using CLI
mygram-cli SYNC STATUS
# Using telnet/nc
echo "SYNC STATUS" | nc localhost 11016Response Examples:
In Progress:
table=articles status=IN_PROGRESS progress=10000/25000 rows (40.0%) rate=5000 rows/s
Completed:
table=articles status=COMPLETED rows=25000 time=5.2s gtid=uuid:123 replication=STARTED
Failed:
table=articles status=FAILED rows=5000 error="MySQL connection lost"
Idle (No sync performed):
status=IDLE message="No sync operation performed"
| Field | Description | Example |
|---|---|---|
table |
Table name being synced | articles |
status |
Current status | IN_PROGRESS, COMPLETED, FAILED, IDLE, CANCELLED |
progress |
Current/total rows processed | 10000/25000 rows (40.0%) |
rate |
Processing rate | 5000 rows/s |
rows |
Total rows processed | 25000 |
time |
Total processing time | 5.2s |
gtid |
Captured snapshot GTID | uuid:123 |
replication |
Replication status | STARTED, ALREADY_RUNNING, DISABLED, FAILED |
error |
Error message (if failed) | MySQL connection lost |
- STARTED: Binlog replication started from snapshot GTID (success)
- ALREADY_RUNNING: Replication was already running (GTID not updated)
- DISABLED: Replication is disabled in configuration
- FAILED: Snapshot succeeded but replication failed to start (check logs)
| Command | Behavior | Reason |
|---|---|---|
DUMP LOAD |
❌ Blocked | Cannot load dump while SYNC is in progress (prevents data corruption) |
REPLICATION START |
❌ Blocked | SYNC automatically starts replication when complete |
SYNC (same table) |
❌ Blocked | SYNC already in progress for this table |
| Command | Behavior | Notes |
|---|---|---|
SEARCH |
✅ Allowed | Returns partial results (data loaded so far) |
COUNT |
✅ Allowed | Returns current count (increases as sync progresses) |
GET |
✅ Allowed | Returns document if loaded, error if not yet loaded |
INFO |
✅ Allowed | Shows current server state including sync progress |
DUMP SAVE |
Allowed but logs warning (saving incomplete snapshot) | |
SYNC (different table) |
✅ Allowed | Multi-table support, independent operations |
REPLICATION STOP |
✅ Allowed | Independent operation |
# 1. Start MygramDB (auto_initial_snapshot=false)
./mygramdb --config config.yaml
# 2. Server starts without building snapshot (no MySQL load)
# Log: "Skipping automatic snapshot build for table: articles (auto_initial_snapshot=false)"
# 3. When ready, manually trigger SYNC
mygram-cli SYNC articles
# 4. Monitor progress
mygram-cli SYNC STATUS
# Output: table=articles status=IN_PROGRESS progress=5000/10000 rows (50.0%) rate=2000 rows/s
# 5. Wait for completion
mygram-cli SYNC STATUS
# Output: table=articles status=COMPLETED rows=10000 time=5.0s gtid=uuid:456 replication=STARTED# config.yaml
tables:
- name: "articles"
# ...
- name: "products"
# ...
- name: "users"
# ...
replication:
enable: true
auto_initial_snapshot: false# Sync all tables independently
mygram-cli SYNC articles
mygram-cli SYNC products
mygram-cli SYNC users
# Check all sync statuses
mygram-cli SYNC STATUS
# Output:
# table=articles status=COMPLETED rows=10000 time=5.0s gtid=uuid:123 replication=STARTED
# table=products status=IN_PROGRESS progress=5000/20000 rows (25.0%) rate=3000 rows/s
# table=users status=COMPLETED rows=5000 time=2.5s gtid=uuid:123 replication=ALREADY_RUNNING# Schedule SYNC during off-peak hours (e.g., 2 AM)
# Add to cron:
0 2 * * * echo "SYNC articles" | nc localhost 11016When MygramDB receives a shutdown signal (SIGTERM/SIGINT) during SYNC:
-
Active SYNC operations are cancelled
SnapshotBuilder::Cancel()is called- Status changes to
CANCELLED
-
Server waits for cleanup
- Maximum wait time: 30 seconds
- Background threads terminate gracefully
-
Server shuts down
- MySQL connections closed
- Resources released
Example Log:
INFO: Stopping TCP server...
INFO: Cancelling SYNC for table: articles
INFO: SYNC cancelled for table articles due to shutdown
WARN: Timeout waiting for SYNC operations to complete
INFO: TCP server stopped
-
Use
auto_initial_snapshot: falsein production- Prevents unexpected MySQL master load on startup
- Allows operators to control when sync occurs
-
Monitor SYNC progress
- Use
SYNC STATUSto track progress - Check server logs for detailed information
- Use
-
Avoid concurrent DUMP LOAD
- Wait for SYNC to complete before loading dumps
- Check
SYNC STATUSfirst
-
Schedule SYNC during off-peak hours
- Use cron or other schedulers
- Reduce impact on MySQL master
-
Monitor memory before SYNC
- Check available system memory
- SYNC checks memory health automatically
Error: Memory critically low. Cannot start SYNC. Check system memory.
Solution:
- Check available system memory
- Increase memory limits in configuration
- Reduce batch size in build configuration
Error: status=FAILED error="MySQL connection lost"
Solution:
- Check MySQL server connectivity
- Verify MySQL credentials
- Check MySQL server logs
- Ensure replication user has sufficient privileges
Status: replication=FAILED
Solution:
- Check server logs for detailed error message
- Verify MySQL GTID is enabled:
SHOW VARIABLES LIKE 'gtid_mode'; - Verify binlog format is ROW:
SHOW VARIABLES LIKE 'binlog_format'; - Check replication user privileges:
SHOW GRANTS FOR 'repl_user'@'%';