Advanced mysql replication techniques

229
Advanced MySQL Replication Techniques Giuseppe Maxia, QA Director, Continuent, Inc Facts. And Demos. Possibly fun 1 Monday, April 11, 2011

description

Advanced MySQL Replication techniques

Transcript of Advanced mysql replication techniques

Page 1: Advanced mysql replication techniques

Advanced MySQL Replication Techniques

Giuseppe Maxia, QA Director, Continuent, Inc

Facts. And Demos. Possibly fun

1Monday, April 11, 2011

Page 2: Advanced mysql replication techniques

about me - Giuseppe Maxia• a.k.a. The Data Charmer• QA Director at Continuent, Inc• Long time hacking with MySQL features• Formerly, community manager,db consultant, designer,

coder.• A passion for QA and open source• Blogger

•http://datacharmer.blogspot.com

2Monday, April 11, 2011

Page 3: Advanced mysql replication techniques

Agenda• Replication basics

• Replication blues

• Master switch and failover

• Slave lagging

• Gotchas

• Replication power techniques and features

• row-based, semi-synch, delayed

• Circular, bi-directional

• leveraging replication

• Tools and tips

• Replication outside the box

• seamless failover, parallel, multi-master3Monday, April 11, 2011

Page 4: Advanced mysql replication techniques

Replication basics

• Principles

• How to set up replication

• Monitoring replication

AGENDA

4Monday, April 11, 2011

Page 5: Advanced mysql replication techniques

PrinciplesAGEN

DA

5Monday, April 11, 2011

Page 6: Advanced mysql replication techniques

a simple web application

scheme

6

database server

web server

clients

r/w requests

6Monday, April 11, 2011

Page 7: Advanced mysql replication techniques

database server

web servers

load balancer

clients

r/w requests

scaling web requests

77Monday, April 11, 2011

Page 8: Advanced mysql replication techniques

read/writemaster

read/onlyslaves

web servers

R/W

R/O

load balancer

load balancer

clients

a web application

scheme with replication

88Monday, April 11, 2011

Page 9: Advanced mysql replication techniques

master

slaveIO thread

SQL thread

reads

reads

client

transaction

binary log

relay log replication concepts

99Monday, April 11, 2011

Page 10: Advanced mysql replication techniques

replication setupAGEN

DA

10Monday, April 11, 2011

Page 11: Advanced mysql replication techniques

11

1 SHUT DOWN THE DATABASE SERVER

Master

11Monday, April 11, 2011

Page 12: Advanced mysql replication techniques

12

2 MAKE A BACKUP COPY

Master

12Monday, April 11, 2011

Page 13: Advanced mysql replication techniques

13

3 ENABLE THE MASTER

Configuration file[mysqld]log-bin=mysql-binserver-id=1

Master

13Monday, April 11, 2011

Page 14: Advanced mysql replication techniques

14

4 RESTART THE MASTER

Master

14Monday, April 11, 2011

Page 15: Advanced mysql replication techniques

15

5 CREATE REPLICATION USER

SQL commandGRANT REPLICATION SLAVE ON *.* to 'slave_user@'10.10.100.%' IDENTIFIED BY 'slave_pass';

Master

15Monday, April 11, 2011

Page 16: Advanced mysql replication techniques

16

6 INSTALL MySQL on the slave

Slave 1

Make sure that:• You're using the same version of MySQL • You have the same directory structure• The server is not started yet

16Monday, April 11, 2011

Page 17: Advanced mysql replication techniques

17

7 COPY THE MASTER DATA to the slave

Slave 1

17Monday, April 11, 2011

Page 18: Advanced mysql replication techniques

18

8 ENABLE THE SLAVE

Configuration file[mysqld]server-id=2relay-log=mysql-relayread-only# optional:log-bin=mysql-bin

Slave 1

18Monday, April 11, 2011

Page 19: Advanced mysql replication techniques

19

9 START THE SLAVE SERVER

Slave 1

19Monday, April 11, 2011

Page 20: Advanced mysql replication techniques

20

10 INITIALIZE THE SLAVE

SQL commandCHANGE MASTER TOMASTER_HOST=master_IP,MASTER_PORT=3306,MASTER_USER=slave_user,MASTER_PASSWORD='slave_pwd';

Slave 1

20Monday, April 11, 2011

Page 21: Advanced mysql replication techniques

21

11 START THE SLAVE SERVICE

SQL commandSTART SLAVE;

Slave 1

21Monday, April 11, 2011

Page 22: Advanced mysql replication techniques

22

12 CHECK THE SLAVE

SQL commandSHOW SLAVE STATUS \G... Slave_IO_Running: YesSlave_SQL_Running: Yes...

Slave 1

22Monday, April 11, 2011

Page 23: Advanced mysql replication techniques

Troubleshooting

• SHOW SLAVE STATUS says SLAVE_IO_RUNNING=No

• Make sure that the slave host can connect to the master

• Make sure that master and slave have different Server-id

• Check the error log of both master and slave

23Monday, April 11, 2011

Page 24: Advanced mysql replication techniques

Testing the slave

• Create a table in the master.

• Make sure that the slave has replicated the table.

• Insert data in the master

• read that data in the slave

24Monday, April 11, 2011

Page 25: Advanced mysql replication techniques

MonitoringAGEN

DA

25Monday, April 11, 2011

Page 26: Advanced mysql replication techniques

26

Sample monitoring

master

slave

Get master binlog and position

get slave status

Running?

Yes

No

alert

Same or later binlog/position?

check table contents

NoYes

26Monday, April 11, 2011

Page 28: Advanced mysql replication techniques

show master status File: mysql-bin.000002

Position: 78045744

Binlog_Do_DB:

Binlog_Ignore_DB:

28Monday, April 11, 2011

Page 29: Advanced mysql replication techniques

show slave status Slave_IO_State: Waiting for master to send event

Master_Host: 127.0.0.1

Master_User: rsandbox

Master_Port: 27371

Connect_Retry: 60

Master_Log_File: mysql-bin.000002

Read_Master_Log_Pos: 78045744

