簡體   English   中英

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

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

目標:成功set()get()鍵值對到本地Redis Python

我可以連接到 Redis。一個可能的問題是防火牆關閉了端口6379 然而,它是開放的。

連接可以使用或不使用參數: redis_dbredis_max_connections

我懷疑問題出在 Python,因為我可以通過SETGET 終端:

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 '^]'.

我希望我的解決方案是宏偉的......

簡單地不傳遞參數會設置默認參數的Redis客戶端。

client = redis.Redis()

其中,如果您不指定localhostport地址等,Redis 將使用本地config文件中的這些默認值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM