最小示例如下:
conf/lua目录下有如下文件,名为testdb.lua
local mysql = require "resty.mysql"
local db = mysql:new()
db:set_timeout(300000)
local ok, err, errno, sqlstate = db:connect({
host = "127.0.0.1",
port = "3306",
database = "test",
user = "root",
password = "passwd",
max_packet_size=1048})
if not ok then
ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)
return
end
local query
query="select * from user limit 50 "
res, err, errno, sqlstate = db:query(query)
if not res then
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
return
end
local cjson = require "cjson"
ngx.say(cjson.encode(res))
nginx conf的配置中加
location = /testdb {
content_by_lua_file 'conf/lua/testdb.lua';
}
mysql数据库中user表的结构为
TABLE `user` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`user` varchar(800) DEFAULT NULL,
`passwd` varchar(20) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6070422 DEFAULT CHARSET=utf8;
通过浏览器访问
http://xxxx/testdb
刷新几次后,会发现只显示
{"user":"zxxx ","id":1},{"user":"xxxx ","id":2}.........
正常的应该显示
[{"user":"xx ","email":"
x...@xxx.net","passwd":"12344321","id":1},{"user":"xxxx
","email":"
xxx...@163.com","passwd":"xxx","id":2}..........
openresty 1.4.3.9
nginx/sbin/nginx -V
nginx version: ngx_openresty/
1.4.3.9
built by gcc 4.2.1 20070719 [FreeBSD]
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx
--add-module=../ngx_devel_kit-0.2.19
--add-module=../echo-nginx-module-0.50
--add-module=../xss-nginx-module-0.04
--add-module=../ngx_coolkit-0.2rc1
--add-module=../set-misc-nginx-module-0.23
--add-module=../form-input-nginx-module-0.07
--add-module=../encrypted-session-nginx-module-0.03
--add-module=../srcache-nginx-module-0.24
--add-module=../ngx_lua-0.9.3
--add-module=../headers-more-nginx-module-0.24
--add-module=../array-var-nginx-module-0.03
--add-module=../memc-nginx-module-0.14
--add-module=../redis2-nginx-module-0.10
--add-module=../redis-nginx-module-0.3.7
--add-module=../auth-request-nginx-module-0.2
--add-module=../rds-json-nginx-module-0.13
--add-module=../rds-csv-nginx-module-0.05
--with-ld-opt=-Wl,-rpath,/usr/local/openresty/luajit/lib
--with-http_ssl_module
uname -a:
FreeBSD
lhm.com 8.2-RELEASE FreeBSD 8.2-RELEASE #0: Fri Feb 18
02:24:46 UTC 2011
ro...@almeida.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC i386