2013-03-04 55 views
3

我已经下载并安装了Apache 2.4.4(它现在带有mod_lua模块)。启用,像这样:无法从mod_lua连接到postgresql

--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:[email protected]/db0") 
    if not err then 
    r:puts("connected!") 
    else 
    r:puts("couldn't connect!") 
    end 
end 

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

感谢您的任何意见!

+0

尝试使用LuaSQL连接脱身。 – hjpotter92 2013-03-04 21:15:04

+0

知道,但我希望能使用mod_lua /阿帕奇内的新的数据库访问的API。 http://httpd.apache.org/docs/2.4/mod/mod_lua.html#databases – rebnoob 2013-03-04 21:48:35

+0

是的,但要检查,如果连接处于活动状态。另外,在'puts()'调用之后包含'db:close()'。 – hjpotter92 2013-03-04 21:52:33

回答

0

原来我拿到了驱动程序名称和连接字符串错误。用这个替换问题中的dbacquire行应该使其工作。

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

更重要的是,通过像这样

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

在httpd.conf中嵌入这些可以通过简单地做这在你的Lua脚本

db = r:dbacquire() 
--start using your db here 
1

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

+0

是的,我配置我的版本以包含dbd和postgres。谢谢。 – rebnoob 2013-03-22 14:00:36