Python连接Redis配置详解

Python连接Redis配置详解


操作环境: CentOS 5.4

操作步骤:

(一) 安装Redis:

[root@localhost /]# 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/N]: 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:

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

[root@localhost etc]# 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:

[root@localhost etc]# 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
(五)测试:

[root@localhost etc]# python
Python 2.6.8 (unknown, Dec  6 2012, 15:31:25)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] 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
>>>


你可能感兴趣的:(Python连接Redis配置详解)