整理自 Redis QuickStart http://redis.io/topics/quickstart
Redis 源码使用C 编写, 不依赖其他软件包,只要系统有gcc 和libc 就可以使用如下 基于源码的编译安装,不推荐使用系统的包管理器(如yum) 安装redis, 理由是安装后的版本可能不是最新的稳定版
wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.tar.gz cd redis-stable make
由于系统没有安装tcl, 刚开始执行make test 报错,
[root@cos65 redis-stable]# make test cd src && make test make[1]: Entering directory `/root/redis-stable/src' You need tcl 8.5 or newer in order to run the Redis test make[1]: *** [test] 错误 1 make[1]: Leaving directory `/root/redis-stable/src' make: *** [test] 错误 2
\o/ All tests passed without errors!
make 之后在 src 目录下编译生成如下binary
[root@cos65 src]# ls redis-* | grep -v "\.c\|\.o" redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server
<strong>redis-server</strong> is the Redis Server itself. <strong>redis-sentinel</strong> is the Redis Sentinel executable (monitoring and failover). <strong>redis-cli</strong> is the command line interface utility to talk with Redis. <strong>redis-benchmark</strong> is used to check Redis performances. <strong>redis-check-aof</strong> and <strong>redis-check-dump</strong> are useful in the rare event of corrupted data files.
可以手工copy 各binary到系统的相关目录(如/usr/local/bin) 也可以通过make install 完成
[root@cos65 redis-stable]# make install cd src && make install make[1]: Entering directory `/root/redis-stable/src' Hint: To run 'make test' is a good idea ;) INSTALL install INSTALL install INSTALL install INSTALL install INSTALL install make[1]: Leaving directory `/root/redis-stable/src' [root@cos65 redis-stable]# which redis-server /usr/local/bin/redis-server
[root@cos65 redis-stable]# redis-server [5507] 24 Sep 20:54:02.109 #<span style="color:#ff0000;"><strong> Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf</strong></span> [5507] 24 Sep 20:54:02.110 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 2.8.13 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in stand alone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 5507 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' [5507] 24 Sep 20:54:02.112 # Server started, Redis version 2.8.13 [5507] 24 Sep 20:54:02.112 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. 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. [5507] 24 Sep 20:54:02.112 * The server is now ready to accept connections on port 6379
以源码包中的redis.conf 为模板,copy 到/etc/ 目录,启动时指定配置文件 redis-server /etc/redis.conf
[root@cos65 redis-stable]# redis-server /etc/redis.conf [5552] 24 Sep 21:52:58.986 * Increased maximum number of open files to 10032 (it was originally set to 1024).
[root@cos65 ~]# redis-cli ping PONG [root@cos65 ~]# redis-cli 127.0.0.1:6379> ping PONG 127.0.0.1:6379> set mykey somevalue OK 127.0.0.1:6379> get mykey "somevalue" 127.0.0.1:6379>
使用python 客户端redis-py 与server 交互,
首先安装redis-py, pip install redis
[root@cos65 ~]# pip install redis Downloading/unpacking redis Downloading redis-2.10.3.tar.gz (86kB): 86kB downloaded Running setup.py egg_info for package redis warning: no previously-included files found matching '__pycache__' warning: no previously-included files matching '*.pyc' found under directory 'tests' Installing collected packages: redis Running setup.py install for redis warning: no previously-included files found matching '__pycache__' warning: no previously-included files matching '*.pyc' found under directory 'tests' Successfully installed redis Cleaning up...
通过导入 redis 模块调用客户端执行查询与设值,
[root@cos65 ~]# python Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>><strong> import redis</strong> >>> <strong>r = redis.StrictRedis(host='localhost', port=6379, db=0)</strong> >>> <strong>r.get('mykey')</strong> 'somevalue' >>><strong> r.set('foo', 'bar')</strong> True >>> <strong>r.get('foo')</strong> 'bar'