scaling your web app with mysql replication

78
Scaling your web app with MySQL replication Giuseppe Maxia MySQL Community Team Lead 1 This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License. 1 Thursday, 30 September 2010

Upload: giuseppe-maxia

Post on 17-May-2015

4.264 views

Category:

Technology


0 download

DESCRIPTION

How to scale from a single server to multiple ones using MySQL built-in replication.

TRANSCRIPT

Page 1: Scaling your web app with MySQL replication

Scaling your web app with MySQL replication

Giuseppe MaxiaMySQL Community Team Lead

1This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.

1Thursday, 30 September 2010

Page 2: Scaling your web app with MySQL replication

2

Table of contents

Why replication

How to set replication

Using replication

Leveraging replication

From single server to

replication

Adding a slave

Replacing a slave

Replacing the master

Managing replication

2Thursday, 30 September 2010

Page 3: Scaling your web app with MySQL replication

3

Table of contents

Why replication

How to set replication

Using replication

Leveraging replication

From single server to

replication

Adding a slave

Replacing a slave

Replacing the master

Managing replication

3Thursday, 30 September 2010

Page 4: Scaling your web app with MySQL replication

a simple web application

scheme

4

database server

web server

clients

r/w requests

4Thursday, 30 September 2010

Page 5: Scaling your web app with MySQL replication

database server

web servers

load balancer

clients

r/w requests

scaling web requests

55Thursday, 30 September 2010

Page 6: Scaling your web app with MySQL replication

writeread

database load on a simple

web application

6

85% 15%

6Thursday, 30 September 2010

Page 7: Scaling your web app with MySQL replication

readwrite

database load on a successful web

application7

20% 80%

7Thursday, 30 September 2010

Page 8: Scaling your web app with MySQL replication

8

database server

web servers

load balancer

clients

r/w requests✘

scaling up means buying

a bigger database

server

8Thursday, 30 September 2010

Page 9: Scaling your web app with MySQL replication

9

readwrite

the bigger database server will eventually

have the same problem

80%20%

9Thursday, 30 September 2010

Page 10: Scaling your web app with MySQL replication

read/writemaster

read/onlyslaves

web servers

R/W

R/O

load balancer

load balancer

clients

a web application

scheme with replication

1010Thursday, 30 September 2010

Page 11: Scaling your web app with MySQL replication

read/writemaster

read/onlyslaves

readwriteread

database load with

replication11

85% 15% 100%

11Thursday, 30 September 2010

Page 12: Scaling your web app with MySQL replication

read/writemaster

read/onlyslaves

readwriteread

scaling database load

with replication

12

85% 15% 100%

12Thursday, 30 September 2010

Page 13: Scaling your web app with MySQL replication

Replication assessment

13

without replication with replication

database handling

performance

Point in Time recovery

failover

write scaling

backup

read scaling

easy harder

high lower (binary logs)

none easy

none possible

none minimal

with downtime without downtime

none easy

13Thursday, 30 September 2010

Page 14: Scaling your web app with MySQL replication

14

Table of contents

Why replication

How to set replication

Using replication

Leveraging replication

From single server to

replication

Adding a slave

Replacing a slave

Replacing the master

Managing replication

14Thursday, 30 September 2010

Page 15: Scaling your web app with MySQL replication

master

slaveIO thread

SQL thread

reads

reads

client

transaction

binary log

relay log replication concepts

1515Thursday, 30 September 2010

Page 16: Scaling your web app with MySQL replication

16

Table of contents

Why replication

How to set replication

Using replication

Leveraging replication

From single server to

replication

Adding a slave

Replacing a slave

Replacing the master

Managing replication

16Thursday, 30 September 2010

Page 17: Scaling your web app with MySQL replication

17

1 SHUT DOWN THE DATABASE SERVER

Master

17Thursday, 30 September 2010

Page 18: Scaling your web app with MySQL replication

18

2 MAKE A BACKUP COPY

Master

18Thursday, 30 September 2010

Page 19: Scaling your web app with MySQL replication

19

3 ENABLE THE MASTER

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

Master

19Thursday, 30 September 2010

Page 20: Scaling your web app with MySQL replication

20

4 RESTART THE MASTER

Master

20Thursday, 30 September 2010

Page 21: Scaling your web app with MySQL replication

21

5 CREATE REPLICATION USER

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

Master

21Thursday, 30 September 2010

Page 22: Scaling your web app with MySQL replication

22

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

22Thursday, 30 September 2010

Page 23: Scaling your web app with MySQL replication

23

7 COPY THE MASTER DATA to the slave

Slave 1

23Thursday, 30 September 2010

Page 24: Scaling your web app with MySQL replication

24

8 ENABLE THE SLAVE

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

Slave 1

24Thursday, 30 September 2010

Page 25: Scaling your web app with MySQL replication

25

9 START THE SLAVE SERVER

Slave 1

25Thursday, 30 September 2010

Page 26: Scaling your web app with MySQL replication

26

10 INITIALIZE THE SLAVE

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

Slave 1

