openresty/lua-resty-redis

Failed to connect with non zero database

leanghychhay opened this issue · 2 comments

I try to connect with non-zero databases but it seems that the select() method not working.

local remote_addr = ngx.var.remote_addr
local k = "help" .. remote_addr
local redis = require "resty.redis"
local red = redis:new()

red:set_timeouts(100)
red:select(1)

local ok, err = red:connect("redis ip address", 6379)
if not ok then
    ngx.log(ngx.ERR, "Redis connection failure: " .. err)
    return
end

local count, err = red:incr(k)

And here is the error log

2021/02/10 15:31:39 [error] 61999#61999: *2563 attempt to send data on a closed socket: u:0000000000000000, c:0000000000000000, ft:0 eof:0, client: 117.20.113.11, server: help.fc2.com, request: "GET / HTTP/1.1", host: "xxx.com"

You can only select the database after you connect to redis. Connect first, then select the database.

Thank you now it has been solved

local remote_addr = ngx.var.remote_addr
local k = "help" .. remote_addr
local redis = require "resty.redis"
local red = redis:new()

red:set_timeout(100)

local ok, err = red:connect("redis ip address", 6379)
if not ok then
    ngx.log(ngx.ERR, "Redis connection failure: " .. err)
    return
end

local ok, err = red:select(1)
if not ok then
     ngx.log(nginx.ERR, "failed to select: ", err)
     return
end

local count, err = red:incr(k)