openresty + lua 1、openresty 連接 mysql,實現 crud


  最近開發一個項目,公司使用的是 openresty + lua,所以就研究了 openresty + 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 的就到這里,這個比較簡單,也沒遇到什么問題,所以先到這里。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM