为什么获取resty.mysql的连接池重复利用次数一直是失败呢?
我的代码如下(官方案例):
local mysql = require "resty.mysql"
local db, err = mysql:new()
if not db then
ngx.say("failed to instantiate mysql: ", err)
return
end
db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{
host = "127.0.0.1",
port = 3306,
database = "test",
user = "root",
password = "111111",
charset = "utf8",
max_packet_size = 1024 * 1024,
}
if not ok then
ngx.say("failed to connect: ", err, ": ", errcode, " ", sqlstate)
return
end
ngx.say("connected to mysql.")
local res, err, errcode, sqlstate =
db:query("drop table if exists cats")
if not res then
ngx.say("bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
return
end
res, err, errcode, sqlstate =
db:query("select * from user;")
if not res then
ngx.say("bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
return
end
local cjson = require "cjson"
ngx.say("result: ", cjson.encode(res))
-- put it into the connection pool of size 100,
-- with 10 seconds max idle timeout
local ok, err = db:set_keepalive(10000, 100)
if not ok then
ngx.log(ngx.ERR,"failed to set keepalive: ",err);
return
end
local times, err = db:get_reused_times();
if times then
ngx.log(ngx.INFO,"重复利用次数:",times);
else
ngx.log(ngx.ERR,err);
end
db:set_keepalive方法是调用成功的!
但是调用db:get_reused_times()方法,一直报错如下:
2017/08/04 17:30:30 [error] 79984#0: *84179 [lua] fetch_task.lua:56: closed, client: 192.168.228.1, server: localhost, request: "GET /api/v1/task/fetch?token=7631dc5176945ds79c8f000c29fffc18
HTTP/1.1", host: "192.168.228.133"