1. 程式人生 > 其它 >python連線redis

python連線redis

環境如下:

[root@mcw01 ~/msRedis]$ ps -ef|grep -v grep|grep redis
root      46061      1  0 14:28 ?        00:00:45 redis-sentinel *:26379 [sentinel]
root      46066      1  0 14:28 ?        00:00:44 redis-sentinel *:26380 [sentinel]
root      46071      1  0 14:28 ?        00:00:44 redis-sentinel *:26381 [sentinel]
root      
46293 1 0 15:17 ? 00:00:14 redis-server *:7000 [cluster] root 46298 1 0 15:18 ? 00:00:14 redis-server *:7001 [cluster] root 46303 1 0 15:18 ? 00:00:13 redis-server *:7002 [cluster] root 46308 1 0 15:18 ? 00:00:13 redis-server *:7003 [cluster] root 46313 1
0 15:18 ? 00:00:13 redis-server *:7004 [cluster] root 46318 1 0 15:18 ? 00:00:14 redis-server *:7005 [cluster] root 54613 1 0 18:57 ? 00:00:00 redis-server *:6380 root 54620 1 0 18:57 ? 00:00:00 redis-server *:6381 [root@mcw01 ~/msRedis]$ [root@mcw01 ~/msRedis]$ redis-cli -p 6381
info replication # Replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=6380,state=online,offset=50805,lag=1 master_replid:e795be2a39f50a18c95c77196bcb56780a062113 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:50933 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:50933 [root@mcw01 ~/msRedis]$ [root@mcw01 ~/msRedis]$ [root@mcw01 ~/msRedis]$ redis-cli -p 6381 127.0.0.1:6381> keys * 1) "name1" 127.0.0.1:6381> [root@mcw01 ~/msRedis]$

安裝了redis模組。

from redis import Redis
redis_cli= Redis(host="10.0.0.11",port=6381)
res=redis_cli.get("name1")
print(res)

連線報錯:
redis.exceptions.ResponseError: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.