26Thursday, 30 September 2010

Page 27: Scaling your web app with MySQL replication

27

11 START THE SLAVE SERVICE

SQL commandSTART SLAVE;

Slave 1

27Thursday, 30 September 2010

Page 28: Scaling your web app with MySQL replication

28

12 CHECK THE SLAVE

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

Slave 1

28Thursday, 30 September 2010

Page 29: Scaling your web app with MySQL replication

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

2929Thursday, 30 September 2010

Page 30: Scaling your web app with MySQL replication

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

3030Thursday, 30 September 2010

Page 31: Scaling your web app with MySQL replication

31

Table of contents

Why replication

How to set replication

Using replication

Leveraging replication

From single server to

replication

Adding a slave

Replacing a slave

Replacing the master

Managing replication

31Thursday, 30 September 2010

Page 32: Scaling your web app with MySQL replication

32

1 NO NEED TO STOP THE MASTER!

Master

32Thursday, 30 September 2010

Page 33: Scaling your web app with MySQL replication

33

2 STOP THE SLAVE

SQL commandSTOP SLAVE IO_THREAD;# wait until the SQL_THREAD# has done everythingSTOP SLAVE SQL_THREAD;# STOP THE SERVER

Slave 1

33Thursday, 30 September 2010

Page 34: Scaling your web app with MySQL replication

34

3 MAKE A COPY OF THE DATA DIRECTORY

Slave 1

34Thursday, 30 September 2010

Page 35: Scaling your web app with MySQL replication

35

4 RESTART THE SLAVE

Slave 1

35Thursday, 30 September 2010

Page 36: Scaling your web app with MySQL replication

36

5 INSTALL MySQL on the new slave

Slave 2

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

36Thursday, 30 September 2010

Page 37: Scaling your web app with MySQL replication

37

6 COPY THE old slave DATA on the slave

Slave 2

37Thursday, 30 September 2010

Page 38: Scaling your web app with MySQL replication

38

7 ENABLE THE NEW SLAVE

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

Slave 2

must be unique!

38Thursday, 30 September 2010

Page 39: Scaling your web app with MySQL replication

39

8 START THE NEW SLAVE

Slave 2

39Thursday, 30 September 2010

Page 40: Scaling your web app with MySQL replication

40

9 CHECK THE SLAVE

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

Slave 2

40Thursday, 30 September 2010

Page 41: Scaling your web app with MySQL replication

Why it works

• No need to issue a CHANGE MASTER TO command.

• Because we cloned the old slave

• The new slave gets its parameters from the .info files in the data directory

4141Thursday, 30 September 2010

Page 42: Scaling your web app with MySQL replication

42

Table of contents

Why replication

How to set replication

Using replication

Leveraging replication

From single server to

replication

Adding a slave

Replacing a slave

Replacing the master

Managing replication

42Thursday, 30 September 2010

Page 43: Scaling your web app with MySQL replication

From single server application

43

r/w requests

43Thursday, 30 September 2010

Page 44: Scaling your web app with MySQL replication

To replication-aware application

44

read/writemaster

read/onlyslaves

R/WR/O

load balancer

44Thursday, 30 September 2010

Page 45: Scaling your web app with MySQL replication

Single server application$link = mysql_connect(

$server_IP,  'mysql_user',  'mysql_password');$result = mysql_query('INSERT INTO table_name (x) VALUES (1)',$link);$result = mysql_query('SELECT * FROM table_name WHERE x=1',$link);

4545Thursday, 30 September 2010

Page 46: Scaling your web app with MySQL replication

Making an application aware of replication

46

connectreadwrite

IPuserpassword

<<R/W database handling>>

db

connect

IPuserpassword

<<database handling>>db

read

IPuserpassword

<<read-only database handling>>

db

connectreadwrite

IPuserpassword

<<R/W database handling>>

db

46Thursday, 30 September 2010

Page 47: Scaling your web app with MySQL replication

Using replication: the WRONG way

47

R/W split by

statement

Write statement?

Connect to the master

Yes

Write to the master

Stop

No

Connect to the next available

slave

Read from slave

47Thursday, 30 September 2010

Page 48: Scaling your web app with MySQL replication

Why statement split is wrong

• Breaks transactions

• High risk of inconsistency

• Loses or corrupts data

4848Thursday, 30 September 2010

Page 49: Scaling your web app with MySQL replication

Using replication:the RIGHT way

49

R/W split by function

Write function?

Connect to the master

Yes

Read and write from master

Stop

No

Connect to the next available

slave

Read from slave

more queries?

No

Yesmore

queries?

No

Yes

49Thursday, 30 September 2010

Page 50: Scaling your web app with MySQL replication

50

Table of contents

Why replication

How to set replication

Using replication

Leveraging replication

From single server to

replication

Adding a slave

Replacing a slave

Replacing the master

Managing replication

50Thursday, 30 September 2010

Page 51: Scaling your web app with MySQL replication

Managing replication

•MONITORING• … or die

5151Thursday, 30 September 2010

