官网链接:https://openresty.org/cn/getting-started.html
[root@vm5 ~]# tar zxvf drizzle7-2011.07.21.tar.gz
[root@vm5 ~]# cd drizzle7-2011.07.21
[root@vm5 drizzle7-2011.07.21]# ./configure –without-server
[root@vm5 drizzle7-2011.07.21]# make libdrizzle-1.0
[root@vm5 drizzle7-2011.07.21]# make install-libdrizzle-1.0
2、安装 OpenResty
[root@vm5 ~]# tar zxvf ngx_openresty-1.2.4.14.tar.gz
[root@vm5 ~]# cd ngx_openresty-1.2.4.14
[root@vm5 ngx_openresty-1.2.4.14]# ./configure –prefix=/usr/local/openresty –with-luajit –with-http_drizzle_module –with-libdrizzle=/usr/local
[root@vm5 ngx_openresty-1.2.4.14]# gmake
[root@vm5 ngx_openresty-1.2.4.14]# gmake install
3、创建测试数据
Query OK, 0 rows affected (0.00 sec)
mysql> insert into users values(1,’zhangsan’,24);
Query OK, 1 row affected (0.00 sec)
mysql> insert into users values(2,’lisi’,26);
Query OK, 1 row affected (0.00 sec)
4、编辑 nginx.conf 配置文件
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream mysql {
drizzle_server 127.0.0.1:3306 dbname=test user=root protocol=mysql;
}
server {
listen 80;
server_name localhost;
root html;
index index.html index.htm;
location / {
drizzle_pass mysql;
drizzle_query “select id,username,age from users where id=1”;
rds_json on;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
5、测试配置文件、启动 nginx
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
[root@vm5 conf]# /usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
[root@vm5 conf]# netstat -ntupl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local AddressForeign AddressStatePID/Program name
tcp000.0.0.0:33060.0.0.0:*LISTEN28013/mysqld
tcp000.0.0.0:800.0.0.0:*LISTEN28036/nginx.conf
tcp00:::22:::*LISTEN2901/sshd
6、测试
[{“id”:1,”username”:”zhangsan”,”age”:24}]
ok,效果出来了,直接用 nginx 去读 mysql 数据,而且输出的数据格式是json格式的,可用ajax折腾,是不是很牛X啊!
测试
[{“id”:1,”username”:”zhangsan”,”age”:24}]
[root@vm5 nginx]# curl localhost/?id=2
[{“id”:2,”username”:”lisi”,”age”:26}]
ok,可以接收参数了,但是这么配置存在一个bug,那就是没有传递参数时会报错,这只是一个测试用例,要想在线上使用需要完善它
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root html;
index index.html index.htm;
location / {
content_by_lua ‘
local mysql = require “resty.mysql”
local db,err = mysql:new()
if not db then
ngx.say(“failed to instantiate mysql: “,err)
return
end
db:set_timeout(1000)
local ok,err,errno,sqlstate = db:connect{
host = “127.0.0.1”,
port = 3306,
database = “test”,
user = “root”,
password = “”,
max_package_size = 1024
}
if not ok then
ngx.say(“failed to connect: “, err, “: “, errno, ” “, sqlstate)
return
end
ngx.say(“connected to mysql.”)
res,err,errno,sqlstate = db:query(“select id,username,age from users where id=1”)
if not res then
ngx.say(“bad result: “, err, “: “, errno, “: “, sqlstate, “.”)
return
end
local cjson = require “cjson”
ngx.say(“result: “,cjson.encode(res))
‘;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
[root@vm5 conf]# killall -HUP nginx
connected to mysql.
result: [{“username”:”zhangsan”,”age”:24,”id”:1}]
ok,输出json格式数据了,结果也是对的。赞一个!
测试配置、重启服务,略
connected to mysql.
result: [{“username”:”lisi”,”age”:26,”id”:2}]
[root@vm5 conf]# curl localhost/?id=1
connected to mysql.
result: [{“username”:”zhangsan”,”age”:24,”id”:1}]
再一次ok,效果不错吧!