#cat mysql_test.lua
local request_args_tab = ngx.req.get_uri_args()for k, v in pairs(request_args_tab) do
test = k
end
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)
local ok, err, errno, sqlstate = db:connect{
host ="127.0.0.1",
port =3306,
database ="ngx_lua",
user ="root",
password="123456",
max_packet_size =1024*1024}if not ok then
ngx.say("failed to connect: ", err,": ", errno," ", sqlstate)return
end
ngx.say("connected to mysql.")--local res, err, errno, sqlstate = db:query("drop table if exists cats")--if not res then
-- ngx.say("bad result: ", err,": ", errno,": ", sqlstate,".")--return--end
----res, err, errno, sqlstate = db:query("create table cats ".."(id int not null primary key auto_increment, "--.."name varchar(30))")--if not res then
-- ngx.say("bad result: ", err,": ", errno,": ", sqlstate,".")--return--end
----ngx.say("table cats created.")----res, err, errno, sqlstate = db:query("insert into cats(name) ".."values (\'Bob\'),(\'\'),(null)")--if not res then
-- ngx.say("bad request: ", err,": ", errno,": ", sqlstate,".")--return--end
--ngx.say(res.affected_rows," rows inserted into table cats ","(last insert id: ", res.insert_id,")")--res, err, errno, sqlstate = db:query("select * from ngx_lua.cats order by id asc",10)
okst =[[select* from ngx_lua.cats where name="]]..test..[["]]
ngx.say(okst)
res, err, errno, sqlstate = db:query(okst)if not res then
ngx.say("bad result ", err,": ", errno,": ", sqlstate,".")return
end
local cjson = require "cjson"
ngx.say("result: ", cjson.encode(res))
local ok, err = db:set_keepalive(1000,100)if not ok then
ngx.say("failed to set keepalive: ", err)return
end
#cat redis_test.lua
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1000)
local ok, err = red:connect("127.0.0.1",6379)if not ok then
ngx.say("failed to connect: ", err)return
end
ngx.say("set result: ", ok)
local res, err = red:get("dog")if not res then
ngx.say("failed to get doy: ", err)return
end
if res == ngx.null then
ngx.say("dog not found.")return
end
ngx.say("dog: ", res)