From a89d796f02699d2d16f99af021cc25d94e2ddf51 Mon Sep 17 00:00:00 2001 From: Sven Hoexter Date: Sun, 5 Feb 2012 12:19:11 +0100 Subject: [PATCH] Add a mysql-proxy sample which mirrors inserts to a sqlite3 db. --- mysql-proxy/query-mirror-sqlite3.lua | 29 ++++++++++++++++++++++++++++ mysql-proxy/usage.txt | 7 +++++++ 2 files changed, 36 insertions(+) create mode 100644 mysql-proxy/query-mirror-sqlite3.lua create mode 100644 mysql-proxy/usage.txt diff --git a/mysql-proxy/query-mirror-sqlite3.lua b/mysql-proxy/query-mirror-sqlite3.lua new file mode 100644 index 0000000..00818b1 --- /dev/null +++ b/mysql-proxy/query-mirror-sqlite3.lua @@ -0,0 +1,29 @@ +-- unfuck the cpath set by mysql-proxy and load sqlite3 driver +package.cpath=package.cpath .. ";/usr/lib/i386-linux-gnu/lua/5.1/?.so" +require "luasql.sqlite3" + +-- intercept mysql queries +function read_query( packet ) + if packet:byte() == proxy.COM_QUERY then + print("DEBUG: we got a normal query: " .. packet:sub(2)) + local query = string.sub(packet, 2) + + if string.match(string.upper(query), '^%s*INSERT') then + print("DEBUG: found a query to process") + -- create environment object + local env = assert (luasql.sqlite3()) + -- connect to data source + local con = assert (env:connect("mydb.sqlite3")) + + -- act on the database + -- local cur = assert (con:execute(query)) + local cur = con:execute(query) + + -- closing time + cur:close() + con:close() + env:close() + end + end +end + diff --git a/mysql-proxy/usage.txt b/mysql-proxy/usage.txt new file mode 100644 index 0000000..eeaf30b --- /dev/null +++ b/mysql-proxy/usage.txt @@ -0,0 +1,7 @@ +mysql proxy startup command: + +mysql-proxy --admin-username=a --admin-password=b --admin-lua-script=/usr/share/mysql-proxy/admin.lua --proxy-backend-addresses=127.0.0.1:3306 --proxy-lua-script=/home/sven/myscript.lua + + +Introduction: +http://www.oreillynet.com/pub/a/databases/2007/07/12/getting-started-with-mysql-proxy.html -- 2.39.5