client端对于不同数据结构是使用不同的命令
这里说一下redis的安装
虚拟机环境: centos
1 wget make 安装
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
注:这里记录一下我安装过程中出现的问题:
make: Warning: File `Makefile' has modification time 5.4e+06 s in the future
cd src && make all
make[1]: Entering directory `/redis/redis-2.4.7/src'
make[1]: Warning: File `Makefile' has modification time 5.4e+06 s in the future
MAKE hiredis
make[2]: Entering directory `/redis/redis-2.4.7/deps/hiredis'
make[2]: Warning: File `Makefile' has modification time 5.4e+06 s in the future
cc -c -std=c99 -pedantic -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb net.c
make[2]: cc: Command not found
make[2]: *** [net.o] Error 127
make[2]: Leaving directory `/redis/redis-2.4.7/deps/hiredis'
make[1]: *** [dependencies] Error 2
make[1]: Leaving directory `/redis/redis-2.4.7/src'
make: *** [all] Error 2
第一个问题
make: Warning: File `Makefile' has modification time 5.4e+06 s in the future
系统时间调整错了,调过来就好了
第二个问题:
make[2]: Entering directory `/redis/redis-2.4.7/deps/hiredis'
cc -c -std=c99 -pedantic -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb net.c
make[2]: cc: Command not found
没安装gcc,
yum install gcc-c++
第三个问题:
make的时候显示
make[1]: Entering directory `/redis/redis-2.4.7/src'
which: no tclsh8.5 in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
You need 'tclsh8.5' in order to run the Redis test
没安装tcl
按照官网http://www.linuxfromscratch.org/blfs/view/cvs/general/tcl.html 上的安装
安装完成之后,make成功!
安装成功之后会在src文件夹内有redis-server和redis-cli两个命令
建议将其放到bin下
sudo cp redis-server /usr/local/bin/
sudo cp redis-cli /usr/local/bin/
好了,现在redis就安装成功了
2 测试redis安装情况
我只在一台虚拟机上安装了redis,所以这台虚拟机既是服务器,又是客户端
测试:
1使用secureRt打开一个会话,redis-server,让其作为服务器运行
[19282] 19 Feb 23:52:57 - 1 clients connected (0 slaves), 726248 bytes in use
[19282] 19 Feb 23:53:02 - DB 0: 1 keys (0 volatile) in 4 slots HT.
[19282] 19 Feb 23:53:02 - 1 clients connected (0 slaves), 726248 bytes in use
[19282] 19 Feb 23:53:07 - DB 0: 1 keys (0 volatile) in 4 slots HT.
[19282] 19 Feb 23:53:07 - 1 clients connected (0 slaves), 726248 bytes in use
2打开另一个会话:
ast login: Tue Feb 19 22:49:49 2013 from 192.168.1.103
set key和get key都正确
redis搭建测试通过
首先下载一个redis的安装包:
我下载的第一个:
https://github.com/dmajkic/redis/downloads
解压后,得到一个redis的文件夹,打开文件夹得到如下图的一些文件:
安装包中是不提供redis.conf的,关于配置可以到网上搜索一下,或者从这里直接下载:
http://pan.baidu.com/share/link?shareid=4010&uk=117752620
下载后可以将redis.conf放到上图所示位置!
用命令行(win键+r),切换到redis的根目录,然后启动redis服务端即redis-server.exe,如下图:
启动cmd窗口要一直开着,关闭后则Redis服务关闭。
这时服务开启着,另外开一个窗口进行,设置客户端:
输入命令:redis-cli.exe -h 192.168.124.102(ps:这个ip是你本机ip,可以在命令行输入ipconfig查看) -p 6379
输入后如下图所示:
现在可以使用啦。
在windows下安装php的redis扩展非常简单,下载一个.dll扩展包放到php的ext目录下,在php.ini里边添加一行配置就可以了.
这里提供php5.3版本的redis扩展包: http://pan.baidu.com/share/link?shareid=4012&uk=117752620
解压并放到php的ext目录下,打开php.ini,增加一行:
extension=php_redis.dll
然后,重启apache或者nginx或者IIS就可以了.
检测是否安装成功,可以打开phpinfo看下:
然后在php代码中测试:
- <?php
- $redis = new Redis();
- $redis->connect('127.0.0.1', 6379);
- $redis->set('key', 'value');
- echo $redis->get('key')."\n";
- $redis->setex('key', 3600, 'value'); // sets key → value, with 1h TTL.
- $redis->set('key1', 'val1');
- $redis->set('key2', 'val2');
- $redis->set('key3', 'val3');
- $redis->set('key4', 'val4');
- $redis->delete('key1', 'key2');
- echo $redis->get('key3')."\n" ;
- $redis->delete(array('key3', 'key4'));
输出为:value val3