getting started with mysql proxy -...

52
Getting started with MySQL Proxy Giuseppe Maxia QA Developer - MySQL AB Sofia - OpenFest 2007

Upload: others

Post on 20-Sep-2019

21 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Getting started with

MySQL Proxy

Giuseppe MaxiaQA Developer - MySQL AB

Sofia - OpenFest 2007

Page 2: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Agenda

• Overview

• Some fun

• Injecting queries

• Filtering and rewriting queries

• Working with results

• Proxy for logging and debugging

• Replication goodies

• Q&A

2

Page 3: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Proxy (< lat. procuratio)

3

= Someone taking care of someone else's interests

A server proxy is something acting on behalf of another server

Page 4: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Overview

4

Page 5: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Overview

5

PROXY CORE

Page 6: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Overview

5

PROXY CORE

hookconnection

hookread query

hookread result

Page 7: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Overview

5

PROXY CORE

hookconnection

hookread query

hookread result

Page 8: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Overview

5

PROXY CORE

hookconnection

hookread query

hookread result

function

function

function

Lua script

Page 9: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Overview

6

??

Page 10: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Overview

6

??Why not ...{Perl ?

PHP?Javascript?[whatever]?

Page 11: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Overview

7

Page 12: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Overview

7

SMALL ( < 200 KB)

Page 13: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Overview

7

SMALL ( < 200 KB)

DESIGNED for EMBEDDED systems

Page 14: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Overview

7

SMALL ( < 200 KB)

DESIGNED for EMBEDDED systems

Widely used (lighttpd)

Page 15: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Overview

7

SMALL ( < 200 KB)

DESIGNED for EMBEDDED systems

Widely used (lighttpd)

lighttpd, like MySQL Proxy, was created by Jan Kneschke

Page 16: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Overview

8

Very popular among game writers

Page 17: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Overview

8

Very popular among game writers

Page 18: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Overview

8

Very popular among game writers

Page 19: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Some fun

9

LIVE

Page 20: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Interceptingfunction read_query(packet) if packet:byte() == proxy.COM_QUERY then local query = packet:sub(2) print("Hello world! Seen query: " .. query ) endend

10

Page 21: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

11

Injecting queries

(1)

Page 22: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

12

Injecting queries

(2)

Page 23: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

injectingfunction read_query(packet) -- ... proxy.queries:append(2, query1 ) proxy.queries:append(1, packet ) proxy.queries:append(3, query2 )

return proxy.PROXY_SEND_QUERY

end

13

Page 24: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

injectingfunction read_query_result(inj) if res.id == 1 then return proxy.PROXY_SEND_RESULT else -- do something

return proxy.PROXY_IGNORE_RESULTend

14

Page 25: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

filtering queries

•Like injecting

•but without the original

Page 26: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

working with results

Page 27: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

working with results

• return the original result

Page 28: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

working with results

• return the original result

• return a fake result

Page 29: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

working with results

• return the original result

• return a fake result

• return an error

Page 30: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

working with results

• return the original result

• return a fake result

• return an error

• alter the original result

Page 31: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

working with results

• return the original result

• return a fake result

• return an error

• alter the original result

• return something different (affected/retrieved)

Page 32: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

debugging

Page 33: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

debugging

•Put a Proxy in between

Page 34: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

debugging

•Put a Proxy in between

•use a sensible script to see what's going on (e.g. tutorial-packets.lua or tutorial-states.lua)

Page 35: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

debugging

server

client

proxy

diagnosticstext

Page 36: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

debugging scripts

server

client

proxy

diagnosticstext

proxy

diagnosticstext

Page 37: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Chained proxies:doubled features

server

client

proxy

pivot tablesproxy

loops

Page 38: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Testingserver

client

proxy

fake packetse.g.

connectors

Page 39: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

logging via Proxy# client (1)mysql> drop table t1;Query OK, 0 rows affected (0.05 sec)

mysql> create table t1 (i int);Query OK, 0 rows affected (0.02 sec)

22

Page 40: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

logging via Proxy# proxy (1)2007-08-24 11:37:28 296 -- drop table t1 >{0}

2007-08-24 11:37:35 296 -- create table t1 (i int) >{0}

23

Page 41: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

logging via Proxy# client (2)mysql> insert into t1;ERROR 1064 (42000): You have an error in your SQL syntax;

24

Page 42: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

logging via Proxy# proxy (2)

2007-08-24 11:37:43 296 -- insert into t1 >{0} [ERR]

25

Page 43: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

logging via Proxy# client (3)mysql> insert into t1 values (1), (2);Query OK, 2 rows affected (0.01 sec)Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from t1;+------+| i |+------+| 1 | | 2 | +------+2 rows in set (0.00 sec)

26

Page 44: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

logging via Proxy# proxy (3)

2007-08-24 11:38:00 296 -- insert into t1 values (1),(2) >{2}

2007-08-24 11:38:03 296 -- select * from t1 <{2}

27

Page 45: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Rerouting traffic

28

Page 46: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Rerouting traffic(1) do

sudo iptables -t nat \ -I PREROUTING \ -s ! 127.0.0.1 -p tcp \ --dport 3306 -j \ REDIRECT --to-ports 4040

29

Page 47: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Rerouting traffic(1) undo

sudo iptables -t nat \ -D PREROUTING \ -s ! 127.0.0.1 -p tcp \ --dport 3306 -j \ REDIRECT --to-ports 4040

30

Page 48: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Rerouting traffic

31

Page 49: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

replication goodiesmaster

client

slave slave

write

readreadread

writewrite

Normally, clients must bereplication-aware

Page 50: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

read

replication goodiesmaster

client

proxy

slave slave

write

write

readreadread

writewrite

With a proxy, clients can be replication unaware

Page 51: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Live examples

• loops

• logs

• shell access

• pivot tables

• more ...

34

Page 52: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone

Q&AAny questions?

slides at http://datacharmer.org

35