linux下redis安装与PHP redis拓展安装

安装环境:

1. 服务器环境:linux / centos6.2
查看命令:cat /etc/redhat-release

2. redis版本(http://download.redis.io/releases/redis-2.8.13.tar.gz)

3. 预装软件(gcc , tcl)
gcc: c语言编译器
tcl: redis一些组件是tcl语言编写
安装:yum -y install gcc tcl

编译安装步骤:

wget http://download.redis.io/releases/redis-2.8.13.tar.gz
tar -xf redis-2.8.13.tar.gz
cd redis-2.8.13
make(编译的二进制文件放在src目录下,用ll src/redis*可以查看,包括服务器端redis-server和客户端redis-cli)
make install(安装,把二进制文件放到usr/local/bin目录下,which redis-server命令可查看安装位置)
redis-server --help可查看帮助
启动redis服务,配置文件在解压目录下的redis.conf,可拷贝到其它地方
修改配置文件,vim redis.conf
后台启动redis
daemonize yes
端口也可更改
port 6379
启动命令:/usr/local/bin/redis-server redis.conf配置文件路径
ps aux|grep redis-server查看是否启动进程

关闭命令:/usr/local/bin/redis-cli shutdown

开机自启动,vi rc.local,加入启动命令

echo '/usr/local/bin/redis-server redis.conf配置文件路径'>> /etc/rc.local

持久化到硬盘:

1、RDB方式(默认,性能较高,但间隔时间写入硬盘,数据不安全)

dbfilename dump.rdb #同步到硬盘文件名(单文化,方便迁移)

dir ./ #表示存放在配置文件同级目录下

save 900 1 #900秒内有1个key发生变化写入硬盘
save 300 10
save 60 10000

2、AOF方式

appendonly yes

appendfilename "appendonly.aof"

appendfsync always #每次修改都写入硬盘(数据最安全)
# appendfsync everysec #每秒写入硬盘
# appendfsync no #不同步


redis客户端使用:

redis-cli --help
redis-cli -h 127.0.0.1 -p 6379
info命令查看redis-server当前状态

set string1 huangxf
get string1
set string2 4
incr string2
get string2
decrby string2 3
get string2
set string3 2.5

lpush list1 12
lpush list1 12
lpush list1 13
rpop list1
llen list1

sadd set1 12
sadd set1 13
sadd set1 13
scard set1
sismember set1 13
srem set1 13

hset hash1 key1 12
hget hash1 key1
hset hash1 key2 13
hset hash1 key3 13
hlen hash1
hmget hash1 key1 key2

zadd zset1 10.1 val1
zadd zset1 10.2 val2
zadd zset1 9.2 val3
zcard zset1
zrange zset1 0 2 withscores
zrank zset1 val2

PHP安装redis拓展:
php -m查看所有拓展,命令找不到则添加环境变量:export PATH=$PATH:/www/wdlinux/apache_php-5.4.26/bin/
已安装phpize和php-config(yum install php-devel)
which phpize, which php-config查看
PHP Redis拓展包:wget https://github.com/phpredis/phpredis/archive/develop.zip
unzip develop.zip
cd phpredis-develop/
phpize
ll configure
./configure --with-php-config=/www/wdlinux/apache_php-5.4.26/bin/php-config
make
make install(拓展包已经放到指定的目录)
修改php.ini(php --ini可查看位置)
添加extension=redis.so

重启php-fpm

PHP测试实例:

connect('localhost',6379);
var_dump($result);// true

// string类型(存储字符串或整形)
$redis->delete("string1");

$redis->set("string1", "val1");
$val = $redis->get("string1");
var_dump($val); // val1

$redis->set("string1", 4);
$redis->incr("string1", 2);
$val = $redis->get("string1");
var_dump($val); // 6

// list类型(存储序列集合,可用于队列)
$redis->delete("list1");

$redis->lPush("list1", "A");
$redis->lPush("list1", "B");
$redis->lPush("list1", "C");

$val = $redis->rPop("list1");
var_dump($val); // A

//set类型(存储无序集合,但每个元素都不一样)
$redis->delete("set1");

$redis->sAdd("set1", "A");
$redis->sAdd("set1", "B");
$redis->sAdd("set1", "C");
$redis->sAdd("set1", "C");

$val = $redis->sCard("set1");
var_dump($val);// 3

$val = $redis->sMembers("set1");
var_dump($val);// array()

//hash 类型(存储复杂的key-val结构)
$redis->delete("driver1");

$redis->hSet("driver1", "name", "hxf");
$redis->hSet("driver1", "age", 30);
$redis->hSet("driver1", "gender", 1);

$val = $redis->hGet("driver1", "name");
var_dump($val);

$val = $redis->hMGet("driver1", array("name", "age"));
var_dump($val);

//sort set类型(存储带分数的有序集合,常用于排行榜)
$redis->delete("zet1");

$redis->zAdd("zset1", 100, "xiaoming");
$redis->zAdd("zset1", 90, "xiaohong");
$redis->zAdd("zset1", 93, "xiaowang");
$redis->zAdd("zset1", 85, "xiaoping");

$val = $redis->zRange("zset1", 0, -1);// 从低到高(全部)
var_dump($val);

$val = $redis->zRevRange("zset1", 0, 2);// 从高到低(前三名)
var_dump($val);

//https://github.com/phpredis/phpredis

你可能感兴趣的:(运维,后端)