kevin0490 发表于 2016-12-18 07:46:23

Python连接Redis配置详解


Python连接Redis配置详解

  

  操作环境: CentOS 5.4

操作步骤:
     (一) 安装Redis:
  

# yum install redis
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.ustc.edu.cn
* epel: mirrors.yun-idc.com
* extras: mirrors.yun-idc.com
* updates: mirrors.yun-idc.com
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package redis.i386 0:2.4.10-1.el5 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package          Arch            Version               Repository       Size
================================================================================
Installing:
redis            i386            2.4.10-1.el5            epel            299 k
Transaction Summary
================================================================================
Install       1 Package(s)
Upgrade       0 Package(s)
Total download size: 299 k
Is this ok : y
Downloading Packages:
redis-2.4.10-1.el5.i386.rpm                              | 299 kB   00:01
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing   : redis                                                    1/1
Installed:
redis.i386 0:2.4.10-1.el5
Complete!
(二)启动redis:  

# redis-server /etc/redis.conf
# ps -ef | grep redis
root   17699   10 12:05 ?      00:00:00 redis-server /etc/redis.conf
root   17762 176150 12:49 pts/1    00:00:00 grep redis
(三)用telnet连接并测试:  

# telnet localhost 6379
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
set key1 4
+OK
get key1
$1
4
set key2 hello
+OK
get key2
$5
hello
quit
+OK
Connection closed by foreign host.
(四)安装python client:  

# easy_install redis
Searching for redis
Reading http://pypi.python.org/simple/redis/
Best match: redis 2.7.6
Downloading https://pypi.python.org/packages/source/r/redis/redis-2.7.6.tar.gz#md5=e975ea446c40046ef6dc41c98e41a4f8
Processing redis-2.7.6.tar.gz
Running redis-2.7.6/setup.py -q bdist_egg --dist-dir /tmp/easy_install-v8JaAU/redis-2.7.6/egg-dist-tmp-6xgSQs
zip_safe flag not set; analyzing archive contents...
Adding redis 2.7.6 to easy-install.pth file
Installed /usr/local/lib/python2.6/site-packages/redis-2.7.6-py2.6.egg
Processing dependencies for redis
Finished processing dependencies for redis
(五)测试:  

# python
Python 2.6.8 (unknown, Dec6 2012, 15:31:25)
on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import redis
>>> r = redis.Redis(host='127.0.0.1', port=6379, db=0)
>>> r.set('name', 'zhangshan')
True
>>> r.get('name')
'zhangshan'
>>> r.set('age', 22)
True
>>> r.get('age')
'22'
>>> r.dbsize()
5L
>>> r.flushdb()
True
>>> r.get('name')
>>> r.dbsize()
0L
>>>



页: [1]
查看完整版本: Python连接Redis配置详解