← Back to team overview

mysql-proxy-discuss team mailing list archive

Re: Choose which server to connect to

 

Hum...

I think we misunderstood a bit.

In fact, I don't really want to know what is the address of the client,
but to which server the client made his connection.

In fact, I want each client to enter a different server adress: 
sqlserver1.dom.tld 
sqlserver2.dom.tld 
sqlserver3.dom.tld 
...

all sqlserverX.dom.tld are redirected to the proxy,
but the proxy redirect the queries according to the address it has been called from.

For instance, if a client tries to connect to sqlserver1.dom.tld, mysqlproxy will redirect the query to 192.168.0.1.
Another one wants to connect to sqlserver2.dom.tld, he will be redirected to 192.168.0.2, through mysql proxy.


Eventually, sqlserverX.dom.tld is always the adress of the mysql proxy.

----- Mail Original -----
De: "Joshua Zhu" <zhuzhaoyuan@xxxxxxxxx>
À: "Pierre" <mail@xxxxxxxx>
Cc: mysql-proxy-discuss@xxxxxxxxxxxxxxxxxxx
Envoyé: Mardi 10 Février 2009 09h26:20 GMT +01:00 Amsterdam / Berlin / Berne / Rome / Stockholm / Vienne
Objet: Re: [Mysql-proxy-discuss] Choose which server to connect to

Hi Pierre, 

I guess the script may look like this: 

-- 
-- redirect-by-client-address.lua 
-- author: Joshua Zhu (zhuzhaoyuan AT gmail.com ) 
-- 
local string = require("string") 

local is_debug = true 
local cli_svr_map 

function get_server(client) 
if not cli_svr_map then 
-- Initializing 
-- Note: You should modify the client IPs and the backend addresses below! 
cli_svr_map = { 
["192.168.0.189"] = { 
address = " 192.168.0.192:3306 ", 
backend_ndx = -1 }, 
["192.168.0.192"] = { 
address = " 192.168.0.189:3306 ", 
backend_ndx = -1 }, 
} 

for _, v in pairs(cli_svr_map) do 
for i = 1, #proxy.global.backends do 
local backend = proxy.global.backends[i] 
if v.address == backend.address then 
v.backend_ndx = i 
break 
end 
end 
end 

if is_debug then 
print("map table:") 
for k, v in pairs(cli_svr_map) do 
print(" [" .. k .. "] = {" .. v.address .. ", " .. v.backend_ndx .. "}") 
end 
end 
end 

local host = string.match(client, "([^:]+):") 
if cli_svr_map[host] then 
return cli_svr_map[host].backend_ndx 
end 

return -1 
end 

function connect_server() 
if is_debug then 
print("[connect_server] " .. proxy.connection.client.address) 
print("we have " .. #proxy.global.backends .. " backends:") 

for i = 1, #proxy.global.backends do 
local backend = proxy.global.backends[i] 
print(" [" .. i .. "].connected_clients = " .. backend.connected_clients) 
print(" [" .. i .. "].address = " .. backend.address) 
print(" [" .. i .. "].state = " .. backend.state) 
print(" [" .. i .. "].type = " .. backend.type) 
print(" [" .. i .. "].uuid = " .. (backend.uuid or "(nil)")) 
end 
end 

local index = get_server(proxy.connection.client.address) 
if index > 0 and index <= #proxy.global.backends then 
if proxy.global.backends[index].state ~= proxy.BACKEND_STATE_DOWN then 
if is_debug then 
print("redirect the client to backends[" .. proxy.global.backends[index].address .. "]") 
end 

proxy.connection.backend_ndx = index 
end 
end 
end 



Cheers! 

-- 
Joshua Zhu 
http://blog.zhuzhaoyuan.com 



Follow ups