Relay_Log_File: mysql_sandbox27372-relay-bin.000055

Relay_Log_Pos: 78045889

Relay_Master_Log_File: mysql-bin.000002

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

29Monday, April 11, 2011

Page 30: Advanced mysql replication techniques

show slave status ...

Replicate_Do_DB:

Replicate_Ignore_DB:

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

...

30Monday, April 11, 2011

Page 31: Advanced mysql replication techniques

show slave status...

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 78045744

Relay_Log_Space: 78046100

...

Seconds_Behind_Master: 0

...

Last_IO_Errno: 0

Last_IO_Error:

Last_SQL_Errno: 0

Last_SQL_Error:

31Monday, April 11, 2011

Page 32: Advanced mysql replication techniques

Replication blues

• Master switch and failover

• Slave lagging

• Gotchas

AGENDA

32Monday, April 11, 2011

Page 33: Advanced mysql replication techniques

Master switch and failoverAGEN

DA

33Monday, April 11, 2011

Page 34: Advanced mysql replication techniques

34

Replacing the master

Master crashes

Let all slaves catch up with

execution

FIND the most up to date slave

STOP replication in

all slaves

make it the master

Stop

FIND which transactions are

missing from other slaves

connect all slaves to the new master

run missing transactions

to other slaves

34Monday, April 11, 2011

Page 35: Advanced mysql replication techniques

Planned master switch

• Stop accepting writes

• Wait until all slaves have caught up

• Stop replication in all slaves

• Promote a slave to master

• Point all slaves to the new master

35Monday, April 11, 2011

Page 36: Advanced mysql replication techniques

Changing a failed master

• Pre-requisite:

• log_bin and log_slave_updates must be enabled in all the slaves

• If not, there is more manual labor

36Monday, April 11, 2011

Page 37: Advanced mysql replication techniques

Changing a failed master (1)

• Wait until all slaves have caught up

• Identify the most advanced slave

• Make that slave the new master

• ... so far, so good

37Monday, April 11, 2011

Page 38: Advanced mysql replication techniques

Changing a failed master (2)• For each remaining slave:

• Find the missing statements

• Find the LAST statement replicated by the slave

• Find the same statement in the new master binlog (*)

• get the position of the NEXT statement

(*) if log_slave_updates was not enabled, you need to convert the relay log statements to SQL and do the next step manually

38Monday, April 11, 2011

Page 39: Advanced mysql replication techniques

Changing a failed master (3)

• For each remaining slave:

• Apply the missing statements

• CHANGE MASTER TO master_host=”new_master_hostname”, master_port=new_master_port, master_log_file=”mysql-bin.xxxxxx”, master_log_pos=YYYY

39Monday, April 11, 2011

Page 40: Advanced mysql replication techniques

Reasons for complexity

• No global transaction ID

40Monday, April 11, 2011

Page 41: Advanced mysql replication techniques

What is a global transaction ID

• A unique identifier of a transaction

• Unique for the whole cluster, not for each node

• Generated by the ultimate source (the master)

• Does not change when the transaction goes through an intermediate master

41Monday, April 11, 2011

Page 42: Advanced mysql replication techniques

Why should you care

• Failure recovery

• MySQL DOES NOT have it

42Monday, April 11, 2011

Page 43: Advanced mysql replication techniques

Slave 3

Defining the problem

Master

Binary log

Transactions--------------------------------

Binary log

Transactions================================

Replication

Relay log

Transactions--------------------------------

Slave 2

Slave 1

43Monday, April 11, 2011

Page 44: Advanced mysql replication techniques

Slave 3

The master crashes

Master

Binary log

Transactions--------------------------------

Binary log

Transactions================================

Replication

Relay log

Transactions--------------------------------

Slave 2

Slave 1

44Monday, April 11, 2011

Page 45: Advanced mysql replication techniques

Slave 3

Where do the slaves stand?

Binary log

Transactions================================

Slave 2Slave 1

Binary log

Transactions================================

Binary log

Transactions================================

45Monday, April 11, 2011

Page 46: Advanced mysql replication techniques

New master

Slave 3 becomes master

Binary log

Transactions================================

Slave 2Slave 1

Binary log

Transactions================================

Binary log

Transactions================================

46Monday, April 11, 2011

Page 47: Advanced mysql replication techniques

New master

Need to synch the missing transactions

Binary log

Transactions================================

Slave 2Slave 1

Binary log

Transactions================================

Binary log

Transactions================================

47Monday, April 11, 2011

Page 48: Advanced mysql replication techniques

New master

Transaction position in the new master binlog

Binary log

Transactions================================

Slave 2Slave 1

Binary log

Transactions================================

Binary log

Transactions================================

??

48Monday, April 11, 2011

Page 49: Advanced mysql replication techniques

An existing solution

• Google has made a patch to implement global transaction IDs

• HOWEVER

• It only works with 5.0

• It doesn't work with row based replication

49Monday, April 11, 2011

Page 50: Advanced mysql replication techniques

Hack ahead!

50Monday, April 11, 2011

Page 51: Advanced mysql replication techniques

CREATE TABLE Global_Trans_ID (

number INT UNSIGNED AUTO_INCREMENT PRIMARY KEY

) ENGINE = MyISAM;

CREATE TABLE Last_Exec_Trans (

server_id INT UNSIGNED,

trans_id INT UNSIGNED

) ENGINE = InnoDB;

51Monday, April 11, 2011

Page 52: Advanced mysql replication techniques

CREATE PROCEDURE commit_trans ()

BEGIN

DECLARE trans_id, server_id INT UNSIGNED;

SET SQL_LOG_BIN = 0;

INSERT INTO global_trans_id() values ();

SELECT LAST_INSERT_ID() INTO trans_id,

@@server_id INTO server_id;

DELETE FROM global_trans_id where number < trans_Id;

SET SQL_LOG_BIN = 1;

INSERT INTO last_exec_trans(server_id, trans_id)

VALUES (server_id, trans_id);

COMMIT;

END52Monday, April 11, 2011

Page 53: Advanced mysql replication techniques

Hacking the hack

