lua-resty-mysql是为基于cosocket API的ngx_lua提供的Lua Mysql客户端,通过它可以完成Mysql的操作。默认安装OpenResty时已经自带了该模块,使用文档可参考https://github.com/openresty/lua-resty-mysql。
#lua操作mysql
location = /testMysql{
#lua_code_cache off;
content_by_lua_block{
local function close_db(db)
if not db then
return
end
db:close()
end
local mysql = require("resty.mysql")
--创建实例
local db, err = mysql:new()
if not db then
ngx.say("new mysql error : ", err)
return
end
--设置超时时间(毫秒)
db:set_timeout(1000)
local props = {
host = "10.23.148.251",
port = 3306,
database = "db1",
user = "root",
password = "123456"
}
local res, err, errno, sqlstate = db:connect(props)
if not res then
ngx.say("connect to mysql error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate)
return close_db(db)
end
--建表
--local create_sql = "create table test_lua(id int primary key auto_increment,ch varchar(20))"
--res,err,errno,sqlstate = db:query(create_sql)
--if not res then
-- ngx.say("create table error:",err,",errno:",errno,",sqlstate:",sqlstate)
-- return close_db(db)
--end
--新增sql
local insert_sql = "insert into test_lua(ch) values('hello 崔春驰')"
res,err,errno,sqlstate = db:query(insert_sql)
if not res then
ngx.say("insert rows error:",err,",errno:",errno,",sqlstate:",sqlstate)
return close_db(db)
end
ngx.say("insert rows:",res.affected_row,",id:",res.insert_id,",res:",type(res))
--更新
local update_sql = " update test_lua set ch = 'hello2' where id = 1"
res,err,errno,sqlstate = db:query(update_sql)
if not res then
ngx.say("update rows error:",err,",errno:",errno,",sqlstate:",sqlstate)
return close_db(db)
end
ngx.say("update rows:",res.affected_row,",res:",type(res))
--查询
local select_sql = " select id,ch from test_lua"
res,err,errno,sqlstate = db:query(select_sql)
if not res then
ngx.say("select rows error:",err,",errno:",errno,",sqlstate:",sqlstate)
return close_db(db)
end
for i,row in ipairs(res) do
for name,id in pairs(row) do
ngx.say("select rows :",i,":",name,"=",id)
end
end
ngx.say("select rows:",res.affected_row,",res:",type(res))
--防止sql注入
local ch_param = ngx.req.get_uri_args()["ch"]
ngx.say("ch_param:",ch_param)
if not ch_param then
ch_param = ''
end
--使用ngx.quote_sql_str防止sql注入
local query_sql = " select id,ch from test_lua where ch =" .. ngx.quote_sql_str(ch_param)
res,err,errno,sqlstate = db:query(query_sql)
if not res then
ngx.say("select rows error:",err,",errno:",errno,",sqlstate:",sqlstate)
return close_db(db)
end
for i,row in ipairs(res) do
for name,id in pairs(row) do
ngx.say("select rows :",i,":",name,"=",id)
end
end
--删除
local delete_sql = "delete from test_lua where id in (2,3,4)"
res,err,errno,sqlstate = db:query(delete_sql)
if not res then
ngx.say("delete rows error:",err,",errno:",errno,",sqlstate:",sqlstate)
return close_db(db)
end
ngx.say("delete rows:",res.affected_rows,"")
测试结果:
其中用到的lua-resty-mysql的一些API方法:
db, err = mysql:new() 创建一个mysql数据库连接对象
ok, err = db:connect(options) 尝试远程连接mysql
host mysql的主机名
port 端口
database 数据库名
user 用户名
password 密码
charset 编码
db:set_timeout(time) 设置数据库连接超时时间
ok, err = db:set_keepalive(max_idle_timeout, pool_size) 设置连接池
ok, err = db:close() 关闭数据库
bytes, err = db:send_query(query) 发送查询
对于Mysql客户端的介绍基本够用了,更多请参考https://github.com/openresty/lua-resty-mysql。