Window 下安装
下载地址:https://github.com/MSOpenTech/redis/releases。
下载到的Redis支持32bit和64bit。根据自己实际情况选择,将64bit的内容cp到自定义盘符安装目录取名redis。如 C:\reids
打开一个cmd窗口 使用cd命令切换目录到 C:\redis 运行 redis-server.exe redis.conf 。
如果想方便的话,可以把redis的路径加到系统的环境变量里,这样就省得再输路径了,后面的那个redis.conf可以省略,如果省略,会启用默认的。输入之后,会显示如下界面:
![Redis 安装](https://i-blog.csdnimg.cn/blog_migrate/8e9eb04c5fd2e17af6e64430cfc7a714.jpeg)
这时候另启一个cmd窗口,原来的不要关闭,不然就无法访问服务端了。
切换到redis目录下运行 redis-cli.exe -h 127.0.0.1 -p 6379 。
设置键值对 set myKey abc
取出键值对 get myKey
![Redis 安装](https://i-blog.csdnimg.cn/blog_migrate/d688873e02afa15fb5e9abd1b41b2b97.jpeg)
Linux 下安装
下载地址:http://redis.io/download,下载最新文档版本。
本教程使用的最新文档版本为 2.8.17,下载并安装:
$ wget http://download.redis.io/releases/redis-2.8.17.tar.gz $ tar xzf redis-2.8.17.tar.gz $ cd redis-2.8.17 $ make
make完后 redis-2.8.17目录下会出现编译后的redis服务程序redis-server,还有用于测试的客户端程序redis-cli,两个程序位于安装目录 src 目录下:
下面启动redis服务.
$ cd src $ ./redis-server
注意这种方式启动redis 使用的是默认配置。也可以通过启动参数告诉redis使用指定配置文件使用下面命令启动。
$ cd src $ ./redis-server redis.conf
redis.conf是一个默认的配置文件。我们可以根据需要使用自己的配置文件。
启动redis服务进程后,就可以使用测试客户端程序redis-cli和redis服务交互了。比如:
$ cd src $ ./redis-cli redis> set foo bar OK redis> get foo "bar" 安装之后……………… 我们现在做 让php识别redis 你的机器上能正常使用 PHP。接下来让我们安装 PHP redis 驱动:下载地址为:https://github.com/phpredis/phpredis/releases。PHP安装redis扩展
以下操作需要在下载的 phpredis 目录中完成:
$ wget https://github.com/phpredis/phpredis/archive/2.2.4.tar.gz $ cd phpredis-2.2.7 # 进入 phpredis 目录 $ /usr/local/php/bin/phpize # php安装后的路径 $ ./configure --with-php-config=/usr/local/php/bin/php-config $ make && make install如果你是 PHP7 版本,则需要下载指定分支:
git clone -b php7 https://github.com/phpredis/phpredis.git修改php.ini文件
vi /usr/local/php/lib/php.ini增加如下内容:
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20090626" extension=redis.so安装完成后重启php-fpm 或 apache。查看phpinfo信息,就能看到redis扩展。
![]()
连接到 redis 服务
<?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server sucessfully"; //查看服务是否运行 echo "Server is running: " . $redis->ping(); ?>执行脚本,输出结果为:
Connection to server sucessfully Server is running: PONG到这里就可以用啦
windows 下也是同理 好好看看 原理明白 Windows安装也简单
下面说下redis的基本数据类型
list string ……
简单的运用示例
Redis PHP String(字符串) 实例
<?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server sucessfully"; //设置 redis 字符串数据 $redis->set("tutorial-name", "Redis tutorial"); // 获取存储的数据并输出 echo "Stored string in redis:: " . $redis->get("tutorial-name"); ?>
执行脚本,输出结果为:
Connection to server sucessfully Stored string in redis:: Redis tutorial
Redis PHP List(列表) 实例
<?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server sucessfully"; //存储数据到列表中 $redis->lpush("tutorial-list", "Redis"); $redis->lpush("tutorial-list", "Mongodb"); $redis->lpush("tutorial-list", "Mysql"); // 获取存储的数据并输出 $arList = $redis->lrange("tutorial-list", 0 ,5); echo "Stored string in redis"; print_r($arList); ?>
执行脚本,输出结果为:
Connection to server sucessfully Stored string in redis Redis Mongodb Mysql
Redis PHP Keys 实例
<?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server sucessfully"; // 获取数据并输出 $arList = $redis->keys("*"); echo "Stored keys in redis:: "; print_r($arList); ?>
执行脚本,输出结果为:
Connection to server sucessfully Stored string in redis:: tutorial-name tutorial-list