我最近通过aws启动了一个redis数据库,并想用一些初始的伪代码做一些测试。在同一vpc内的ec2示例上运行代码时,我遇到了一个错误: redis.exceptions.ResponseError: invalid DB index
.
代码如下:
import redis
import pickle
import zlib
EXPIRATION_SECONDS = 600
r = redis.Redis(host='dse-inventory-orders.xxxxxx.xxxxxx.xxxxxx.cache.amazonaws.com',
port=6379, db='dse-inventory-orders')
r.set('foo', 'bar')
r.get('foo')
logger.info(f"r.get('foo'): {r.get('foo')}")
这是错误信息。你知道我做错了什么吗
Traceback (most recent call last):
File "/tmp/scripts/redis_test.py", line 25, in <module>
r.set('foo', 'bar')
File "/home/ec2-user/anaconda3/envs/solver_env/lib/python3.7/site-packages/redis/client.py", line 1801, in set
return self.execute_command('SET', *pieces)
File "/home/ec2-user/anaconda3/envs/solver_env/lib/python3.7/site-packages/redis/client.py", line 898, in execute_command
conn = self.connection or pool.get_connection(command_name,**options)
File "/home/ec2-user/anaconda3/envs/solver_env/lib/python3.7/site-packages/redis/connection.py", line 1192, in get_connection
connection.connect()
File "/home/ec2-user/anaconda3/envs/solver_env/lib/python3.7/site-packages/redis/connection.py", line 567, in connect
self.on_connect()
File "/home/ec2-user/anaconda3/envs/solver_env/lib/python3.7/site-packages/redis/connection.py", line 664, in on_connect
if nativestr(self.read_response()) != 'OK':
File "/home/ec2-user/anaconda3/envs/solver_env/lib/python3.7/site-packages/redis/connection.py", line 756, in read_response
raise response
redis.exceptions.ResponseError: invalid DB index
ERROR conda.cli.main_run:execute(39): Subprocess for 'conda run ['/bin/bash', '/usr/local/bin/fetch_and_run.sh', 'pythonRunnerCplex.sh', 's3://srdata-lab/ipredict_dev/dynamic_scheduling_engine/scripts/redis_test.py', 'redis_test.py']' command failed. Stderr was:
Traceback (most recent call last):
File "/tmp/scripts/redis_test.py", line 25, in <module>
r.set('foo', 'bar')
File "/home/ec2-user/anaconda3/envs/solver_env/lib/python3.7/site-packages/redis/client.py", line 1801, in set
return self.execute_command('SET', *pieces)
File "/home/ec2-user/anaconda3/envs/solver_env/lib/python3.7/site-packages/redis/client.py", line 898, in execute_command
conn = self.connection or pool.get_connection(command_name,**options)
File "/home/ec2-user/anaconda3/envs/solver_env/lib/python3.7/site-packages/redis/connection.py", line 1192, in get_connection
connection.connect()
File "/home/ec2-user/anaconda3/envs/solver_env/lib/python3.7/site-packages/redis/connection.py", line 567, in connect
self.on_connect()
File "/home/ec2-user/anaconda3/envs/solver_env/lib/python3.7/site-packages/redis/connection.py", line 664, in on_connect
if nativestr(self.read_response()) != 'OK':
File "/home/ec2-user/anaconda3/envs/solver_env/lib/python3.7/site-packages/redis/connection.py", line 756, in read_response
raise response
redis.exceptions.ResponseError: invalid DB index
1条答案
按热度按时间svmlkihl1#
在建立联系的同时;
你曾经
db
作为dse-inventory-orders
(字符串),但根据文档;选择具有指定的基于零的数字索引的redis逻辑数据库。新连接始终使用数据库0。
所以换成
0
,它会起作用的。