前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >openesty嵌入lua脚本操作mysql

openesty嵌入lua脚本操作mysql

作者头像
友儿
发布2022-09-13 16:03:18
4870
发布2022-09-13 16:03:18
举报
文章被收录于专栏:友儿
openesty嵌入lua脚本连接mysql

下载地址

nginx.conf文件

代码语言:javascript
复制
worker_processes  1;
error_log logs/error.log;
events {
  worker_connections 1024;
}
http {
  # 设置纯 Lua 扩展库的搜寻路径(';;' 是默认路径)
  lua_package_path "/data/www/code/nginx+lua/config/lua_p/?.lua;;";
  # 设置 C 编写的 Lua 扩展模块的搜寻路径(也可以用 ';;')
  lua_package_cpath "/data/www/code/nginx+lua/config/lua_p_c/?.so;;";

  server {
     listen 8080;
     location /mysql {
           default_type 'text/html';
           lua_code_cache off;
           content_by_lua_file   ./config/lua/mysql.lua;
           charset utf-8;
     }
  }
}
mysql.lua文件

代码语言:javascript
复制
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

-- or connect to a unix domain socket file listened
-- by a mysql server:
--     local ok, err, errcode, sqlstate =
--           db:connect{
--              path = "/path/to/mysql.sock",
--              database = "ngx_test",
--              user = "ngx_test",
--              password = "ngx_test" }

local ok, err, errcode, sqlstate = db:connect{
  host = "127.0.0.1",
  port = 3306,
  database = "test",
  user = "root",
  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.");

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("create table cats "
      .. "(id serial primary key, "
      .. "name varchar(5))");
if not res then
  ngx.say("bad result: ", err, ": ", errcode, ": ", sqlstate, ".");
  return
end

ngx.say("table cats created.</br>");

res, err, errcode, sqlstate =
db:query("insert into cats (name) "
      .. "values (\'Bob\'),(\'老王\'),(\'老张\')");
if not res then
  ngx.say("bad result: ", err, ": ", errcode, ": ", sqlstate, ".");
  return
end

ngx.say(res.affected_rows, " rows inserted into table cats </br>",
  "(last insert id: ", res.insert_id, ")</br>");

-- run a select query, expected about 10 rows in
-- the result set:
res, err, errcode, sqlstate =
db:query("select * from cats order by id asc", 10);
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.say("failed to set keepalive: ", err);
  return
end

-- or just close the connection right away:
-- local ok, err = db:close()
-- if not ok then
--     ngx.say("failed to close: ", err)
--     return
-- end
拓展一些常用的封装好的工具包
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • openesty嵌入lua脚本连接mysql
  • nginx.conf文件
  • mysql.lua文件
  • 拓展一些常用的封装好的工具包
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档