巨蟒|Redis连接但卡在Set或Get上

h9vpoimq  于 2023-01-20  发布在  Redis
关注(0)|答案(1)|浏览(190)

目标:通过.Python成功地将set()get()键值对转换到本地Redis
我可以连接到Redis。一个可能的问题是防火墙关闭了端口6379。但是,它是打开的。
连接使用或不使用参数:一米三氮一x一米四氮一x。
我怀疑问题出在Python上,因为我可以通过.终端:

127.0.0.1:6379> SET my-key TESTVAL
OK
127.0.0.1:6379> GET my-key
"TESTVAL"
127.0.0.1:6379> DEL my-key
(integer) 1
127.0.0.1:6379> GET my-key
(nil)
    • 代码**
import redis

redis_host = 'localhost'  # 127.0.0.1
redis_port = 6379
redis_password = ''  # 'your-redis-password'
redis_db = 2
redis_max_connections = 100

client = redis.Redis(host=redis_host, port=redis_port, password=redis_password, ssl=True, db=redis_db,
                           max_connections=redis_max_connections)
print('Connected!')

key = 'KEY'
value = 'VALUE'

client.set(key, value)
print('Data stored on Redis with key: ', key)

data = client.get(key)
print('Data retrieved from Redis with key: ', key)
print(data)
    • 运行时间**
(venv) me@laptop:~/GitHub/project$ python3 foo/bar/minimal_working_example.py
Connected!
|

Redis服务器已上线:

(base) me@laptop:~$ redis-server
4965:C 17 Jan 2023 09:36:56.119 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4965:C 17 Jan 2023 09:36:56.119 # Redis version=7.0.7, bits=64, commit=00000000, modified=0, pid=4965, just started
4965:C 17 Jan 2023 09:36:56.119 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
4965:M 17 Jan 2023 09:36:56.119 * Increased maximum number of open files to 10032 (it was originally set to 1024).
4965:M 17 Jan 2023 09:36:56.119 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 7.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 4965
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

4965:M 17 Jan 2023 09:36:56.120 # Server initialized
4965:M 17 Jan 2023 09:36:56.120 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
4965:M 17 Jan 2023 09:36:56.120 * Ready to accept connections

在新终端中测试:

(base) me@laptop:~$ redis-cli
127.0.0.1:6379> ping
PONG

端口6379打开:

(base) me@laptop:~$ nc -z localhost 6379
(base) me@laptop:~$ telnet localhost 6379
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
fdbelqdn

fdbelqdn1#

我希望我的解决方案是宏伟的...
只要不传递参数就可以设置Redis客户机的默认参数。

client = redis.Redis()

如果您没有指定localhostport地址等,Redis将使用本地config文件中的默认值。

相关问题