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服务,每天会产生大量的日志,这些 ...
随机推荐
- board_led.h/board_led.c
/******************************************************************************* Filename: board_led ...
- Array.prototype.push.apply(a,b)和Array.prototype.slice.call(arguments)
Array.prototype.push.apply(a,b) 时常看到在操作数组的时候有这样的写法: var a = [1,2,3]; var b = [4,5,6]; a.push.apply(a ...
- mysql_day01
1.MySQL概述 1.什么是数据库 数据库是一个存储数据的仓库 2.都有哪些公司在用数据库 金融机构.游戏网站.购物网站.论坛网站 ... ... 3.提供数据库服务的软件 1.软件分类 MySQL ...
- SQL Server日志文件过大 大日志文件清理方法 不分离数据库
SQL Server日志文件过大 大日志文件清理方法 ,网上提供了很多分离数据库——〉删除日志文件-〉附加数据库 的方法,此方法风险太大,过程也比较久,有时候也会出现分离不成功的现象.下面的方式 ...
- 绑定checkedComboBox
using System; namespace CommonLib{ /// <summary> /// CommonCode 的摘要说明. /// </summary> [S ...
- Python学习笔记-常用内置函数
输出:print() 功能:输出打印 语法:print(*objects, sep=' ', end='\n', file=sys.stdout) 参数:objects----复数,表示可以一次输出多 ...
- Springboot学习06-Spring AOP封装接口自定义校验
Springboot学习06-Spring AOP封装接口自定义校验 关键字 BindingResult.Spring AOP.自定义注解.自定义异常处理.ConstraintValidator 前言 ...
- 七、eclipse添加离线约束,使不联网也能有一些代码的提示,例如dubbo
eclipse添加离线约束,使不联网也能有一些代码的提示,例如dubbo 1.将dubbo.xsd文件放到一个无中文目录下 2.eclipse->windows->preferences- ...
- application/json和application/x-www-form-urlencoded使用选择
一.参考资料 选application/x-www-form-urlencoded还是application/json? @RequestBody应用 二.理解 1.@RequestBody的作用 注 ...
- Python之路(第三十篇) 网络编程:socket、tcp/ip协议
一.客户端/服务器架构 1.硬件C/S架构(打印机) 打印机作为一个服务端,电脑连接打印机进行打印 2.软件C/S架构 互联网中处处是C/S架构 如谷歌网站是服务端,你的浏览器是客户端(B/S架构也是 ...