【问题标题】:Unable to write to Redis using the protocol specification无法使用协议规范写入 Redis
【发布时间】:2014-01-23 01:03:18
【问题描述】:

当我在端口 6379 上远程登录到我的 Redis 服务器时,我应该能够使用http://redis.io/topics/protocol 中概述的协议规范。相反,我得到无效的批量长度。我在初始 *3(启动协议规范)之后放置的任何内容都会引发此错误。 我在 Suse Linux Enterprise 11 上使用 Bash。

例如:

telnet localhost 6379
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
*3\r\n$5\r\nLPUSH\r\n$4\r\nlogs\r\n$20\r\n"this is some data!"\r\n
-ERR Protocol error: invalid multibulk length
Connection closed by foreign host.

甚至:

telnet localhost 6379
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
*3\r\n
-ERR Protocol error: invalid multibulk length
Connection closed by foreign host.

我使用的是应该支持协议规范的 Redis 2.6(Redis 服务器 v=2.6.13)。 我可能做错了什么? 提前致谢, 科尔

【问题讨论】:

    标签: bash nosql redis


    【解决方案1】:

    \r\n 不被 telnet 解释。它们代表回车和换行,它们只是 telnet 协议的一部分。只需按“Enter”即可从 telnet 生成它们。

    $ telnet 127.0.0.1 6379
    Trying 127.0.0.1...
    Connected to 127.0.0.1.
    Escape character is '^]'.
    *3
    $5
    LPUSH
    $4
    logs
    $20
    "this is some data!"
    :1
    quit
    +OK
    

    我建议使用任何 Perl/Python/Ruby/PHP/... 脚本来访问 Redis,而不必直接从 shell 脚本处理 Redis 协议。

    【讨论】:

    • 我没有想到已经内置到 Telnet 的回车和换行。我真正想做的是通过端口 6379 将其输入管道,在这种情况下使用 syslog-ng 写入该端口。我也尝试过类似 echo "*3\r\n$5\r\nLPUSH\r\n$4\r\nlogs\r\n$17\r\nthis is some data\r\n" | redis-cli --pipe 在 bash 中,它会导致相同的错误。我想我的下一个问题是如何将回车和换行内联流式传输到 redis。
    • 对于 syslog-ng,我猜它可以很好地解释 \r\n - 请参阅 benwilber.net/realtime-pixel-tracking-nginx-syslog-ng-redis
    • 对于 redis-cli --pipe,这完全取决于你的 shell。使用 bash 您应该编写 echo -n $'hello\r\nhello\r\n' 以正确扩展字符 - 请参阅 stackoverflow.com/questions/13195341/…
    猜你喜欢
    • 2019-06-21
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多