Home >Database >Mysql Tutorial >用来分析、监控和变换通信数据MySQL-Proxy(2)_MySQL

用来分析、监控和变换通信数据MySQL-Proxy(2)_MySQL

WBOY
WBOYOriginal
2016-06-01 13:59:28892browse

 if is_in_transaction == 0 and
  packet:byte() == proxy.COM_QUERY and
  packet:sub(2, 7) == "SELECT" then
  local max_conns = -1
  local max_conns_ndx = 0
  for i = 1, #proxy.servers do
  local s = proxy.servers[i]

  -- 需要选择一个拥有空闲连接的从数据库


  if s.type == proxy.BACKEND_TYPE_RO and
  s.idling_connections > 0 then
  if max_conns == -1 or
  s.connected_clients   max_conns = s.connected_clients
  max_conns_ndx = i
  end
  end
  end


  -- 至此,我们找到了一个拥有空闲连接的从数据库


  if max_conns_ndx > 0 then
  proxy.connection.backend_ndx = max_conns_ndx
  end
  else


  -- 发送到主数据库


  end
  return proxy.PROXY_SEND_QUERY


  注释:此技巧还可以用来实现其他的数据分布策略,例如分片(Sharding)。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn