无法从 mod_lua 连接到 postgresql。

我已经下载并安装了Apache 2.4.4(现在带有mod_lua模块)。像这样启用它:

-- httpd.conf -

LoadModule lua_module modules/mod_lua.so
AddHandler lua-script .lua

并运行了一个简单的脚本,它可以工作。

-- htdocs / hello.lua -

function handler)
     r.content_type =“text / html”
     rputs(“Hello Lua World!\ n”)
end

我现在想连接到本地的pg数据库,但无法工作。

function handle(r)
     r.content_type =“text / html”
     r:puts(“Hello Lua World!\ n”)
     local db,err = r:dbacquire(“ postgres”,“postgres://user:secret @ localhost / db0”)
     if not err then
      r:puts(“connected!”)
     else
      r:puts(“无法连接!”)
     end
end

没有任何错误消息。我错过了进一步的配置吗?

感谢任何输入!

点赞
用户2199133
用户2199133

Apache httpd 基于 APR,APR 提供了数据库连接; 因此,请确保您的 APR 安装支持您想要使用的数据库层。

2013-03-22 12:09:19
用户764370
用户764370

原来我把驱动程序名称和连接字符串搞错了。将问题中的dbacquire代码替换为以下代码即可正常运行。

db = r:dbacquire("pgsql", "hostname=localhost dbname=foo user=bar password=baz")

更好的办法是将它们嵌入到httpd.conf中,像这样

DBDriver pgsql
DBDParams "hostname=localhost dbname=foo user=bar password=baz"

这样,您可以在lua脚本中简单地执行以下操作。

db = r:dbacquire()
--在这里开始使用您的数据库
2013-03-22 14:05:37