Page 52: Scaling your web app with MySQL replication

52

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

52Thursday, 30 September 2010

Page 53: Scaling your web app with MySQL replication

53

monitoringcontents

GET table CRC

GET table CRC

GET table CRC

master slave

same?

Yes

No

alert

GET table CRC

Stop

53Thursday, 30 September 2010

Page 54: Scaling your web app with MySQL replication

54

Table of contents

Why replication

How to set replication

Using replication

Leveraging replication

From single server to

replication

Adding a slave

Replacing a slave

Replacing the master

Managing replication

54Thursday, 30 September 2010

Page 55: Scaling your web app with MySQL replication

55

Replacing a slave

Slave crashes

are there more slaves?

STOP the master STOP one

slave

YesNo

add the first slave

add another slave

Stop

55Thursday, 30 September 2010

Page 56: Scaling your web app with MySQL replication

56

Table of contents

Why replication

How to set replication

Using replication

Leveraging replication

From single server to

replication

Adding a slave

Replacing a slave

Replacing the master

Managing replication

56Thursday, 30 September 2010

Page 57: Scaling your web app with MySQL replication

57

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

57Thursday, 30 September 2010

Page 58: Scaling your web app with MySQL replication

58

Table of contents

Why replication

How to set replication

Using replication

Leveraging replication

From single server to

replication

Adding a slave

Replacing a slave

Replacing the master

Managing replication

58Thursday, 30 September 2010

Page 59: Scaling your web app with MySQL replication

59

read/writemaster

read/onlyslaves

R/WR/O

load balancer

load balancing

59Thursday, 30 September 2010

Page 60: Scaling your web app with MySQL replication

60

backup master

slaves

STOP SLAVE

remove slave from load balancer

START SLAVE

perform backup

attach slave to load

balancer

Let slave catch up

60Thursday, 30 September 2010

Page 61: Scaling your web app with MySQL replication

master

slaves

STOP SLAVE

remove slave from load balancer

START SLAVE

calculate summary

tables

attach slave to load

balancer

Let slave catch up

61

makesummary

tables

61Thursday, 30 September 2010

Page 62: Scaling your web app with MySQL replication

62

master

slave

innodbnon partitioned

slave

innodbnon partitionedinnodb

partitioned by range

slave

MyISAMpartitioned by range

Partitionsfor heavy statistics

62Thursday, 30 September 2010

Page 63: Scaling your web app with MySQL replication

63

master

slave

innodbnon partitioned

slave

innodbnon partitioned

ARCHIVEpartitioned by range

(date)

slave

ARCHIVEpartitioned by range

(product)

slave

ARCHIVEpartitioned by range

(location)

Simulating multiple

dimensions

63Thursday, 30 September 2010

Page 64: Scaling your web app with MySQL replication

Semi-synchronous replication

• Available in 5.5 and higher

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

• Increases reliability

6464Thursday, 30 September 2010

Page 65: Scaling your web app with MySQL replication

65

transaction with regular replication

clientmaster

slave

commit

binary log

execute

returns to client

replication

65Thursday, 30 September 2010

Page 66: Scaling your web app with MySQL replication

66

transaction with semi-

synchronous replication

clientmaster

slave

commit

binary log

execute

returns to client

sends transaction

to slave

gets acknowledgement

relay log

66Thursday, 30 September 2010

Page 67: Scaling your web app with MySQL replication

READ MORE

6767Thursday, 30 September 2010

Page 68: Scaling your web app with MySQL replication

The MySQL online manual

68http://dev.mysql.com/doc68Thursday, 30 September 2010

Page 69: Scaling your web app with MySQL replication

High Performance MySQL

6969Thursday, 30 September 2010

Page 70: Scaling your web app with MySQL replication

MySQL High Availability

7070Thursday, 30 September 2010

Page 71: Scaling your web app with MySQL replication

Web Operations

7171Thursday, 30 September 2010

Page 72: Scaling your web app with MySQL replication

Cloud Application Architectures

7272Thursday, 30 September 2010

Page 73: Scaling your web app with MySQL replication

What we didn't cover

73

(And are matter for more presentations)

73Thursday, 30 September 2010

Page 74: Scaling your web app with MySQL replication

Partial replication

• Replicating only one or more objects

• Specialized slaves

• Different storage engines

• Different data structures

7474Thursday, 30 September 2010

Page 75: Scaling your web app with MySQL replication

Row-based replication

• Available in 5.1 and higher

• Makes your data more consistent.

• Fixes many problems with statement-based replication

7575Thursday, 30 September 2010

Page 76: Scaling your web app with MySQL replication

Delayed replication

• Available in 5.6 and higher

• Protect replication against human mistakes and data corruption

7676Thursday, 30 September 2010

Page 77: Scaling your web app with MySQL replication

Tools

• Monitoring

• Testing and simulating

• Repairing

• Filtering, improving

7777Thursday, 30 September 2010

Page 78: Scaling your web app with MySQL replication

THANKS FOR YOUR

ATTENTION

78

This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.

78Thursday, 30 September 2010