连接到Redis
主要分为运维人员的连接和程序的连接
本机非密码连接
#redis-cli
1 | #redis-cli |
跨主机非密码连接
redis-cli -h HOSTNAME/IP -p PORT
1 | redis - cli - h HOSTNAME / IP - p PORT |
跨主机密码连接
redis-cli -h HOSTNAME/IP -p PORT -a PASSWORD
1 | redis - cli - h HOSTNAME / IP - p PORT - a PASSWORD |
shell脚本写入数据到Redis
[root@centos8 ~]##cat redis_test.sh #!/bin/bash NUM=100000 PASS=123456 for i in `seq $NUM`;do redis-cli -h 127.0.0.1 -a "$PASS" set key${i} value${i} echo "key${i} value${i} 写入完成" done echo "$NUM个key写入到Redis完成"
1 2 3 4 5 6 7 8 9 10 | [ root @ centos8 ~ ] ##cat redis_test.sh #!/bin/bash NUM = 100000 PASS = 123456 for i in ` seq $ NUM ` ; do redis - cli - h 127.0.0.1 - a "$PASS" set key $ { i } value $ { i } echo "key${i} value${i} 写入完成" done echo "$NUM个key写入到Redis完成" |
python连接方式
[root@centos8 ~]#yum -y install python3 python3-redis [root@centos8 ~]#cat redis_test.py #!/bin/env python3 import redis #import time pool = redis.ConnectionPool(host="127.0.0.1", port=6379,password="") r = redis.Redis(connection_pool=pool) for i in range(100): r.set("k%d" % i,"v%d" % i) # time.sleep(1) data=r.get("k%d" % i) print(data) [root@centos8 ~]# python3 redis_test.py ...... b'v94' b'v95' b'v96' b'v97' b'v98' b'v99' [root@centos8 ~]#redis-cli 127.0.0.1:6379> get k10 "v10" 127.0.0.1:6379>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | [ root @ centos8 ~ ] #yum -y install python3 python3-redis [ root @ centos8 ~ ] #cat redis_test.py #!/bin/env python3 import redis #import time pool = redis . ConnectionPool ( host = "127.0.0.1" , port = 6379 , password = "" ) r = redis . Redis ( connection_pool = pool ) for i in range ( 100 ) : r . set ( "k%d" % i , "v%d" % i ) # time.sleep(1) data = r . get ( "k%d" % i ) print ( data ) [ root @ centos8 ~ ] # python3 redis_test.py . . . . . . b 'v94' b 'v95' b 'v96' b 'v97' b 'v98' b 'v99' [ root @ centos8 ~ ] #redis-cli 127.0.0.1 : 6379 > get k10 "v10" 127.0.0.1 : 6379 > |