ngx_lua 连接mysql

Posted by wxianfeng Sat, 16 Nov 2013 04:02:00 GMT

openresty 安装后, 默认包含 lua-resty-mysql, 我就是用的这个连接的 mysql, 很好用.

当一个 request 进来过后, 一般这么用:

local mysql = require "resty.mysql"
local db = mysql:new()
local cfg = { ... }
db:connect(cfg)
local res = db:query("select * from users")
ngx.say(json.encode(res))

但是如果我 require 的一个文件中也需要连接 mysql, 怎么办?

刚开始我这么用的:

a.lua

local Email = require("email")
local item = Email:get_item(5)

email.lua

local mysql = require("resty.mysql")
local db = mysql:new
db:connect(cfg)

function Email:get_item(id)
  local res = db:query("select * from emails where id = " .. id)
  local item = res[1]
  return item
end

一个 request 过来后, 直接报错:

> 2013/11/14 19:30:09 [error] 5290#0: *425 lua entry thread aborted: runtime
> error: attempt to yield across C-call boundary

出现这个原因是因为 require 的文件中, 不能直接有 mysql io 操作.

看下 春哥 的回复:

Hello!

2013/11/14 wxianfeng:
> 最新测试, 发现同时在两个lua文件中 db:connect(cfg) 不成功, 报下面错误:
>
> 2013/11/14 19:30:09 [error] 5290#0: *425 lua entry thread aborted: runtime
> error: attempt to yield across C-call boundary
>

这个错误是说你使用的某个 C 函数实现的 Lua 原语(比如 require 和 loadfile 之类)直接触发了 resty.mysql
的 I/O 操作(例如 connect 之类)。你应当避免让 require 和 loadfile 等操作直接触发 mysql 的 I/O
操作(比如把 mysql 相关的操作都放在你自己的 Lua 函数里,而不是放在 Lua 模块文件(.lua 文件)的顶层上)。

最后我把 email.lua 中连接 mysql 的操作, 放到一个函数中 解决.

local mysql = require("resty.mysql")
local db = mysql:new


local function connect_db()
  db:connect(cfg)
end

function Email:get_item(id)
  connect_db()
  local res = db:query("select * from emails where id = " .. id)
  local item = res[1]
  return item
end

This entry was posted on Sat, 16 Nov 2013 04:02:00 GMT and Posted in . You can follow any any response to this entry through the Atom feed. You can leave a comment or a trackback from your own site.

Tags , , , ,


Trackbacks

Use the following link to trackback from your own site:
http://blog.wxianfeng.com/trackbacks?article_id=319

Comments

  1. Avatar
    rumahpoker88.yolasite.com over {{count}} years ago:

    I have read so many articles or reviews about the blogger lovers but this piece of writing is in fact a
    good piece of writing, keep it up.

  2. Avatar
    rumahpoker88.yolasite.com over {{count}} years ago:

    I have read so many articles or reviews about the blogger lovers but this piece of writing is in fact a good piece
    of writing, keep it up.

  3. Avatar
    https://ratudomino88.dewalego.casa over {{count}} years ago:

    Wow, tһat’s whаt I was seeking for, what a data!

    existing heree at thiѕ weblog, tһanks admin of
    thіs site.

  4. Avatar
    https://ratudomino88.dewalego.casa over {{count}} years ago:

    Wow, that’ѕ ᴡhat I wwas seeking for, what a data!
    existing hre аt this weblog, thanks admin of tһis site.

  5. Avatar
    pokermas99.dewalego.xyz over {{count}} years ago:

    Ԍreat site. Lots оf helpful info һere. І аm sending іt to a few friends aans alѕо sharing in delicious.
    Ꭺnd obviⲟusly, thаnks in yߋur sweat!

  6. Avatar
    pokermas99.dewalego.xyz over {{count}} years ago:

    Gгeat site. Lots оf helpful nfo here. I am sending it
    to a few friends anns also sharing іn delicious.
    And obviously, thankѕ in youг sweat!

  7. Avatar
    https://pojokqq.dewalego.com/ over {{count}} years ago:

    Excellent web skte yoou һave gοt һere.. It’s difficult to fіnd hіgh quality writng ⅼike yours
    thede daуs. I truly appгeciate individuals ⅼike you!
    Tɑke care!!

  8. Avatar
    https://pojokqq.dewalego.com/ over {{count}} years ago:

    Excellent web site you hsve got herе.. It’ѕ difficult tⲟ fіnd higһ quality writing ⅼike yoսrs
    tһese dayѕ. Ι truuly appreciatte individuals ⅼike you!

    Tɑke care!!

  9. Avatar
    Phillis over {{count}} years ago:

    If you would like to improve your experience only keep visiting this website and be updated with the most
    recent news update posted here.

  10. Avatar
    Phillis over {{count}} years ago:

    If you would like to improve your experience only keep
    visiting this website and be updated with the most recent news update posted here.

  11. Avatar
    royalqq.dewalego.website over {{count}} years ago:

    Ԝhen Ι initially commrnted Ӏ clicked thе “Notify me when new comments are added” checkbox and
    noᴡ еach time a cοmment is ɑdded Ӏ gеt three emails with tһе sаme comment.
    Іs thеre any way үoս сan remove me from tthat service?
    Аppreciate it!

  12. Avatar
    https://asikqq.asia over {{count}} years ago:

    Excellen ᴡay ߋf describing, and pleasant piece օf writing t᧐ obtaikn іnformation аbout my presentation subject matter, ᴡhich i aam going to convey іn college.

  13. Avatar
    https://asikqq.asia over {{count}} years ago:

    Excellent wɑy оf describing, and pleasant pjece оf writing
    to οbtain infߋrmation about mʏ presentation subject matter, ԝhich і amm going to convey in college.

Leave a comment