openresty redis configuration

本文档详细介绍了如何配置OpenResty及其Lua模块lua-resty-mongol,并提供了使用示例。此外,还介绍了Redis的安装配置过程,包括编译安装、初始化脚本设置及服务启动配置。
PART I openresty configuration
# yum -y install perl-devel perl-ExtUtils-Embed
# wget http://openresty.org/download/ngx_openresty-1.4.3.6.tar.gz
# tar zxvf ngx_openresty-1.4.3.6.tar.gz
# ./configure --prefix=/root/test/openresty \
--with-luajit \
--with-http_iconv_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_perl_module 
--with-debug 
# gmake && gmake install

PART II lua-resty-mongol configuration
1. get the lasest package
# git clone git://github.com/bigplum/lua-resty-mongol.git
2. modify the makefile
# vi Makefile
OPENRESTY_PREFIX=/root/test/openresty

PREFIX ?= /root/test/openresty
LUA_INCLUDE_DIR ?= $(PREFIX)/luajit/include/luajit-2.0
LUA_LIB_DIR ?= $(PREFIX)/lualib
INSTALL ?= install

.PHONY: all test install

all: ;

install: all
        $(INSTALL) -d $(LUA_LIB_DIR)/resty/mongol
        $(INSTALL) lib/resty/mongol/*.lua $(LUA_LIB_DIR)/resty/mongol


test:
        PATH=$(OPENRESTY_PREFIX)/nginx/sbin:$$PATH prove -I../test-nginx/lib -r t
3. installation 
# make install
4. test mongodb.lua
-- example.lua
local mongo = require "resty.mongol"
conn = mongo:new()
conn:set_timeout(1000)
ok, err = conn:connect("192.168.1.208","27017")
if not ok then
    ngx.say("connect failed: "..err)
end
local db = conn:new_db_handle ( "ddb" )
db:auth("admin","admin")
col = db:get_col("testData")
r = col:find_one({name})
ngx.say(r["name"])


-- testmongodb.lua
local mongo = require "resty.mongol"
conn = mongo:new()
ok, err = conn:connect("192.168.1.208","27017")
if not ok then
    ngx.say("connect failed: "..err)
end

local db = conn:new_db_handle("ddb")
db:auth("admin","admin")
col = db:get_col("ddb")

r, err = col:insert(, nil, true)

if not r then
    ngx.status = 400
    ngx.say("not ok")
else
    ngx.say("ok")
end

local ok, err = conn:set_keepalive(0,1000)
if not ok then
    ngx.say("failed to set keepalive: ", err)
    return
end

PART III redis configuration
1. download redis
wget http://download.redis.io/releases/redis-2.6.16.tar.gz
tar zxvf redis-2.6.16.tar.gz
# make or make CFLAGS="-march=i686"  
# make test

2. install tcl
wget http://downloads.sourceforge.net/project/tcl/Tcl/8.6.1/tcl8.6.1-src.tar.gz?r=http%3A%2F%2Fwww.linuxfromscratch.org%2Fblfs%2Fview%2Fsvn%2Fgeneral%2Ftcl.html&ts=1384333410&use_mirror=jaist
cd unix &&
./configure --prefix=/usr           \
            --without-tzdata        \
            --mandir=/usr/share/man \
            $([ $(uname -m) = x86_64 ] && echo --enable-64bit) &&
make &&

sed -e "s@^\(TCL_SRC_DIR='\).*@\1/usr/include'@" \
    -e "/TCL_B/s@='\(-L\)\?.*unix@='\1/usr/lib@" \
    -i tclConfig.sh
    
make install &&
make install-private-headers &&
ln -v -sf tclsh8.6 /usr/bin/tclsh &&
chmod -v 755 /usr/lib/libtcl8.6.so
# which tclsh
/usr/bin/tclsh

Command Explanations
--without-tzdata: This switch prevents installation of the shipped timezone data which are older than the ones provided in LFS.
$([ $(uname -m) = x86_64 ] && echo --enable-64bit): This switch is used to enable 64 bit support in Tcl on 64 bit operating systems.
make install-private-headers: This command is used to install the Tcl library interface headers used by other packages if they link to the Tcl library.
ln -v -sf tclsh8.6 /usr/bin/tclsh: This command is used to create a compatibility symbolic link to the tclsh8.6 file as many packages expect a file named tclsh.
sed -e ... tclConfig.sh: The Tcl package expects that its source tree is preserved so that packages depending on it for their compilation can utilize it. This sed removes the references to the build directory and replaces them with saner system-wide locations.

3. In order to install Redis binaries into /usr/local/bin
# make install

4. complete the redis configuration
1) copy the initlial scripts
# cp redis-2.6.16/utils/redis_init_script /etc/init.d/redis_6379

2) create the necessary directory
# mkdir /etc/redis
# mkdir -p /var/redis/6379

3) modify the configuration of redis
# cp redis-2.6.16/redis.conf /etc/redis/6379.conf
# daemonize => yes, pidfile => /var/run/redis_6379.pid, port => 6379, (153)dir => /var/redis/6379 

4) modify the program to boot
# chkconfig: 345 26 74
# description: start the redis server
chkconfig --add redis_6379
chkconfig redis_6379 on


转载于:https://my.oschina.net/u/1449160/blog/198640

### 如何在 OpenResty 中配置 Redis 密码 在 OpenResty 中使用 `resty.redis` 模块连接 Redis 时,如果 Redis 配置了密码认证,则需要在连接后调用 `auth` 方法进行密码验证。以下是具体的实现示例和相关说明: #### 示例代码 以下是一个完整的示例,展示如何在 OpenResty 中配置 Redis 密码并完成连接: ```lua local redis = require "resty.redis" -- 引入 resty.redis 模块 local red = redis:new() -- 创建 Redis 对象 -- 设置连接超时时间(单位:毫秒) red:set_timeouts(1000, 1000, 1000) -- 连接到 Redis 数据库 local ok, err = red:connect("127.0.0.1", 6379) if not ok then ngx.say("failed to connect: ", err) return end -- 如果 Redis 配置了密码,则需要进行认证 local auth_ok, auth_err = red:auth("yourpassword") -- 替换为实际的 Redis 密码 if not auth_ok then ngx.say("failed to authenticate: ", auth_err) red:close() return end -- 执行 Redis 命令(例如设置一个键值对) local res, err = red:set("key", "value") if not res then ngx.say("failed to set key: ", err) red:close() return end ngx.say("key set successfully") -- 断开连接 local ok, err = red:close() if not ok then ngx.say("failed to close: ", err) return end ``` #### 关键点说明 1. **引入模块**:通过 `require "resty.redis"` 引入 Redis 模块[^3]。 2. **创建对象**:使用 `redis:new()` 创建 Redis 对象实例。 3. **设置超时**:调用 `set_timeouts` 方法设置连接、读取和写入的超时时间[^3]。 4. **连接数据库**:通过 `connect` 方法指定 Redis 的地址和端口进行连接。 5. **密码认证**:调用 `auth` 方法传入 Redis 密码以完成身份验证。 6. **执行命令**:在认证成功后,可以正常执行 Redis 命令。 7. **断开连接**:操作完成后调用 `close` 方法释放连接资源。 #### 注意事项 - 确保 Redis 服务器已配置密码,并且密码正确无误[^1]。 - 如果 Redis 使用了非默认端口,请将 `connect` 方法中的端口号替换为实际使用的端口。 - 在生产环境中,建议将密码存储在安全的地方(如环境变量或配置文件),避免直接硬编码在代码中。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值