建议一静态虚拟用户表,这个表的更新需要重新mysql-proxy
mysql-proxy本向链接的几个lua则更新不需要重启,这个表是在lua里再包含进来的,似乎不会监视文件更新重新加载。

vim /usr/local/mysql-proxy/lib/mysql-proxy/lua/proxy/userlist.lua

内容:

userlist = {['jj'] = 'abc',['coco'] = 'def'}
return userlist

配置mysql-proxy

vim /usr/local/mysql-proxy/lua/rw-splitting.lua

增加或替换方法

--用户帐号处理函数

function read_auth()
    local c = proxy.connection.client
    local s = proxy.connection.server
    local password =assert(require("mysql.password"))
    local proto = assert(require("mysql.proto"))
    
    local ud = require("proxy.userlist")
    
    local check = 0
    
    for user, passwd in pairs(ud) do
        if user == c.username and password.scramble(s.scramble_buffer, password.hash(passwd)) == c.scrambled_password then
            check = 1
            break
        end
    end
    
    if check == 0 then
        proxy.response.type = proxy.MYSQLD_PACKET_ERR
        proxy.response.errmsg ="error , code 111 !"
        return proxy.PROXY_SEND_RESULT
    end

    --如果二级帐号授权成功 则把连接交给真实帐号
    if check == 1 then
        local protocol_41_default_capabilities = 8 + 512 + 32768 
        proxy.queries:append(1,
                        proto.to_response_packet({
                                username = '真实用户',
                                response = password.scramble(s.scramble_buffer, password.hash('真实密码')),
                                charset  = 8, -- default charset
                                database = c.default_db,
                                max_packet_size = 1 * 1024 * 1024,
                                server_capabilities=protocol_41_default_capabilities
                        })
                )

        return proxy.PROXY_SEND_QUERY
    end
    
end