53Monday, April 11, 2011

Page 54: Advanced mysql replication techniques

Simplifying the scheme

• No MyISAM table and LAST_INSERT_ID()

• Use UUID_SHORT() instead

• Replace the InnoDB table with a BlackHole one

54Monday, April 11, 2011

Page 55: Advanced mysql replication techniques

Advantages

• 10 times faster

• No additional tables and storage required

55Monday, April 11, 2011

Page 56: Advanced mysql replication techniques

Disadvantages

• The master SERVER ID must be < 256

56Monday, April 11, 2011

Page 57: Advanced mysql replication techniques

Another solution

• We’ll see that in the second part of the talk

57Monday, April 11, 2011

Page 58: Advanced mysql replication techniques

Slave laggingAGEN

DA

58Monday, April 11, 2011

Page 59: Advanced mysql replication techniques

Slave lagging

• Common causes:

• Excess of traffic

• Slave restart

• Long DDL operations

59Monday, April 11, 2011

Page 60: Advanced mysql replication techniques

Slave lagging

BINARY LOG

REPLICATION

MySQLDBMS

trans

actio

n

trans

actio

n

trans

actio

n

trans

actio

n

trans

actio

ntra

nsac

tion

trans

actio

ntra

nsac

tion

trans

actio

ntra

nsac

tion

trans

actio

n

MySQLDBMS

transactiontransaction

transactiontransaction

transactiontransaction

transactiontransaction

transactiontransaction

transactiontransaction

MySQL replicationis SINGLE THREAD

60Monday, April 11, 2011

Page 61: Advanced mysql replication techniques

Slave lagging remedies

• row-based replication (helps in some cases)

• Expensive query split

• Double apply (dangerous)

• Slave query prefetch (requires external app)

• Parallel replication (requires external app)

61Monday, April 11, 2011

Page 62: Advanced mysql replication techniques

statement-based replicationmaster> set global binlog_format=‘statement’;master> insert into test.t1 select count(*) from information_schema.columns;master> show global status like 'opened_tables'; +---------------+-------+| Variable_name | Value |+---------------+-------+| Opened_tables | 41 |+---------------+-------+slave> show global status like 'opened_tables'; +---------------+-------+| Variable_name | Value |+---------------+-------+| Opened_tables | 41 |+---------------+-------+

62Monday, April 11, 2011

Page 63: Advanced mysql replication techniques

row-based replicationmaster> set global binlog_format=‘row’;master> insert into test.t1 select count(*) from information_schema.columns;master> show global status like 'opened_tables'; +---------------+-------+| Variable_name | Value |+---------------+-------+| Opened_tables | 41 |+---------------+-------+slave> show global status like 'opened_tables'; +---------------+-------+| Variable_name | Value |+---------------+-------+| Opened_tables | 17 |+---------------+-------+

63Monday, April 11, 2011

Page 64: Advanced mysql replication techniques

Expensive query split# instead of this:insert into t1 select benchmark(10000000, sha('abc'));Query OK, 1 row affected (4.19 sec)Records: 1 Duplicates: 0 Warnings: 0

# you may do thisset @result=(select benchmark(10000000, sha('abc')));Query OK, 0 rows affected (4.19 sec)

insert into t1 values (@result);Query OK, 1 row affected (0.00 sec)

64Monday, April 11, 2011

Page 65: Advanced mysql replication techniques

Expensive query split# at 471#110410 8:21:21 server id 1 end_log_pos 518 User_varSET @`result`:=0/*!*/;# at 518#110410 8:21:21 server id 1 end_log_pos 612 Query thread_id=3 exec_time=0 error_code=0SET TIMESTAMP=1302448881/*!*/;insert into t1 values (@result)

65Monday, April 11, 2011

Page 66: Advanced mysql replication techniques

Slave query prefetch

• What is it?

• a technique that fetches queries from the relay logs, and runs them as SELECT statements, to warm up the indexes

• How do you do it?

• mk-slave-prefetch (http://www.maatkit.org)

• Slave readahead http://sourceforge.net/projects/slavereadahead/

66Monday, April 11, 2011

Page 67: Advanced mysql replication techniques

GotchasAGEN

DA

67Monday, April 11, 2011

Page 68: Advanced mysql replication techniques

Gotchas

• When you thought that you had figured out everything ...

68Monday, April 11, 2011

Page 69: Advanced mysql replication techniques

Gotchas: LOAD DATA

• LOAD DATA INFILE is replicated as a temporary file

• The slave needs three times the size of the data:

• the relay log

• the temporary file

• the data itself

69Monday, April 11, 2011

Page 70: Advanced mysql replication techniques

Gotchas: default engine

• If you define storage_engine in the master, it does not get replicated

70Monday, April 11, 2011

Page 71: Advanced mysql replication techniques

Gotchas: binlog format

• In circular replication, and relay slaves

• Changes to binlog_format are not propagated

71Monday, April 11, 2011

Page 72: Advanced mysql replication techniques

Gotchas: binlog format

server id: 101format: statement

server id: 102format: row

server id: 103format: row

insert into t1 values (@@server_id) 101

102

102

72Monday, April 11, 2011

Page 73: Advanced mysql replication techniques

Gotchas: set global not really global

• set global binlog_format=row

• Works for all new connections after the change

• It does not work for the event scheduler!

73Monday, April 11, 2011

Page 74: Advanced mysql replication techniques

Gotchas: triggers and row replication

• In statement based replication, triggers are fired both on master and slave

• in row-based replication, triggers are fired on master only, and the resulting rows are moved to the slave

74Monday, April 11, 2011

Page 75: Advanced mysql replication techniques

Replication power techniques and features

• row-based

• semi-synch

• delayed

• Circular

• bi-directional

• Leveraging replication

AGENDA

75Monday, April 11, 2011

Page 76: Advanced mysql replication techniques

row-based replicationAGEN

DA

76Monday, April 11, 2011

Page 77: Advanced mysql replication techniques

row-based replication

• Available in 5.1 and later

• can be changed using “binlog_format”

• row

• mixed

• statement (default)

77Monday, April 11, 2011

Page 78: Advanced mysql replication techniques

row-based replication

• DO

• when you have expensive queries in the master

• when you are using non-deterministic functions

• DON’T

• When you are updating a lot of rows

78Monday, April 11, 2011

Page 79: Advanced mysql replication techniques

semi-synch replicationAGEN

DA

79Monday, April 11, 2011

Page 80: Advanced mysql replication techniques

semi-synchronous replication

• Available in 5.5 and higher

• Makes sure that at least one slave has copied the data.

• Increases reliability

80Monday, April 11, 2011

Page 81: Advanced mysql replication techniques

81

transaction with regular replication

clientmaster

slave

commit

binary log

execute

returns to client

replication

1

2

34

581Monday, April 11, 2011

Page 82: Advanced mysql replication techniques

82

transaction with semi-

synchronous replication

clientmaster

slave

commit

binary log

execute

returns to client

sends transaction

to slave

gets acknowledgement

relay log

1

2

3

4 5

6

7

82Monday, April 11, 2011

Page 83: Advanced mysql replication techniques

semi-synchronous replication in practice

• installation:

• it’s a plugin.

• Actually, two plugins

83Monday, April 11, 2011

Page 84: Advanced mysql replication techniques

semi-synch replication install# in the masterplugin-load=rpl_semi_sync_master=semisync_master.sorpl_semi_sync_master_enabled=1

# in each slaveplugin-load=rpl_semi_sync_slave=semisync_slave.sorpl_semi_sync_slave_enabled=1

# restar all servers

84Monday, April 11, 2011

Page 85: Advanced mysql replication techniques

semi-synch replication check# in the mastershow variables like 'rpl_semi%';+------------------------------------+-------+| Variable_name | Value |+------------------------------------+-------+| rpl_semi_sync_master_enabled | ON || rpl_semi_sync_master_timeout | 10000 || rpl_semi_sync_master_trace_level | 32 || rpl_semi_sync_master_wait_no_slave | ON |+------------------------------------+-------+

85Monday, April 11, 2011

Page 86: Advanced mysql replication techniques

semi-synch replication checkshow status like "rpl_semi_%tx";+-----------------------------+-------+| variable_name | value |+-----------------------------+-------+| RPL_SEMI_SYNC_MASTER_NO_TX | 0 || RPL_SEMI_SYNC_MASTER_YES_TX | 0 |+-----------------------------+-------+

86Monday, April 11, 2011

Page 87: Advanced mysql replication techniques

semi-synch replication testmaster> create table t1 ( i int);Query OK, 0 rows affected (0.01 sec)

master> show status like "rpl_semi_%tx";+-----------------------------+-------+| Variable_name | Value |+-----------------------------+-------+| Rpl_semi_sync_master_no_tx | 0 || Rpl_semi_sync_master_yes_tx | 1 |+-----------------------------+-------+

87Monday, April 11, 2011

Page 88: Advanced mysql replication techniques

disabling semi-synch# for each slave

set global rpl_semi_sync_slave_enabled=0;stop slave io_thread; start slave io_thread;

88Monday, April 11, 2011

Page 89: Advanced mysql replication techniques

disabled semi-synch replication testmaster> insert into t1 values (1);Query OK, 1 row affected (10.00 sec)

master> show status like "rpl_semi_%tx";+-----------------------------+-------+| Variable_name | Value |+-----------------------------+-------+| Rpl_semi_sync_master_no_tx | 1 || Rpl_semi_sync_master_yes_tx | 1 |+-----------------------------+-------+2 rows in set (0.00 sec)

89Monday, April 11, 2011

Page 90: Advanced mysql replication techniques

disabled semi-synch replication testmaster> insert into t1 values (2);Query OK, 1 row affected (0.01 sec)

master> show status like "rpl_semi_%tx";+-----------------------------+-------+| Variable_name | Value |+-----------------------------+-------+| Rpl_semi_sync_master_no_tx | 2 || Rpl_semi_sync_master_yes_tx | 1 |+-----------------------------+-------+2 rows in set (0.00 sec)

90Monday, April 11, 2011

Page 91: Advanced mysql replication techniques

re-enabling semi-synch# in one slave

set global rpl_semi_sync_slave_enabled=1;stop slave io_thread; start slave io_thread;

91Monday, April 11, 2011

Page 92: Advanced mysql replication techniques

reenabled semi-synch replication testmaster> insert into t1 values (3);Query OK, 1 row affected (0.01 sec)

master> show status like "rpl_semi_%tx";+-----------------------------+-------+| Variable_name | Value |+-----------------------------+-------+| Rpl_semi_sync_master_no_tx | 2 || Rpl_semi_sync_master_yes_tx | 2 |+-----------------------------+-------+2 rows in set (0.00 sec)

92Monday, April 11, 2011

Page 93: Advanced mysql replication techniques

delayed replicationAGEN

DA

93Monday, April 11, 2011

Page 94: Advanced mysql replication techniques

delayed replication in practiceSTOP SLAVE;change master to master_delay=60;START SLAVE;

94Monday, April 11, 2011

Page 95: Advanced mysql replication techniques

delayed replication

• DEMO

95Monday, April 11, 2011

Page 96: Advanced mysql replication techniques

Circular replicationAGEN

DA

96Monday, April 11, 2011

Page 97: Advanced mysql replication techniques

CircularMySQLDBMS

MySQLDBMS

MySQLDBMS

MySQLDBMS

A B

CD

mastermaster

mastermaster

A

C

B

D

97Monday, April 11, 2011

Page 98: Advanced mysql replication techniques

bi-directional replicationAGEN

DA

98Monday, April 11, 2011

Page 99: Advanced mysql replication techniques

bi-directional replication

MySQLDBMS

MySQLDBMS

99Monday, April 11, 2011

Page 100: Advanced mysql replication techniques

bi-directional replication

MySQLDBMS MySQL

DBMS

MySQLDBMS

MySQLDBMS

Los Angeles New York

MySQLDBMS

MySQLDBMS

100Monday, April 11, 2011

Page 101: Advanced mysql replication techniques

Leveraging replicationAGEN

DA

101Monday, April 11, 2011

Page 102: Advanced mysql replication techniques

Overview of MySQL partitions

102Monday, April 11, 2011

Page 103: Advanced mysql replication techniques

Partition pruning 1a - unpartitioned table - SINGLE RECORD

select * from table_name where colx = 120

INDEX

DATA

103Monday, April 11, 2011

Page 104: Advanced mysql replication techniques

Partition pruning 2a - table partitioned by colx - SINGLE REC

select * from table_name where colx = 120

100-199

1-99

200-299

300-399

400-499

500-599

104Monday, April 11, 2011

Page 105: Advanced mysql replication techniques

Partition pruning 1a - unpartitioned table - RANGE

INDEX

DATA

select * from table_name where colx between 120 and 230

105Monday, April 11, 2011

Page 106: Advanced mysql replication techniques

Partition pruning 2a - table partitioned by colx - RANGE

100-199

1-99

200-299

300-399

400-499

500-599

select * from table_name where colx between 120 and 230

106Monday, April 11, 2011

Page 107: Advanced mysql replication techniques

Storage comparison

engine storage (GB)

innodb (with PK) 330myisam (with PK) 141archive 13innodb partitioned (no PK) 237myisam partitioned (no PK) 107archive partitioned 13

107Monday, April 11, 2011

Page 108: Advanced mysql replication techniques

Benchmarking resultsengine 6 month rangeInnoDB 4 min 30sMyISAM 25.03sArchive 22 min 25sInnoDB partitioned by month 13.19MyISAM partitioned by year 6.31MyISAM partitioned by month 4.45Archive partitioned by year 16.67Archive partitioned by month 8.97

108Monday, April 11, 2011

Page 109: Advanced mysql replication techniques

Partitions limits§ Little concurrency§ Need of using the partitioning column

109Monday, April 11, 2011

Page 110: Advanced mysql replication techniques

The ARCHIVE storage engine

110Monday, April 11, 2011

Page 111: Advanced mysql replication techniques

Size matters

Innodb

Archive

111Monday, April 11, 2011

Page 112: Advanced mysql replication techniques

large data

REPLICATION PARTITIONING

ARCHIVE

112Monday, April 11, 2011

Page 113: Advanced mysql replication techniques

Replication =

let the slave do the dirty work

113Monday, April 11, 2011

Page 114: Advanced mysql replication techniques

master

slaves

STOP SLAVE

remove slave from load balancer

START SLAVE

perform backup

attach slave to load

balancer

Let slave catch up

backup

114Monday, April 11, 2011

Page 115: Advanced mysql replication techniques

master

slaves

STOP SLAVE

remove slave from load balancer

START SLAVE

calculate summary

tables

attach slave to load

balancer

Let slave catch up

makesummary

tables

115Monday, April 11, 2011

Page 116: Advanced mysql replication techniques

master

slave

innodbnon partitioned

slave

innodbnon partitionedinnodb

partitioned by range

slave

MyISAMpartitioned by range

Partitionsfor heavy statistics

116Monday, April 11, 2011

Page 117: Advanced mysql replication techniques

master

slave

innodbnon partitioned

slave

innodbnon partitioned

ARCHIVEpartitioned by range

(date)

slave

ARCHIVEpartitioned by range

(product)

slave

ARCHIVEpartitioned by range

(location)

Simulating multiple

dimensions

117Monday, April 11, 2011

Page 118: Advanced mysql replication techniques

Tools and tips

• MySQL Sandbox

• Replaying binary logs

• Filtering binary logs

• Baron Schwartz’s Maatkit

• Shlomi Noach’s openark kit

• Facebook’s Online Schema Change

• Measuring replication speed

• Detecting if replication is on

AGENDA

118Monday, April 11, 2011

Page 119: Advanced mysql replication techniques

MySQL SandboxAGEN

DA

119Monday, April 11, 2011

Page 120: Advanced mysql replication techniques

MySQL Sandbox

• Free software (Perl under GPL)

• One (unix) host

• Many database servers

• Single or multiple sandboxes

• Customized scripts to use the servers

• Standard or circular replication

• Installs IN SECONDS

http://mysqlsandbox.net

120Monday, April 11, 2011

Page 121: Advanced mysql replication techniques

overview

MySQLserver

MySQLserver

Data DB1

DB2 DB3

Data DB1

DB2 DB3DATA DIRECTORY

PORT

SOCKET

121Monday, April 11, 2011

Page 122: Advanced mysql replication techniques

overview

MySQLserver

MySQLserver

Data DB1

DB2 DB3

Data DB1

DB2 DB3

SAME DATA

DIRECTORY?

DATA CORRUPTION

/var/lib/mysql /var/lib/mysql

122Monday, April 11, 2011

Page 123: Advanced mysql replication techniques

overview

MySQLserver

MySQLserver

SAME PORT or SOCKET?

DOES NOT START

/tmp/mysql.sock /tmp/mysql.sock

33063306

123Monday, April 11, 2011

Page 124: Advanced mysql replication techniques

The hard way (1)

124Monday, April 11, 2011

Page 125: Advanced mysql replication techniques

the hard way (2)

125Monday, April 11, 2011

Page 126: Advanced mysql replication techniques

The easy way$ make_sandbox \

/path/to/mysql-5.1.54_linux.tar.gz

# it should work always

126Monday, April 11, 2011

Page 127: Advanced mysql replication techniques

The easier way$ make_sandbox 5.1.54

# Needs some preliminary work

127Monday, April 11, 2011

Page 128: Advanced mysql replication techniques

The easiest way$ sb 5.1.54

# Needs the same preliminary work

128Monday, April 11, 2011

Page 129: Advanced mysql replication techniques

MySQL Sandbox

MySQLserver

VERSION

$SANDBOX_HOME/msb_VERSION/dataData DB1

DB2 DB3

VERSION

/tmp/mysql_VERSION.sock

129Monday, April 11, 2011

Page 130: Advanced mysql replication techniques

MySQL Sandbox

MySQLserver

5.1.54

$SANDBOX_HOME/msb_5_1_54/dataData DB1

DB2 DB3

5154

/tmp/mysql_5154.sock

130Monday, April 11, 2011

Page 131: Advanced mysql replication techniques

MySQL Sandbox

MySQLserver

5.5.9

$SANDBOX_HOME/msb_5_5_09/dataData DB1

DB2 DB3

5509

/tmp/mysql_5509.sock

131Monday, April 11, 2011

Page 132: Advanced mysql replication techniques

Single SandboxMySQLserver

customized scripts

startstop

restartstatusclear

send_killuse

132Monday, April 11, 2011

Page 133: Advanced mysql replication techniques

Multiple SandboxMySQLserver

customized scripts

start_allstop_all

restart_allstatus_allclear_all

send_kill_all

use_all

ms1s2

n1n2n3

133Monday, April 11, 2011

Page 134: Advanced mysql replication techniques

Where do you get it

•from CPANsudo cpan MySQL::Sandbox

•from launchpadhttp://launchpad.net/mysql-sandbox

134Monday, April 11, 2011

Page 135: Advanced mysql replication techniques

The easy replication way$ make_replication_sandbox \

/path/to/mysql-5.1.54_linux.tar.gz

# or, after some preparation

$ make_replication_sandbox 5.1.54

135Monday, April 11, 2011

Page 136: Advanced mysql replication techniques

default architecture

$HOME

/sandboxes opt

mysql

$SANDBOX_HOME

$SANDBOX_BINARY

expandedtarballs

installed sandboxes

136Monday, April 11, 2011

Page 137: Advanced mysql replication techniques

default architecture

$HOME

/sandboxes opt

mysql

5.0.91

5.1.45

5.1.48

5.5.4

msb_5_0_91

msb_5_1_48

rsandbox_5_1_48

master

node1

node2137Monday, April 11, 2011

Page 138: Advanced mysql replication techniques

138Monday, April 11, 2011

Page 139: Advanced mysql replication techniques

creating a single sanbox

make_sandbox \ /path/to/mysql-X.X.XX-OS.tar.gz

139Monday, April 11, 2011

Page 140: Advanced mysql replication techniques

using a single sanbox

# after # make_sandbox \# /path/to/mysql-X.X.XX-OS.tar.gz

$ cd $SANDBOX_HOME/msb_X_X_XX$ ./use

140Monday, April 11, 2011

Page 141: Advanced mysql replication techniques

creating a single sanboxwith a specific options file

make_sandbox \ /path/to/mysql-X.X.XX-OS.tar.gz \ --my_file=/path/to/my.cnf

141Monday, April 11, 2011

Page 142: Advanced mysql replication techniques

easily create a sandbox after the first one

$ cd $HOME/opt/mysql$ gunzip -c \ /path/to/mysql-5.1.34-osx10.5-x86.tar.gz \ | tar -xf -$ mv mysql-5.1.34-osx10.5-x86 5.1.34$ make sandbox 5.1.34

The long way

# $SANDBOX_BINARY

142Monday, April 11, 2011

Page 143: Advanced mysql replication techniques

easily create a sandbox after the first one

$ make_sandbox \ path/to/mysql-5.1.34-osx10.5-x86.tar.gz \ --export_binaries

The short way

143Monday, April 11, 2011

Page 144: Advanced mysql replication techniques

starting a single sanbox

$ cd $SANDBOX_HOME/msb_X_X_XX$ ./start

144Monday, April 11, 2011

Page 145: Advanced mysql replication techniques

starting a single sanboxwith temporary options

$ cd $SANDBOX_HOME/msb_X_X_XX$ ./start --option=value

$ ./restart --option=value

$ ./start --key-buffer=20000000

145Monday, April 11, 2011

Page 146: Advanced mysql replication techniques

creating a sandbox with custom port and directory

$ make_sandbox 5.1.34 \ --sandbox_port=7800 \ --sandbox_directory=mickeymouse

146Monday, April 11, 2011

Page 147: Advanced mysql replication techniques

creating a sandbox with automatic port checking

$ make_sandbox 5.1.34 --check_port

# if 5.1.34 is free# port=5134# directory=msb_5_1_34# else# port=5135 (or the first free)# directory=msb_5_1_34_a

147Monday, April 11, 2011

Page 148: Advanced mysql replication techniques

create a replication sandbox

$ make_replication_sandbox \ path/to/mysql-5.1.34-osx10.5-x86.tar.gz

148Monday, April 11, 2011

Page 149: Advanced mysql replication techniques

create a circular replication sandbox

$ make_replication_sandbox \ --circular=4 \ path/to/mysql-5.1.34-osx10.5-x86.tar.gz

149Monday, April 11, 2011

Page 150: Advanced mysql replication techniques

changing port to an existing sandbox

$ sbtool -o port \ -s /path/to/source/sandbox \ --new_port=XXXX

150Monday, April 11, 2011

Page 151: Advanced mysql replication techniques

installing the innodb plugin

$ sbtool -o plugin \ --plugin=innodb \ -s /path/to/source/sandbox

151Monday, April 11, 2011

Page 152: Advanced mysql replication techniques

creating a replication sandbox with new base port

$ make_replication_sandbox \ --replication_directory=newwdir \ --check_base_port 5.0.79

# Creates a replication directory under# $SANDBOX_HOME/newdir# The previous one is preserved. # No conflicts happen

152Monday, April 11, 2011

Page 153: Advanced mysql replication techniques

Replaying binary logsAGEN

DA

153Monday, April 11, 2011

Page 154: Advanced mysql replication techniques

Filtering binary logsAGEN

DA

154Monday, April 11, 2011

Page 155: Advanced mysql replication techniques

MaatkitAGEN

DA

155Monday, April 11, 2011

Page 156: Advanced mysql replication techniques

maatkit• mk-heartbeat Monitor MySQL replication delay.

• mk-purge-logs Purge binary logs on a master based on purge rules.

• mk-slave-delay Make a MySQL slave server lag behind its master.

• mk-slave-find Find and print replication hierarchy tree of MySQL slaves.

• mk-slave-move Move a MySQL slave around in the replication hierarchy.

• mk-slave-prefetch Pipeline relay logs on a MySQL slave to pre-warm caches.

• mk-slave-restart Watch and restart MySQL replication after errors.

• mk-table-checksum Perform an online replication consistency check, or checksum MySQL tables efficiently on one or many servers.

• mk-table-sync Synchronize MySQL table data efficiently.

• http://www.maatkit.org/

156Monday, April 11, 2011

Page 157: Advanced mysql replication techniques

Facebook Online Schema Change

AGENDA

157Monday, April 11, 2011

Page 158: Advanced mysql replication techniques

Facebook Online Schema Change

• Based on one of Shlomi Noach OpenArk utilities

• Developed in PHP by Facebook

• http://www.facebook.com/note.php?note_id=430801045932

158Monday, April 11, 2011

Page 159: Advanced mysql replication techniques

OpenArkAGEN

DA

159Monday, April 11, 2011

Page 161: Advanced mysql replication techniques

Check replication speedAGEN

DA

161Monday, April 11, 2011

Page 162: Advanced mysql replication techniques

check replication speed

• Write a time to the master

• Retrieve it from the slave

• Compare times

• http://forge.mysql.com/tools/tool.php?id=5

162Monday, April 11, 2011

Page 163: Advanced mysql replication techniques

Replication outside the box

• Seamless failover

• Parallel replication

• Multiple source replication

• Multiple master replication

AGENDA

163Monday, April 11, 2011

Page 164: Advanced mysql replication techniques

Advanced MySQL Replication for the

masses

164Monday, April 11, 2011

Page 165: Advanced mysql replication techniques

Once Upon A Time, In The Life Of A

Database Consultant ...

165Monday, April 11, 2011

Page 166: Advanced mysql replication techniques

The story of a steel foundry

• Used MySQL databases to store production monitoring data

• Inserted a zillion records per second.

• Slaves often lagged behind

166Monday, April 11, 2011

Page 167: Advanced mysql replication techniques

BINARY LOG

REPLICATION

MySQLDBMS

trans

actio

n

trans

actio

n

trans

actio

n

trans

actio

n

trans

actio

ntra

nsac

tion

trans

actio

ntra

nsac

tion

trans

actio

ntra

nsac

tion

trans

actio

n

MySQLDBMS

transactiontransaction

transactiontransaction

transactiontransaction

transactiontransaction

transactiontransaction

transactiontransaction

167Monday, April 11, 2011

Page 168: Advanced mysql replication techniques

The story of a shoe maker

• Had a successful business, spread to a dozen stores.

• Needed to aggregate the data from the stores in his headquarters database.

168Monday, April 11, 2011

Page 169: Advanced mysql replication techniques

MySQLDBMS MySQL

DBMS

MySQLDBMS

MySQLDBMS

headquarters

store storestore

169Monday, April 11, 2011

Page 170: Advanced mysql replication techniques

The story of a widgets seller

• Had a successful business, designed for one server.

• Products were created in several sites.

• Needed to allow insertions from more than one master at the time

170Monday, April 11, 2011

Page 171: Advanced mysql replication techniques

MySQLDBMS

MySQLDBMS

MySQLDBMS

MySQLDBMS

mastermaster

master master

171Monday, April 11, 2011

Page 172: Advanced mysql replication techniques

All these stories tell us:

Nice dream, but MySQL

can’t do it172Monday, April 11, 2011

Page 173: Advanced mysql replication techniques

Enter Tungsten Replicator

173Monday, April 11, 2011

Page 174: Advanced mysql replication techniques

Tungsten Replicator 2.0

• What is it?

174Monday, April 11, 2011

Page 175: Advanced mysql replication techniques

http://code.google.com/p/tungsten-replicator

100% GPL v2Open Source

175Monday, April 11, 2011

Page 176: Advanced mysql replication techniques

What can it do?

• Easy failover

• Multiple masters

• Multiple sources to a single slave

• Parallel replication

• Replicate to Oracle and PostgreSQL database

176Monday, April 11, 2011

Page 177: Advanced mysql replication techniques

© Continuent 2010

MySQL to foreign services

BinLogs

Master DB

(Binlogs enabled) Slave DB

MySQL setup to run as

MySQL master

Data is applied to PostgreSQL or

Oracle

177Monday, April 11, 2011

Page 178: Advanced mysql replication techniques

From the beginning ...

178Monday, April 11, 2011

Page 179: Advanced mysql replication techniques

Fail-over (1)

179Monday, April 11, 2011

Page 180: Advanced mysql replication techniques

Fail-over (2)

180Monday, April 11, 2011

Page 181: Advanced mysql replication techniques

Fail-over (3)

181Monday, April 11, 2011

Page 182: Advanced mysql replication techniques

Fail-over (4)

182Monday, April 11, 2011

Page 183: Advanced mysql replication techniques

Fail-over (5)

183Monday, April 11, 2011

Page 184: Advanced mysql replication techniques

Fail-over (6)

184Monday, April 11, 2011

Page 185: Advanced mysql replication techniques

Fail-over (7)

185Monday, April 11, 2011

Page 186: Advanced mysql replication techniques

Fail-over (8)

186Monday, April 11, 2011

Page 187: Advanced mysql replication techniques

Failover

• DEMO

187Monday, April 11, 2011

Page 188: Advanced mysql replication techniques

master/slave with an attitude

188Monday, April 11, 2011

Page 189: Advanced mysql replication techniques

The steel foundry dreamor parallel replication

From here ...

189Monday, April 11, 2011

Page 190: Advanced mysql replication techniques

The steel foundry dreamor parallel replication

To here.

190Monday, April 11, 2011

Page 191: Advanced mysql replication techniques

Parallel replication facts

• Sharded by database

• Good choice for slave lag problems

• Bad choice for single database projects

191Monday, April 11, 2011

Page 192: Advanced mysql replication techniques

Testing parallel replication

192Monday, April 11, 2011

Page 193: Advanced mysql replication techniques

sysbench

sysbench

sysbench

sysbench

sysbench

sysbench

sysbench

sysbench

sysbench

sysbench

db1

db2

db3

db4

db5

db6

db7

db8

db9

preparation (1)db0

193Monday, April 11, 2011

Page 194: Advanced mysql replication techniques

preparation (2)db0

db1

db2

db3

db4

db5

db6

db7

db8

db9

194Monday, April 11, 2011

Page 195: Advanced mysql replication techniques

before the test (1)db0

db1

db2

db3

db4

db5

db6

db7

db8

db9

195Monday, April 11, 2011

Page 196: Advanced mysql replication techniques

before the test (2)

binary logs

MySQL slave

Tungsten slave

RELAY logs

RELAY logs

IO thread

SQL thread

replicator alpha

direct: alpha(slave)

196Monday, April 11, 2011

Page 197: Advanced mysql replication techniques

starting the test

binary logs

MySQL slave

Tungsten slave

RELAY logs

RELAY logs

IO thread

SQL thread

replicator alpha

direct: alpha(slave)

197Monday, April 11, 2011

Page 198: Advanced mysql replication techniques

MySQL native replication

slave catch up in 00:02:30

198Monday, April 11, 2011

Page 199: Advanced mysql replication techniques

Tungsten parallel replication

slave catch up in 00:01:20

199Monday, April 11, 2011

Page 200: Advanced mysql replication techniques

The widget seller dream, or multi masters

• Tungsten Replicator recipe: use more services

200Monday, April 11, 2011

Page 201: Advanced mysql replication techniques

Bi-directional replication

201Monday, April 11, 2011

Page 202: Advanced mysql replication techniques

Bi-directional replication with slaves

202Monday, April 11, 2011

Page 203: Advanced mysql replication techniques

True multiple master

We’ll see that in a moment.But first

203Monday, April 11, 2011

Page 204: Advanced mysql replication techniques

The shoe maker dream, or multiple sources

• Tungsten Replicator recipe is still valid: use more services

204Monday, April 11, 2011

Page 205: Advanced mysql replication techniques

Multiple source replication

205Monday, April 11, 2011

Page 206: Advanced mysql replication techniques

Multiple masters replication: 3 nodes

206Monday, April 11, 2011

Page 207: Advanced mysql replication techniques

Multiple masters replication: 4 nodes

207Monday, April 11, 2011

Page 208: Advanced mysql replication techniques

Updating 4 masters : 1 flow

208Monday, April 11, 2011

Page 209: Advanced mysql replication techniques

Updating 4 masters : 2 flows

209Monday, April 11, 2011

Page 210: Advanced mysql replication techniques

Updating 4 masters : 3 flows

210Monday, April 11, 2011

Page 211: Advanced mysql replication techniques

Updating 4 masters : 4 flows

211Monday, April 11, 2011

Page 212: Advanced mysql replication techniques

Tungsten in practice

• installation

212Monday, April 11, 2011

Page 213: Advanced mysql replication techniques

Installation:the long way

• Get the binaries

• Expand the tarball

• Check the requirements in the manual

• Run ./configure

• Run ./configure-service

213Monday, April 11, 2011

Page 214: Advanced mysql replication techniques

Installation:the quick way

• Get the tarball

• Get the tungsten deployer from Google Code

• Sit back and enjoy!

214Monday, April 11, 2011

Page 215: Advanced mysql replication techniques

Tools

• replicator

• trepctl

• thl

215Monday, April 11, 2011

Page 216: Advanced mysql replication techniques

replicator

• It’s the service provider

• You launch it once when you start

• You may restart it when you change config

216Monday, April 11, 2011

Page 217: Advanced mysql replication techniques

trepctl

• Tungsten Replicator ConTroLler

• It’s the driving seat for your replication

• You can start, update, and stop services

• You can get specific info

217Monday, April 11, 2011

Page 218: Advanced mysql replication techniques

thl

• Transaction History List

• Gives you access to the Tungsten relay logs

218Monday, April 11, 2011

Page 219: Advanced mysql replication techniques

More info on replicationAGEN

DA

219Monday, April 11, 2011

Page 220: Advanced mysql replication techniques

Books

220Monday, April 11, 2011

Page 221: Advanced mysql replication techniques

High Performance MySQL

221Monday, April 11, 2011

Page 222: Advanced mysql replication techniques

MySQL High Availability

222Monday, April 11, 2011

Page 223: Advanced mysql replication techniques

Web Operations

223Monday, April 11, 2011

Page 224: Advanced mysql replication techniques

Cloud Application Architectures

224Monday, April 11, 2011

Page 225: Advanced mysql replication techniques

Talks• Monday, 1:30pm “Learn how to cure replication

deprivation with Tungsten”

• Tuesday 10:50am “Replication Update”

• Tuesday 2:00pm “Diagnosing replication failures”

• Tuesday 3:05 “Advanced replication monitoring”

• Thursday 10:50am “Replication for Availability & Durability with MySQL and Amazon RDS”

• Thursday 11:55am “Build your own PaaS for MySQL with Tungsten Enterprise”

• Thursday 2:00pm “Error detection and correction with MySQL Replication”

• Thursday 2:50pm “Performance comparisons and trade-offs for various MySQL replication schemes”

225Monday, April 11, 2011

Page 226: Advanced mysql replication techniques

ADVERTISING

226Monday, April 11, 2011

Page 227: Advanced mysql replication techniques

http://www.continuent.com/about/careers

WE ARE HIRING!

QA and supportengineers

227Monday, April 11, 2011

Page 228: Advanced mysql replication techniques

Open Database Camp

SardiniaTechnology Park

6-7-8 May 2011

http://opendbcamp.org

228Monday, April 11, 2011

Page 229: Advanced mysql replication techniques

© Continuent 2011

Worldwide560 S. Winchester Blvd., Suite 500 San Jose, CA 95128 Tel (866) 998-3642 Fax (408) 668-1009e-mail: [email protected]

Contact Information

Continuent Web Site:http://www.continuent.com

Tungsten Projecthttp://code.google.com/p/tungsten-replicator

229Monday, April 11, 2011