4. Slides
• I will post these slides on the conference site
and slideshare
• No need to copy them down
5. Who Am I?
• MySQL user since 2007
• Radian6: Social Media Monitoring
• Now part of
Salesforce ExactTarget Marketing Cloud
• Large Data Set
• Dozens of Databases
• Replication is CRUCIAL for us
6. Why Am I Doing This?
• Replication isn’t hard
• Show simplest replication case
• Share how we deal with common problems
7. Our First Replication
• Set up a separate database for running
reports, to separate back-end from user-facing
activity
• Alexander Rubin of MySQL Support in 2008
10. Replication Compared
• DRBD
– Block-level
– Replica is unusable until switched to
• Galera / Percona Cluster
– More complicated
– Not WAN-Friendly for High Transaction Rate
• Tungsten Replicator
• Others..?
13. Replication Described
Master
table1
table2
mysql
Update table1 set...
Insert into table2...
Binlogs:
• Events in commit order
• Contains timestamps and auto-increment IDs
client
Replica
table1
table2
IO Thread SQL Thread
Update table1 set...
Insert into table2...
mysql
42. Basic Setup Walk-Through
• Unique server_id values
my.cnf on master
[mysqld]
server_id=1
my.cnf on replica
[mysqld]
server_id=2
43. Basic Setup Walk-Through
• Replication User
On Master:
grant replication slave on *.* to ‘repl’
identified by ‘pass’;
44. Basic Setup Walk-Through
• Point to the correct binlog position
• On master:
show master status;
...once both databases are equal
• On Replica:
change master to ...
45. Basic Setup Walk-Through
• Start it up
• On replica:
start slave;
• Check it
• On replica:
show slave statusG
48. Limiting Tables or Schemas
• If you don’t need all the tables
• In my.cnf:
replicate-do-table=schema.tableName
replicate-skip-table=schema.otherTable
• If you don’t need all the schemas
• In my.cnf
replication-do-db=schemaOne
replication_skip_db=schemaTwo
50. Limiting Columns or Rows
• If you don’t need all columns
• Subset of table for reporting or analytics
51. Limiting Columns or Rows
• Original table
create table x (a int primary key,
b int, c int) engine=InnoDB;
• Blackhole table on replica like master table
create table x (a int, b int, c int)
engine=blackhole;
• Target table on replica
create table y (a int primary key,
b int) engine=InnoDB;
52. Limiting Columns or Rows
• Before-insert trigger on replica to put data into
second “real” table
• Only columns a and b, and only for b=1
create trigger x_bi before insert on x
for each row
insert into y (a, b)
select new.a, new.b
from dual
where new.b=1;
53. Limiting Columns or Rows
(1) Master
table1
table2
mysql
Update table1 set...
Insert into table2...
client
Replica (2)
x y
IO Thread SQL Thread
Update table1 set...
Insert into table2...
mysql
TRG
54. Multi-Level
• Local and Remote DR Site
• Spreading load over many servers
M R M R
M R
R
R
R
R
R
R
R
...
56. Multi-Level
Master Replica
table1
table2
table1
table2
mysql IO Thread SQL Thread
Update table1 set...
Insert into table2...
Update table1 set...
Insert into table2...
Replica
table1
table2
IO Thread SQL Thread
Update table1 set...
Insert into table2...
Update table1 set...
Insert into table2...
mysql
57. Types of Binlogs
• Statement-Based
– Contains the actual statements run
– Data drift is more likely (code modified by
triggers, etc)
• Row-based
– Replicates changes for each row
– Mostly unreadable by humans
– Unaffected by how the change is done (data drift less
likely)
– mysqlbinlog -v
59. Fault: Lag
• Long-running transactions
• High concurrency on master becomes single
session on replica
• show slave statusG
• Easy to alarm on
60. Fault: Stopping from Error
• Any error stops SQL Thread!
• Duplicate Key
• Unknown Function
• slave_exec_mode=idempotent
...suppresses duplicate-key and no-key-found
errors
• show slave statusG
61. Faults: Can’t Connect to Master
• Fault shows in slave status as soon as you
start slave.
• Did you create replication slave user?
• Correct user/password?
• Correct hostname/port?
• Master is running and reachable?
62. Fault: Can’t Find Binlog
• Clearly stated in the slave status
• Can happen if replica has been stopped (or is
lagged) and the binlogs are removed from the
master
• Execute from archive
OR
• Rebuild replica and restart from a known good
binlog position
66. Gotcha: functions
• Functions will be run on the replica even if
only appearing in a select on the master
• Make sure all functions exist on replica
DB, even if only a dummy version
create function myFtn (a int, b int)
returns int deterministic return 0;
67. Gotcha: Reconnection
• When a replica is stopped and reset, until the
database is restarted, replication may
reconnect to a master after “reset slave”
change master to master_port=1234;
68. Gotcha: Master Crash
• Replication becomes unstable if master crashes
• Error: unknown instruction at position xxx
• Often need to use mysqlbinlog to find a
suitable position in the binlog to restart
replication from
stop slave; reset slave;
change master to ...;
start slave;
69. Gotcha: MySQL Versions
• Replication not completely compatible
between versions (especially later=>earlier)
• 5.5 master to 5.1 replica: character set error
“sometimes”
• Stops replication
70. Impossible
• One replica can have only one master
(until 5.7)
• Can’t change table or schema filters
dynamically
• Can’t put binlogs back on master server, for
recovery purposes