Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trace MySql queries using MySql-Proxy?

I just downloaded the mysql-proxy and created this script lua (found in Mysql docs):

function read_query(packet)
   if string.byte(packet) == proxy.COM_QUERY then
     print("QUERY: " .. string.sub(packet, 2))
   end
 end

This is the command-line I'm using:

mysql-proxy -P localhost:1234 -b localhost:3306 --proxy-lua-script=profile.lua --plugins=proxy

When I run a simple query (like "select * from table1"), this error is reported: "failed: .\lua-scope.c:241: stat(C:...\profile.lua) failed: No error (0)"

Note: If I run mysql-proxy without lua script, no error occurs.

I need to install something to get mysql-proxy and query tracing working?

My environment is Windows 7 Professional x64.

Sorry the bad english.

like image 512
Maico Avatar asked Jan 12 '11 17:01

Maico


People also ask

How does MySQL proxy work?

In the most basic configuration, MySQL Proxy simply interposes itself between the server and clients, passing queries from the clients to the MySQL Server and returning the responses from the MySQL Server to the appropriate client.

How does SQL proxy work?

ProxySQL accepts incoming traffic from an application and forwards it to backend MySQL / MariaDB / Percona servers. In case of any failure in the nodes(servers), ProxySQL understands the issue and routes the traffic to another highly available node. Thus ProxySQL ensures that there is no single point of failure.


2 Answers

The error you're getting is caused by --proxy-lua-script pointing to a file that mysql-proxy can't find. Either you've typed the name in wrong, you've typed the path in wrong, or you are expecting it in your CWD and it's not there. Or actually, looking at the entire error a little more closely, it seems possible that mysql-proxy itself sees the file in CWD itself OK, but one of the underlying modules doesn't like it (possibly because mysql-proxy changes the CWD somehow?)

Try saving profile.lua to the root of your C: drive and trying different versions of the option like so:

--proxy-lua-script=c:\profile.lua
--proxy-lua-script=\profile.lua
--proxy-lua-script=/profile.lua

One of those would probably work

like image 68
jj33 Avatar answered Oct 18 '22 07:10

jj33


simple query log lua script:

require("mysql.tokenizer")

local fh = io.open("/var/log/mysql/proxy.query.log", "a+")
fh:setvbuf('line',4096)
local the_query = "";
local seqno = 0;

function read_query( packet )
    if string.byte(packet) == proxy.COM_QUERY then
        seqno = seqno + 1
        the_query = (string.gsub(string.gsub(string.sub(packet, 2), "%s%s*", ' '), "^%s*(.-)%s*$", "%1"))
        fh:write(string.format("%s %09d %09d : %s (%s) -- %s\n",
            os.date('%Y-%m-%d %H:%M:%S'),
            proxy.connection.server.thread_id,
            seqno,
            proxy.connection.client.username,
            proxy.connection.client.default_db,
            the_query))
        fh:flush()
        return proxy.PROXY_SEND_QUERY
    else
        query = ""
    end
end
like image 45
jmullee Avatar answered Oct 18 '22 07:10

jmullee