From: Sven Hoexter Date: Sun, 5 Feb 2012 11:19:11 +0000 (+0100) Subject: Add a mysql-proxy sample which mirrors inserts to a sqlite3 db. X-Git-Url: https://git.sven.stormbind.net/?a=commitdiff_plain;h=a89d796f02699d2d16f99af021cc25d94e2ddf51;p=sven%2Fscripts.git Add a mysql-proxy sample which mirrors inserts to a sqlite3 db. --- 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