MySQL Master-Master Replication: How to Configure Automatic Database Synchronization Without Restarts


7 views

When implementing MySQL Master-Master replication in production environments, administrators often need the flexibility to add new databases to the replication set without service interruptions. The conventional approach requiring configuration file modifications and server restarts creates unacceptable downtime for many applications.

Your current setup using MySQL 5.1 with row-based replication (RBR) has a solid foundation. The key parameters like binlog-format = row, proper server IDs, and auto-increment settings are correctly configured. However, the critical missing piece is the database filtering strategy.

# Current problematic parameters (should be removed)
binlog-do-db = db1
replicate-do-db = db1
binlog-ignore-db = mysql
replicate-ignore-db = test

To achieve automatic replication of all databases (including newly created ones), follow these steps:

  1. Remove all database-specific filters from both servers' my.cnf
  2. Configure the replication user with proper global privileges
  3. Verify the binary log position synchronization

Here's how your final configuration should look:

[mysqld]
binlog-format = ROW
server-id = 1 # Unique on each server
log-bin = mysql-bin
sync_binlog = 1
auto-increment-increment = 2
auto-increment-offset = 1 # 2 on second server
expire_logs_days = 7
max_binlog_size = 100M
relay_log_space_limit = 4G

After making these changes, test the setup by:

# On Master 1
CREATE DATABASE new_db;
CREATE TABLE new_db.test_table (id INT);
INSERT INTO new_db.test_table VALUES (1);

# On Master 2 (should appear within seconds)
SELECT * FROM new_db.test_table;

For production environments, consider these enhancements:

  • Implement slave_parallel_workers for multi-threaded replication (MySQL 5.6+)
  • Set up monitoring for replication lag
  • Configure automatic failover using tools like MHA or Orchestrator

Some system databases like mysql shouldn't be replicated. Use these filters instead:

replicate-ignore-db = mysql
replicate-ignore-db = information_schema
replicate-ignore-db = performance_schema

Remember that temporary tables (created with CREATE TEMPORARY TABLE) won't replicate by default in MySQL 5.1. This is important for applications relying on temporary tables.


When implementing MySQL 5.1 master-master replication with row-based replication (RBR), many administrators struggle with dynamically adding new databases to the replication set without service interruption.

Your current approach is fundamentally correct - omitting database-specific filters enables replication of all databases. Here's the critical configuration principle:

# Key parameters to EXCLUDE from my.cnf:
# binlog-do-db
# binlog-ignore-db  
# replicate-do-db
# replicate-ignore-db

To confirm your setup works for all databases:

-- On Master 1:
CREATE DATABASE test_replication;
USE test_replication;
CREATE TABLE verify (id INT);
INSERT INTO verify VALUES (1);

-- On Master 2:
SELECT * FROM test_replication.verify;

The solution you're implementing works because:

  • Without filters, MySQL replicates ALL binary log events by default
  • New databases automatically get included in the replication stream
  • No configuration changes or restarts are required

While this approach solves your immediate need, consider these factors:

# Recommended additions to my.cnf:
sync_binlog = 1
innodb_flush_log_at_trx_commit = 1
slave_parallel_threads = 0 # For MySQL 5.1 compatibility

If replication breaks after adding databases:

  1. Check slave status with SHOW SLAVE STATUS\G
  2. Verify both masters have matching server-id values
  3. Confirm no temporary network issues occurred

For high-volume environments:

# In my.cnf:
slave_net_timeout = 60
slave_compressed_protocol = 1
skip-slave-start # For manual control