Initially we are in /home/kiran.chinta directory:
step1: dowloading mysql proxy.
wget http://mysql.oss.eznetsols.org/Downloads/MySQL-Proxy/mysql-proxy-0.8.1-linux-rhel5-x86-64bit.tar.gz
step2 : unzip the direcctory:
tar -xvf mysql-proxy-0.8.1-linux-rhel5-x86-64bit.tar.gz
step3: create the mysql-proxy.cnf file.
The file content is as follows:
[mysql-proxy]
daemon = true
pid-file = /var/run/mysql-proxy.pid
log-file = /var/log/mysql-proxy.log
log-level = debug
proxy-address = 10.10.10.98:4040
admin-address = 127.0.0.1:4041
admin-username = admin
admin-password = adpass
admin-lua-script = /home/kiran.chinta/mysql-proxy-0.8.1-linux-glibc2.3-x86-64bit/share/doc/mysql-proxy/admin-sql.lua
proxy-lua-script = /home/kiran.chinta/mysql-proxy-0.8.1-linux-glibc2.3-x86-64bit/share/doc/mysql-proxy/sample2.lua
step 4: change the file permissions of mysql-proxy.cnf file
chmod 660 /etc/mysql-proxy.cnf
step 5: The sample2.lua script is as follows:
-- Prints a line for each query received from the client along with times
local log_file = '/home/kiran.chinta/mysqlproxy.log'
local fh = io.open(log_file, "a+")
--fh:write("hello how r you")
function read_query(packet)
if packet:byte() == proxy.COM_QUERY then
-- inject the query so that we can inspect the results and get the times
proxy.queries:append(1, packet, { resultset_is_needed = true })
return proxy.PROXY_SEND_QUERY
end
end
-- read_query_result() is only called for queries injected via read_query()
function read_query_result(inj)
local row_count = 0
local res = assert(inj.resultset)
local num_cols = string.byte(res.raw, 1)
if num_cols > 0 and num_cols < 255 then
for row in inj.resultset.rows do
row_count = row_count + 1
end
end
if (res.affected_rows) then
row_count = res.affected_rows
end
local error_status ="[OK]"
-- print("the query status is")
-- print(res.query_status)
if (res.query_status > 0 ) then
-- print("error command")
error_status = "[ERROR]" .. "\t" ..
"Error Code = " .. res.raw:byte(2)+(res.raw:byte(3)*256) .. "\t" ..
"SQL State = " .. string.format("%q", res.raw:sub(5, 9)) .. "\t" ..
"Err Msg = " .. string.format("%q", res.raw:sub(10)) .. "\t"
end
local log_string = string.format("Query [%.3f ms]: %s [%d rows]",
(inj.query_time / 1000),
inj.query,
row_count)
print(log_string)
local ipadd1 = proxy.connection.client.src.name
local hname = proxy.connection.client.src.address
local uname1 = proxy.connection.client.username
local threadid = proxy.connection.server.thread_id
print(proxy.connection.client)
--fh:write("hello")
fh:write(os.date('%Y-%m-%d %H:%M:%S'))
fh:write(" ipaddress: ", ipadd1, " ")
fh:write(" username: " , uname1, "\t ")
fh:write(" ThreadID: " , threadid, "\t")
print("the hostname is" )
print(hname);
print("the username is")
print(uname1)
fh:write(log_string)
fh:write(error_status)
fh:write("\n")
--fh:write(proxy.connection.client.username)
fh:flush()
end
step 6: start the mysql-proxy
sh /home/kiran.chinta/mysql-proxy-0.8.1-linux-glibc2.3-x86-64bit/bin/mysql-proxy --defaults-file=/etc/mysql-proxy.cnf
step7 : check mysql-proxy has started
ps -ef | grep proxy
optional step8 and set9
step-8: we can divery the port 3306 to 4040:
sudo iptables -t nat -I PREROUTING -s ! 127.0.0.1 -p tcp --dport 3306 -j REDIRECT --to-ports 4040
sudo iptables -t nat -I PREROUTING -s ! localhost -p tcp --dport 3306 -j REDIRECT --to-ports 4040
sudo iptables -t nat -I PREROUTING -s ! 10.10.10.68 -p tcp --dport 3306 -j REDIRECT --to-ports 4040
step9: we can revert back the ports from 3306
sudo iptables -t nat -I PREROUTING -s ! 127.0.0.1 -p tcp --dport 3306 -j REDIRECT --to-ports 4040
sudo iptables -t nat -I PREROUTING -s ! localhost -p tcp --dport 3306 -j REDIRECT --to-ports 4040
sudo iptables -t nat -I PREROUTING -s ! 10.10.10.68 -p tcp --dport 3306 -j REDIRECT --to-ports 4040
#### to know more about mysqlproxy som of the usefule urls are as follows ######
http://dev.mysql.com/tech-resources/articles/proxy-gettingstarted.html
http://downloads.mysql.com/docs/mysql-proxy-en.pdf
step1: dowloading mysql proxy.
wget http://mysql.oss.eznetsols.org/Downloads/MySQL-Proxy/mysql-proxy-0.8.1-linux-rhel5-x86-64bit.tar.gz
step2 : unzip the direcctory:
tar -xvf mysql-proxy-0.8.1-linux-rhel5-x86-64bit.tar.gz
step3: create the mysql-proxy.cnf file.
The file content is as follows:
[mysql-proxy]
daemon = true
pid-file = /var/run/mysql-proxy.pid
log-file = /var/log/mysql-proxy.log
log-level = debug
proxy-address = 10.10.10.98:4040
admin-address = 127.0.0.1:4041
admin-username = admin
admin-password = adpass
admin-lua-script = /home/kiran.chinta/mysql-proxy-0.8.1-linux-glibc2.3-x86-64bit/share/doc/mysql-proxy/admin-sql.lua
proxy-lua-script = /home/kiran.chinta/mysql-proxy-0.8.1-linux-glibc2.3-x86-64bit/share/doc/mysql-proxy/sample2.lua
step 4: change the file permissions of mysql-proxy.cnf file
chmod 660 /etc/mysql-proxy.cnf
step 5: The sample2.lua script is as follows:
-- Prints a line for each query received from the client along with times
local log_file = '/home/kiran.chinta/mysqlproxy.log'
local fh = io.open(log_file, "a+")
--fh:write("hello how r you")
function read_query(packet)
if packet:byte() == proxy.COM_QUERY then
-- inject the query so that we can inspect the results and get the times
proxy.queries:append(1, packet, { resultset_is_needed = true })
return proxy.PROXY_SEND_QUERY
end
end
-- read_query_result() is only called for queries injected via read_query()
function read_query_result(inj)
local row_count = 0
local res = assert(inj.resultset)
local num_cols = string.byte(res.raw, 1)
if num_cols > 0 and num_cols < 255 then
for row in inj.resultset.rows do
row_count = row_count + 1
end
end
if (res.affected_rows) then
row_count = res.affected_rows
end
local error_status ="[OK]"
-- print("the query status is")
-- print(res.query_status)
if (res.query_status > 0 ) then
-- print("error command")
error_status = "[ERROR]" .. "\t" ..
"Error Code = " .. res.raw:byte(2)+(res.raw:byte(3)*256) .. "\t" ..
"SQL State = " .. string.format("%q", res.raw:sub(5, 9)) .. "\t" ..
"Err Msg = " .. string.format("%q", res.raw:sub(10)) .. "\t"
end
local log_string = string.format("Query [%.3f ms]: %s [%d rows]",
(inj.query_time / 1000),
inj.query,
row_count)
print(log_string)
local ipadd1 = proxy.connection.client.src.name
local hname = proxy.connection.client.src.address
local uname1 = proxy.connection.client.username
local threadid = proxy.connection.server.thread_id
print(proxy.connection.client)
--fh:write("hello")
fh:write(os.date('%Y-%m-%d %H:%M:%S'))
fh:write(" ipaddress: ", ipadd1, " ")
fh:write(" username: " , uname1, "\t ")
fh:write(" ThreadID: " , threadid, "\t")
print("the hostname is" )
print(hname);
print("the username is")
print(uname1)
fh:write(log_string)
fh:write(error_status)
fh:write("\n")
--fh:write(proxy.connection.client.username)
fh:flush()
end
step 6: start the mysql-proxy
sh /home/kiran.chinta/mysql-proxy-0.8.1-linux-glibc2.3-x86-64bit/bin/mysql-proxy --defaults-file=/etc/mysql-proxy.cnf
step7 : check mysql-proxy has started
ps -ef | grep proxy
optional step8 and set9
step-8: we can divery the port 3306 to 4040:
sudo iptables -t nat -I PREROUTING -s ! 127.0.0.1 -p tcp --dport 3306 -j REDIRECT --to-ports 4040
sudo iptables -t nat -I PREROUTING -s ! localhost -p tcp --dport 3306 -j REDIRECT --to-ports 4040
sudo iptables -t nat -I PREROUTING -s ! 10.10.10.68 -p tcp --dport 3306 -j REDIRECT --to-ports 4040
step9: we can revert back the ports from 3306
sudo iptables -t nat -I PREROUTING -s ! 127.0.0.1 -p tcp --dport 3306 -j REDIRECT --to-ports 4040
sudo iptables -t nat -I PREROUTING -s ! localhost -p tcp --dport 3306 -j REDIRECT --to-ports 4040
sudo iptables -t nat -I PREROUTING -s ! 10.10.10.68 -p tcp --dport 3306 -j REDIRECT --to-ports 4040
#### to know more about mysqlproxy som of the usefule urls are as follows ######
http://dev.mysql.com/tech-resources/articles/proxy-gettingstarted.html
http://downloads.mysql.com/docs/mysql-proxy-en.pdf

No comments:
Post a Comment