lua连接数据库操作示例代码

lua连接数据库可以使用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 = "10.209.**.**",
    port = 1234,
    database = "test",
    user = "user",
    password = "123456",
    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.")

 

posted @ 2018-02-22 13:55  SonoFelice  阅读(868)  评论(0编辑  收藏  举报