openresty 配置 mongodb 可操作插件
1、下载lua-resty-mongol https://github.com/bigplum/lua-resty-mongol
2、配置_mongo.conf文件,在conf创建_mongo.conf文件,配置信息如下
#_mongo.conf
server {
listen ;
#server_name 192.168.1.128;
#关闭lua_code 缓存 location /lua {
content_by_lua_file /opt/openresty/lualib/resty/mongol/test_lua.lua;
}
location /lua_mongo {
content_by_lua_file lualib/resty/mongol/test_mongol.lua; }
location /lua_test {
set $test "hello world";
#使用acess 阶段做准入条件处理
access_by_lua '
if (ngx.var.test =="hello world") then
ngx.say("验证通过"..ngx.var.test)
else
ngx.log(ngx.ERR,"验证失败","")
end
';
#业务处理
content_by_lua '
ngx.header.content_type ="text/plain";
local a, b =;
ngx.say(ngx.var.test..a);
';
} }
然后记得把_mongo.conf配到主配置文件上,在nginx.conf里添加【include _mongo.conf; 】
3、在content_by_lua_file lualib/resty/mongol/文件夹下创建数据库的测试文件test_mongol.lua,代码如下:
local mongo =require "resty.mongol"
local json = require "cjson"
--获取连接对象
local conn =mongo:new()
conn:set_timeout()
--获取连接客户端
local ok,err =conn:connect("127.0.0.1",) if not ok then
ngx.say("connect failed"..err)
end
--获取数据库
local db = conn:new_db_handle("testdb")
--用户授权
local ok ,err = db:auth("","") if ok then
ngx.say("user auth success"..ok)
end
--获取集合
local coll = db:get_col("testtable")
--获取document集合
local cursor = coll:find({}) --json 转码
--
function json_decode( str )
local json_value =nil
pcall(function (str) json_value = json.decode(str) end, str)
return json_value
end --循环
for index,item in cursor:pairs() do
ngx.say('数据: '..index)
if not item['url'] then
ngx.say('数据:'..item["title"])
else
ngx.say('数据:'..item["title"]..item['url'])
ngx.say(json_decode(item['url']))
end end
--获取单个集合
local res =coll:find_one({key = }) if res then ngx.say(res['title'])
end --插入集合
local bson1 ={title ='哈哈',url = 'www.baidu.com',key = };
--插入table 表中
local docs ={bson1}; local rsOk,err =coll:insert(docs,,) if err then
ngx.say('error--'..err)
else
ngx.say('ok---- '..rsOk)
end --删除操作
local deOk,err = coll:delete({title ='你好'},,) if err then
ngx.say('delete error--'..err)
else
ngx.say('delete ok--'..deOk)
end --关闭连接
if conn then conn:close()
end
openresty 配置 mongodb 可操作插件的更多相关文章
- IDEA 安装配置可视化 MongDB 插件
IDEA 安装配置可视化 MongDB 插件 1.安装MongoDB插件 打开 IDEA ,file --> settings --> plugins,在右边搜索栏中输入Mongo,点击 ...
- 搭建高可用mongodb集群(一)——配置mongodb
在大数据的时代,传统的关系型数据库要能更高的服务必须要解决高并发读写.海量数据高效存储.高可扩展性和高可用性这些难题.不过就是因为这些问题Nosql诞生了. NOSQL有这些优势: 大数据量,可以通过 ...
- 【转载】CentOS6.5_X64下安装配置MongoDB数据库
[转载]CentOS6.5_X64下安装配置MongoDB数据库 2014-05-16 10:07:09| 分类: 默认分类|举报|字号 订阅 下载LOFTER客户端 本文转载自zhm&l ...
- Linux下安装配置MongoDB 3.0.x 版本数据库
说明: 操作系统:CentOS 5.X 64位 IP地址:192.168.21.128 实现目的: 安装配置MongoDB数据库 具体操作: 一.关闭SElinux.配置防火墙 1.vi /etc/s ...
- windows下安装和配置mongoDB
上次在mac下安装和配置了mongodb,这次在windows下也尝试安装和配置mongodb. 1.首先下载mongodb压缩包,下载后解压到D盘或E盘.如下: 2.配置环境变量:桌面—计算机右键— ...
- Mac下安装和配置mongoDB
mac下的mongodb下载安装比较简单,主要有两种方式,一种是下载压缩包解压,另一种是通过npm或者homebrew命令安装,这里就不赘述了, 复杂的在于mongodb运行环境的配置(若未配置运行环 ...
- maven的安装,maven库配置和Eclipse插件的安装
maven的安装,maven库配置和Eclipse插件的安装 1.下载并解压maven 2.配置环境变量 3.配置maven配置文件 1.下载链接 Downloading Apache Maven 2 ...
- 【MongoDB数据库】怎样安装、配置MongoDB
本blog以最简洁的方式记录了博主在折腾MongoDB过程中点点滴滴,当中包含下载MongoDB.配置环境变量.怎样启动MongoDBserver.怎样连接MongoDBserver以及怎样连接Mon ...
- Linux下PHP安装配置MongoDB数据库连接扩展
Web服务器: IP地址:192.168.21.127 PHP安装路径:/usr/local/php 实现目的: 安装PHP的MongoDB数据库扩展,通过PHP程序连接MongoDB数据库 具体操作 ...
随机推荐
- C语言归并排序
这篇文章是学习了小甲鱼-数据结构与算法结合自考教材编写出的代码,希望自己逐渐在算法造诣上能更上一层楼. 归并排序(递归实现) “归并”一词在中文含义中就是合并的意思,而在数据结构中的定义是将两个或者两 ...
- 浅谈用于WEBGIS开发最重要的4个HTML5特性
WebGIS是GIS与Internet相结合的产物,一般Internet的开发手段都可用于WEBGIS的开发,比较流行的有Javascript.FLash,到现在应该说市面上的WEBGIS产品和具有的 ...
- springmvc封装list个数限制问题
提交一颗树,三级区域个数大于1000个导致提交失败!!! org.springframework.beans.InvalidPropertyException: Invalid property 'd ...
- Vue2.0中的系统指令
v-on注册事件 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...
- swoole 创建web服务器
http_server.php $http = new swoole_http_server("0.0.0.0", 9501); // 请求监听事件 $http->on('r ...
- Subversion FAQ(常见问题解答)
转自:http://subversion.apache.org/faq.zh.html 常见问题: 为什么会有这样一个项目? 为了接管CVS的用户基础.确切的说,我们写了一个新的版本控制系统,它和CV ...
- jquery事件绑定函数
1.bind 使用语法: jQueryObject.bind( events [, data ], handler ) jQueryObject.bind( events [, data ] [, i ...
- 【NLP_Stanford课堂】语言模型2
一.如何评价语言模型的好坏 标准:比起语法不通的.不太可能出现的句子,是否为“真实”或"比较可能出现的”句子分配更高的概率 过程:先在训练数据集上训练模型的参数,然后在测试数据集上测试模型的 ...
- ubuntu安装最新版node和npm
1.先在系统上安装好nodejs和npm sudo apt-get install nodejs-legacy sudo apt-get install npm 2.升级n ...
- 【Leetcode】【Medium】Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...