openresty + lua 1、openresty 连接 mysql,实现 crud,openrestycrud


  最近开发一个项目,公司使用的是 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(1000) local ok, err, errno, sqlstate = db:connect{ host = "127.0.0.1", port = 3306, database = "ngx_test", user = "root", password = "000000", max_packet_size = 1024 * 1024 } if not ok then ngx.say("connect mysql failed") return false end return db end return connectMysqlUtil View Code

  

  然后来一个例子,引入这个工具类,来实现 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", 10) if not res then ngx.say("bad result: ", err, ": ", errcode, ": ", sqlstate, ".") return end local cjson = require "cjson" ngx.say("result: ", cjson.encode(res)) View Code

  好了,搞定,收工。来再配置 openresty,来浏览器请求一下,看看效果吧.

  openresty 如下:

server {
    listen 6699;
    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 的就到这里,这个比较简单,也没遇到什么问题,所以先到这里。

相关内容

    暂无相关文章