Getting started with MySQL Proxy -...
Transcript of Getting started with MySQL Proxy -...
![Page 1: Getting started with MySQL Proxy - datacharmer.comdatacharmer.com/presentations/openfest2007/mysqlproxy_openfest2007.pdf · Proxy (< lat. procuratio) 3 = Someone taking care of someone](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/1.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/2.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/3.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/4.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/5.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/6.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/7.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/8.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/9.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/10.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/11.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/12.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/13.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/14.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/15.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/16.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/17.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/18.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/19.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/20.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/21.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/22.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/23.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/24.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/25.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/26.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/27.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/28.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/29.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/30.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/31.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/32.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/33.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/34.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/35.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/36.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/37.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/38.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/39.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/40.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/41.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/42.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/43.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/44.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/45.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/46.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/47.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/48.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/49.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/50.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/51.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022040710/5e0eed702a2f2361765e2343/html5/thumbnails/52.jpg)
Q&AAny questions?
slides at http://datacharmer.org
35