ERR Lua script attempted to access nonexistent global variable: Lua script attempted to access a nonexistent global variable(Lua 脚本试图访问不存在的全局变量)
Redis含义
通过 EVAL/EVALSHA 运行的 Lua 脚本试图访问未定义的全局变量。为保护脚本环境,Redis 限制创建/读取全局变量,并将此类访问作为错误返回。
原因
在脚本中误用了全局变量而非局部变量(忘记写 local 关键字);因拼写错误访问了未定义的名称;或试图在全局变量中保存状态。Redis 的 Lua 沙箱环境不允许这样做。
解决方法
在脚本中用 local 声明所有变量:local x = ... 修正拼写错误;确保每个变量在使用前已定义。脚本应当无状态;持久状态请使用 Redis 键(KEYS/redis.call),而不是全局 Lua 变量。保持脚本简短且具有确定性。
同一数据库的其他条目
- NOPROTO unsupported protocol versionUnsupported RESP protocol version(不支持的 RESP 协议版本)
- ERR Unknown option or number of arguments for CONFIG SETUnknown option or number of arguments for CONFIG SET (older versions: Unsupported CONFIG parameter)(CONFIG SET 的选项未知或参数数量错误)
- ERR MULTI calls can not be nestedMULTI calls can not be nested(MULTI 调用不能嵌套)
- ERR unknown commandUnknown command(未知命令)
- TRYAGAIN Multiple keys request during rehashing of slotMultiple keys request during rehashing of slot (cluster)(槽重新哈希期间的多键请求)
- ERR DISCARD without MULTIDISCARD without MULTI(未执行 MULTI 就调用 DISCARD)
- BUSYGROUP Consumer Group name already existsConsumer group already exists(消费者组已存在)