openresty + lua 1、openresty 连接 mysql,实现 crud
最近开发一个项目,公司使用的是 openresty + lua,所以就研究了 openresty + lua。介绍的话,我就不多说了,网上太多了。
写这个博客主要是记录一下,在学习的过程中遇到的一些坑吧(其实会了一种语言,再学习其他语言不难,但是毕竟属于新的东西,环境、写法什么的还是有点差别,如果不注意也是心醉呢,比如说我,就遇到了一些问题)
先贴下我几个学习的网站:
- 1、http://www.runoob.com/lua/lua-tutorial.html
- 2、http://wiki.jikexueyuan.com/list/lua/
- 3、http://jinnianshilongnian.iteye.com/category/333854 (京东构架师,他有很多学习系列的话,可以看看,关注下大神)
- 4、http://openresty.org/en/
- 5、https://github.com/openresty/openresty(春哥创立的openresty,可以去 github 上关注一下他,他也写了很多第三方的 lua 插件,后面都会用到)
好了,废话不说,直接上代码:(openresty 的安装我就不多说了,参考官网即可。基本没遇到什么大难题,mac 推荐 brew 安装)
简单写了一个工具类,后面直接引入即可:
local connectMysqlUtil = {}
local mysql = require "resty.mysql"
-- connect to mysql;
function connectMysqlUtil.connect()
local db, err = mysql:new()
if not db then
return false
end
db:set_timeout()
local ok, err, errno, sqlstate = db:connect{
host = "127.0.0.1",
port = ,
database = "ngx_test",
user = "root",
password = "",
max_packet_size = * }
if not ok then
ngx.say("connect mysql failed")
return false
end
return db
end
return connectMysqlUtil
然后来一个例子,引入这个工具类,来实现 crud:
local connectMysqlUtil = require("connectMysqlUtil")
local db = connectMysqlUtil.connect()
if db == false then
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
return
end
--drop table
local res, err, errcode, sqlstate =
db:query("drop table if exists cats")
if not res then
ngx.say("drop bad result:", res, ",errcode:", errcode, ",sqlstate:", sqlstate)
return
end
--create table
res, err, errcode, sqlstate =
db:query("create table cats(id int primary key auto_increment,name varchar(20))")
if not res then
ngx.say("create bad result:", err, ",errcode:", errcode, ",sqlstate:",sqlstate)
return
end
--insert
res, err, errcode, sqlstate =
db:query("insert into cats (name) "
.. "values (\'bamboo\'),(\'zhuzi\'),(\'zi\'),(\'anya\'),(\'ying\'),(\'ping\')")
if not res then
ngx.say("insert failed")
return
end
--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", )
if not res then
ngx.say("bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
return
end
local cjson = require "cjson"
ngx.say("result: ", cjson.encode(res))
好了,搞定,收工。来再配置 openresty,来浏览器请求一下,看看效果吧.
openresty 如下:
server {
listen ;
server_name localhost;
default_type "text/html";
lua_need_request_body on;
location = /test_mysql_queryAndGet {
content_by_lua_file /Users/zhuzi/zhuzi_relation/exercise/lua_pro/mysql_queryAndGet.lua;
}
}
浏览器访问:http://localhost:6699/test_mysql_queryAndGet,得到如下结果.
result: [{"name":"bamboo","id":1},{"name":"zhuzi","id":2},{"name":"zi","id":3},{"name":"anya","id":4},{"name":"ying","id":5},{"name":"ping","id":6}]
可以看到,获取数据库数据成功。
这里要提一下 openresty 加载预置 lua 的问题,因为我这个工具类并不放在 resty 目录下,是自己的一个目录,所以 openresty 里要加上相应的配置才可以在 openresty 启动的时候去读取到这些 lua 脚本。
具体如下:
lua_package_path "/usr/local/Cellar/openresty/1.11.2.5/lualib/resty/?.lua;/Users/zhuzi/zhuzi_relation/exercise/lua_pro/util/?.lua;;;";
(我标红的两个分号";" 要注意了,必须要加上,要不找路径的时候会有问题 )
第二个目录是我的目录,我加在了 resty 目录的后面,所以加载的时候会加载到此配置。
好了,今天 mysql 的就到这里,这个比较简单,也没遇到什么问题,所以先到这里。
openresty + lua 1、openresty 连接 mysql,实现 crud的更多相关文章
- openresty 学习笔记四:连接mysql和进行相关操作
openresty 学习笔记四:连接mysql和进行相关操作 毕竟redis是作为缓存,供程序的快速读写,虽然reidis也可以做持久化保存,但还是需要一个做数据存储的数据库.比如首次查询数据在red ...
- Struts2连接Mysql的Crud使用
今天分享的是struts2框架中增删改查的用法: 一:利用Struts2框架 1.1在pom.xml中导入相关依赖 <project xmlns="http://maven.apach ...
- Layui连接mysql操作CRUD案例
今天分享的是一个新前端框架Layui,用它来链接数据库实现一下crud的操作. 一:layui简历 layui,是一款采用自身模块规范编写的前端 UI 框架,遵循原生 HTML/CSS/JS 的书写与 ...
- Openresty最佳案例 | 第6篇:OpenResty连接Mysql
转载请标明出处: http://blog.csdn.net/forezp/article/details/78616698 本文出自方志朋的博客 centos 安装mysl Centos系统下安装my ...
- openresty+lua做接口调用权限限制
说明:openresty可以理解为一个服务器它将nginx的核心包含了过来,并结合lua脚本语言实现一些对性能要求高的功能,该篇文章介绍了使用openresty 1.purview.lua --调用j ...
- Openresty+Lua+Kafka实现日志实时采集
简介 在很多数据采集场景下,Flume作为一个高性能采集日志的工具,相信大家都知道它.许多人想起Flume这个组件能联想到的大多数都是Flume跟Kafka相结合进行日志的采集,这种方案有很多他的优点 ...
- 畅购商城(四):Lua、OpenResty、Canal实现广告缓存与同步
好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star,更多文章请前往:目录导航 畅购商城(一):环境搭建 畅购商 ...
- openresty+lua+kafka方案与Tomcat接口并发度对比分析
1.openresty+lua+kafka 1.1 openresty+lua+kafka方案 之前的项目基于nginx反向代理后转发到Tomcat的API接口进行业务处理,然后将json数据打入ka ...
- OpenResty + Lua + Kafka 实现日志收集系统以及部署过程中遇到的坑
********************* 部署过程 ************************** 一:场景描述 对于线上大流量服务或者需要上报日志的nginx服务,每天会产生大量的日志,这些 ...
随机推荐
- keepalive高可用
Keepalived软件起初是专门为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能.因此,Keepalived除了能够管理LVS软 ...
- 一些hue的参考网址
CDH5.8 HUE的使用(那些年走过的坑) https://blog.csdn.net/gao123456789amy/article/details/79242713 HUE的时间问题等 http ...
- TaskScheduler
一初始化 在SparkContext初始化的时候,同时初始化三个对象.DAGScheduler,TaskScheduler,SchedulerBackend.DAGScheduler,前面已经讲到,做 ...
- 转)nodejs后台启动方式PM2
如果直接通过node app来启动,如果报错了可能直接停在整个运行,supervisor感觉只是拿来用作开发环境的.再网上找到pm2.目前似乎最常见的线上部署nodejs项目的有forever,pm2 ...
- 738. Monotone Increasing Digits 单调递增的最接近数字
[抄题]: Given a non-negative integer N, find the largest number that is less than or equal to N with m ...
- viewstate?
用于存放数据,可永久保存,存放在页面的隐藏控件里 支持string,integer,array,boolean,ArrayList,hashtable类型,使用viewstate会增加页面h ...
- ES6 proxy(代理拦截) &&Reflect
- Linux anaconda 内网 安装 卸载
安装并不难, 官网介绍的很清楚, 但每次到官网找安装方法不方便,我总结了本文(很全) 官网下载Linux版anaconda, 地址https://www.anaconda.com/download/# ...
- 杨其菊201771010134《面向对象程序设计(Java)》第三周学习总结
<面向对象程序设计(Java)>第三周学习总结 第一部分:理论知识 这周课程没有新进度,由于感觉对基础语法的不熟悉,复习了一遍前三章的细碎知识,学到一些之前不知道的原理: 1.计算机高级语 ...
- AX_InventCounting
static void Job649(Args _args) { ItemId ItemId = "000XA00612R1& ...