redis 的安装以及主从配置

来源:互联网 发布:数据采集卡ni 编辑:程序博客网 时间:2024/05/18 01:42

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 安装

这时候另启一个cmd窗口,原来的不要关闭,不然就无法访问服务端了。

切换到redis目录下运行 redis-cli.exe -h 127.0.0.1 -p 6379

设置键值对 set myKey abc

取出键值对 get myKey

Redis 安装

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-cliredis> set foo barOKredis> 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扩展。

PHP 使用 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 sucessfullyServer 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 sucessfullyStored 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 sucessfullyStored string in redisRedisMongodbMysql

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 sucessfullyStored string in redis::tutorial-nametutorial-list

0 0