1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| local props = { host = "xxxx", port = 3306, database = "xxx", user = "xxx", password = "xxxx", charset = 'utf8mb4', max_packet_size = 1024 * 1024 }
local function close_db(db) if not db then return end local pool_max_idle_time = 10000 local pool_size = 100 local ok, err = db:set_keepalive(pool_max_idle_time, pool_size) if not ok then ngx.log(ngx.ERR, "set keepalive error : ", err) end end
local mysql = require("resty.mysql")
local db, err = mysql:new() if not db then ngx.log(ngx.ERR, "new mysql error : ", err) return end
db:set_timeout(10000)
local res,err,errno,sqlstate = db:connect(props)
if not res then ngx.log(ngx.ERR, "connect to mysql error:", err,", errno:", errno,", sqlstate:", sqlstate) return close_db(db) end ngx.say("connectd")
ngx.say(ngx.var.arg_access_token) ngx.log(ngx.INFO, ngx.var.arg_access_token)
local select_sql = "show PROCESSLIST" res, err, errno, sqlstate = db:query(select_sql) if not res then ngx.say("select table error :", err,", errno", errno, ", sqlstate : ",sqlstate) return close_db(db) end ngx.say(sqlstate)
for i, row in ipairs(res) do for key,value in pairs(row) do ngx.say("select row", i," : ", key, " = ", value) end ngx.say("<br/>===================") end
close_db(db)
|