openresty lua操作mongodb

准备

wget -c https://github.com/1030907690/public-script/raw/master/generic/install-openresty-centos.sh && sh install-openresty-centos.sh
  • 下载三个依赖
git clone https://github.com/isage/lua-resty-moongoo.git
git clone git://github.com/mongodb/libbson.git
git clone https://github.com/isage/lua-cbson.git

安装依赖

lua-resty-moongo

  • 下载下来后,把moongoo.luamoongoo文件夹复制到lualib/resty,如下图所示。
    在这里插入图片描述
    在这里插入图片描述

libbson

  • 执行以下命令。如下图所示。

先yum install libtool安装一下,否则执行./autogen.sh 可能报错。

cd libbson
./autogen.sh 
make
make install
make clean && make LUA_INCLUDE_DIR=/usr/local/openresty/luajit/include/luajit-2.1 LUA_CMODULE_DIR=/usr/local/openresty/lualib LUA_MODULE_DIR=/usr/local/openresty/lualib CBSON_CFLAGS="-g -fpic -I/usr/local/include/libbson-1.0/ " CC=cc

在这里插入图片描述
在这里插入图片描述

lua-cbson

  • 执行以下命令,如下图所示。
cd lua-cbson
mkdir build  
cd build  
cmake ..  
make  
make install

在这里插入图片描述

使用

Openresty配置文件

...省略...
    location / {
    	 default_type text/html;
	    content_by_lua_file conf/lua_src/video_openapi.lua;    
    }
    ...省略...

操作MongoDB数据库Lua测试代码

local moongoo = require("resty.moongoo") -- 引入依赖
local cbson = require("cbson") 
local mg, err = moongoo.new("mongodb://root:root@127.0.0.1:27017") -- 创建连接
if not mg then
  error(err) -- 如果有错误
 end
local col = mg:db("mm_video"):collection("video_config") -- 选择db和表
-- 插入数据
local ids, err = col:insert({ videoName = "bar",vipVideoPlayUrl= "test"})
mg:close() --关闭连接

Api文档地址: https://github.com/isage/lua-resty-moongoo,也可以自己找其他的库也条件的自己写也行。

测试结果

  • 成功插入数据,如下图所示。
    在这里插入图片描述

常见问题整理

[error] 26883#0: *1673522 lua entry thread aborted: runtime error: error loading module ‘cbson’ from file ‘/usr/local/openresty/luajit/lib/lua/5.1/cbson.so’: libbson-1.0.so.0: cannot open shared object file: No such file or directory stack traceback:

这个问题发生在代码运行阶段 。

  • 建立libbson-1.0.so.0软链接并刷新和系统共享的动态链接库
ln -s /usr/local/lib/libbson-1.0.so.0  /usr/lib/libbson-1.0.so.0
ldconfig

posted on 2021-03-17 15:20  愤怒的苹果ext  阅读(373)  评论(0编辑  收藏  举报

导航