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数据库 具体操作 ...
随机推荐
- Js 对Dom的操作
一.DOM的概述 DOM(Document Object Model,文档对象模型)描绘了一个层次化的节点树,允许开发人员添加.移除和修改页面的某一部分.这使得JavaScript操作HTML,不是在 ...
- CSS的BFC和hasLayout及其应用场景
前端精选文摘:BFC 神奇背后的原理 一.BFC是什么? 先介绍 Box.Formatting Context的概念. Box: CSS布局的基本单位 Box 是 CSS 布局的对象和基本单位, 直观 ...
- JDBC事务和数据库事务嵌套的讨论 .
首先必须执行con.setAutoCommit(false)方法,将JDBC事务设置为手动提交,否则手动提交con.commit()无效,手动回滚con.rollback()引发SQLExceptio ...
- DESCRIBE:When you mouse click right-side is open an application and click left-side is attribution.
DESCRIBE:When you mouse click right-side is open an application and click left-side is attribution. ...
- Linux内核收包过程
net/core/dev.c int __init net_dev_init(void) { queue->backlog.poll = process_backlog; open_softir ...
- Hive入门操作
Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供类SQL查询功能.本文描述了HIve的一些基本操作,如有错误之处还请指出. 常用语法 #显示相关信息 sh ...
- ctrl + alt + o 快速删除掉没有使用的 import
ctrl + alt + o 优化导入,可以快速删除掉没有使用的 import
- Perl中的字符串操作函数
1.$position = index(string,substring,skipchars): 该函数返回子串substring在字符串string中的位置,如果不存在,则返回-1:参数skipch ...
- BZOJ 3289: Mato的文件管理 【莫队 + 树状数组】
任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=3289 3289: Mato的文件管理 Time Limit: 40 Sec Memory ...
- 实例:接口并发限流RateLimiter
需求:接口每秒最多只能相应1个请求 1.创建 全局类对象 import com.google.common.util.concurrent.RateLimiter; import org.spring ...