简体   繁体   English

Python | Redis 连接但卡在 Set 或 Get 上

[英]Python | Redis Connects but Stuck on Set or Get

Goal: Successfully set() and get() key-value pairs to local Redis via.目标:成功set()get()键值对到本地Redis Python . Python

I can connect to Redis. A possible issue is a firewall has closed port 6379 .我可以连接到 Redis。一个可能的问题是防火墙关闭了端口6379 However, it is open.然而,它是开放的。

Connection works with or without parameters: redis_db , redis_max_connections .连接可以使用或不使用参数: redis_dbredis_max_connections

I suspect the issue is with Python, as I'm able to SET and GET via.我怀疑问题出在 Python,因为我可以通过SETGET Terminal:终端:

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)

Code代码

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)

Runtime运行

(venv) me@laptop:~/GitHub/project$ python3 foo/bar/minimal_working_example.py
Connected!
|

Redis server is live: 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

Tested in a new Terminal:在新终端中测试:

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

Port 6379 is open:端口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 '^]'.

I wish my solution was grandiose...我希望我的解决方案是宏伟的......

Simply not passing paramters sets up a Redis client of default paramters.简单地不传递参数会设置默认参数的Redis客户端。

client = redis.Redis()

Which, if you do not specify localhost or port addresses, etc., Redis will use those default values in your local config file.其中,如果您不指定localhostport地址等,Redis 将使用本地config文件中的这些默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM