MygramDB supports real-time replication from MySQL using GTID-based binlog streaming with guaranteed data consistency.
MygramDB requires:
- MySQL Version: 8.4+ / 9.x (tested with MySQL 8.4 and 9.6)
- GTID Mode: Must be enabled
- Binary Log Format: ROW format required
- Privileges: Replication user needs specific privileges
Check if GTID mode is enabled:
SHOW VARIABLES LIKE 'gtid_mode';If GTID mode is OFF, enable it:
-- Enable GTID mode
SET GLOBAL gtid_mode = ON;
SET GLOBAL enforce_gtid_consistency = ON;Ensure binary logging is enabled with ROW format:
-- Check binary log format
SHOW VARIABLES LIKE 'binlog_format';
-- Set to ROW format (add to my.cnf and restart)
SET GLOBAL binlog_format = ROW;Create a user with replication privileges:
-- Create replication user
CREATE USER 'repl_user'@'%' IDENTIFIED BY 'your_password';
-- Grant replication privileges (for binlog reading and GTID information)
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl_user'@'%';
-- Grant SELECT privilege for snapshot creation (REQUIRED)
-- Note: SELECT privilege on target tables is necessary for snapshot building
GRANT SELECT ON database_name.table_name TO 'repl_user'@'%';
-- Apply changes
FLUSH PRIVILEGES;Important Notes:
- REPLICATION CLIENT privilege is required: Necessary for retrieving GTID information
- SELECT privilege is required: Necessary for reading table data during initial snapshot creation
- No restart required:
GRANTstatements are applied online and take effect immediately - Principle of least privilege: When synchronizing multiple tables, grant SELECT privilege for each table individually
- MySQL credentials are transmitted in plain text unless your MySQL server requires TLS. Place MygramDB close to MySQL on a trusted network, or terminate TLS/SSH tunnels in front of it when replicating across untrusted links.
- Snapshots created via
DUMP SAVEinclude the MySQL host/user/password. Store dump files on encrypted storage with restrictive permissions (e.g.,chmod 600) and rotate them like any other secret.
Starting from version 1.X.X, MygramDB supports manual snapshot synchronization to prevent unexpected load on the MySQL master during startup.
By default, MygramDB no longer automatically builds snapshots on startup:
replication:
enable: true
auto_initial_snapshot: false # Default: false (safe by default)
server_id: 12345
start_from: "snapshot"Use the SYNC command to manually trigger snapshot synchronization:
# Trigger snapshot sync for a table
mygram-cli SYNC articles
# Check sync progress
mygram-cli SYNC STATUSSee SYNC Command Guide for detailed usage.
To restore automatic snapshot building on startup:
replication:
auto_initial_snapshot: true # Restore legacy behaviorBenefits of manual sync:
- Prevents unexpected MySQL load during server startup
- Allows operators to control when synchronization occurs
- Enables scheduled sync during off-peak hours
- Provides progress monitoring and cancellation
Configure replication.start_from in your config file:
Starts from GTID captured during initial snapshot build:
replication:
start_from: "snapshot"How it works:
- Uses
START TRANSACTION WITH CONSISTENT SNAPSHOTfor data consistency - Captures
@@global.gtid_executedat exact snapshot moment - Guarantees no data loss between snapshot and binlog replication
When to use:
- Initial setup (recommended for most cases)
- When you need a consistent point-in-time view
- When starting from scratch
Starts from current GTID position (ignores historical data):
replication:
start_from: "latest"How it works:
- Uses
SHOW BINARY LOG STATUSto get latest GTID - Only captures changes after MygramDB starts
When to use:
- When you only need real-time changes
- When historical data is not important
Starts from specific GTID position:
replication:
start_from: "gtid=3E11FA47-71CA-11E1-9E33-C80AA9429562:100"When to use:
- Manual recovery from specific point
- Testing or debugging
MygramDB automatically handles:
- INSERT (WRITE_ROWS events)
- Adds new documents to index and store
- UPDATE (UPDATE_ROWS events)
- Updates document content and filters
- Re-indexes text if changed
- DELETE (DELETE_ROWS events)
- Removes document from index and store
MygramDB handles these DDL operations:
Automatically clears index and document store for the target table:
TRUNCATE TABLE articles;MygramDB will:
- Clear all documents from the table
- Clear all posting lists
- Reset document ID counter
Clears all data and logs an error:
DROP TABLE articles;MygramDB will:
- Clear all data
- Log error message
- Require manual restart/reconfiguration
Logs a warning about potential schema inconsistency:
ALTER TABLE articles ADD COLUMN new_col VARCHAR(100);
ALTER TABLE articles MODIFY COLUMN content TEXT;Important notes:
- Type changes (e.g., VARCHAR to TEXT) may cause replication issues
- Column additions/removals affecting
text_sourceorfiltersrequire MygramDB restart - Recommendation: Rebuild MygramDB snapshot after schema changes
MygramDB can replicate these MySQL column types:
- TINYINT, SMALLINT, INT, MEDIUMINT, BIGINT (signed/unsigned)
- VARCHAR, CHAR, TEXT, BLOB, ENUM, SET
- DATE, TIME, DATETIME, TIMESTAMP (with fractional seconds)
- DECIMAL, FLOAT, DOUBLE
- JSON, BIT, NULL
- Snapshot and binlog replication are coordinated via consistent snapshot transaction
- No data loss between snapshot and replication
- Atomic persistence with state file
- Automatic save on shutdown
- Resume on restart
- Checks GTID mode on startup
- Clear error messages if not configured
- Handles connection loss gracefully
- Exponential backoff retry (configurable)
- Continues from last GTID position
- Thread pool architecture for efficient request handling
- Configurable queue size for performance tuning
Use the CLI or TCP protocol:
# Using CLI
./build/bin/mygram-cli REPLICATION STATUS
# Using telnet
echo "REPLICATION STATUS" | nc localhost 11016Response:
OK REPLICATION status=running gtid=3E11FA47-71CA-11E1-9E33-C80AA9429562:1-100
Stop binlog replication (index becomes read-only):
./build/bin/mygram-cli REPLICATION STOPResume binlog replication:
./build/bin/mygram-cli REPLICATION STARTSolution: Enable GTID mode on MySQL server:
SET GLOBAL gtid_mode = ON;
SET GLOBAL enforce_gtid_consistency = ON;Then restart MygramDB.
Solution: Set binary log format to ROW:
SET GLOBAL binlog_format = ROW;Or add to my.cnf and restart MySQL:
[mysqld]
binlog_format = ROWPossible causes:
- High write volume on MySQL
- Insufficient MygramDB resources
- Network latency
Solutions:
- Increase
replication.queue_sizein config - Increase
build.parallelismfor faster processing - Add more MygramDB replicas
MygramDB will automatically reconnect with exponential backoff:
replication:
reconnect_backoff_min_ms: 500
reconnect_backoff_max_ms: 10000Solution: Rebuild snapshot after schema changes:
- Stop MygramDB
- Update config file to match new schema
- Restart MygramDB (will rebuild snapshot)
- Always use GTID mode for consistent replication
- Use
snapshotstart mode for initial setup - Monitor replication lag regularly
- Rebuild snapshot after significant schema changes
- Test configuration before deploying to production
- Keep state file for crash recovery
- Use multiple replicas for high availability
Complete replication configuration:
mysql:
host: "127.0.0.1"
port: 3306
user: "repl_user"
password: "your_password"
database: "mydb"
use_gtid: true
binlog_format: "ROW"
binlog_row_image: "FULL"
replication:
enable: true
server_id: 0 # 0 = auto-generate
start_from: "snapshot" # snapshot|latest|gtid=<UUID:txn>
queue_size: 10000
reconnect_backoff_min_ms: 500
reconnect_backoff_max_ms: 10000- SYNC Command Guide - Manual snapshot synchronization
- Configuration Guide - Full configuration reference
- Protocol Reference - REPLICATION commands