无法从mod_lua连接到postgresql

3
我已经下载并安装了Apache 2.4.4(现在带有mod_lua模块)。像这样启用它:
--httpd.conf--
LoadModule lua_module modules/mod_lua.so
AddHandler lua-script .lua

"并且运行了一个简单的脚本,它可以工作。
--htdocs/hello.lua--"
function handle(r)
    r.content_type = "text/html"
    r:puts("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("couldn't connect!")
    end
end

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

尝试使用LuaSQL连接。 - hjpotter92
当然,我希望在mod_lua / Apache中使用新的db访问API。http://httpd.apache.org/docs/2.4/mod/mod_lua.html#databases - Sunder
是的,但要检查连接是否处于活动状态。在您的puts()调用之后,还要包括一个db:close() - hjpotter92
2个回答

1

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


是的,我已经配置了我的构建以包括dbd和postgres。谢谢。 - Sunder

0
原来是我把驱动程序名称和连接字符串都弄错了。将问题中的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()
--start using your db here

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接