Redis安装、配置及拓展使用总结
来源:互联网 发布:萝卜花园软件 编辑:程序博客网 时间:2024/04/27 13:33
Redis安装、配置及拓展使用总结:
Redis是一种开源的(BSD许可),高性能的内存数据结构存储系统,由C语言所编写,常被用作为数据库、缓存以及消息中间件,支持数据持久化,可将内存中数据保存到磁盘中,支持数据备份、原子性操作、发布/预定及管道技术,另外,它提供了多种存储数据类型,读取速度最高为110000次/s,写入速度最高为81000次/s。
· 安装
· 使用
· 拓展
一、安装
这里介绍的是在Mac OS下Redis的安装过程,如下:
1、官网下载最新的压缩包
http://redis.io
2、拷贝并解压到/redis目录
$ sudo tar –zxf redis-3.0.7.tar
$ cd redis-3.0.7
3、编译测试
$ sudo make test
看到上面的结果,代表编译没发现任何问题,可以继续安装了。
4、编译安装
$ sudo make install
安装成功了,下面进行基本配置并启动服务。
5、基本配置
a、在/redis/下新建etc、bin、db三个目录
b、拷贝src目录下的mkreleasehdr.sh,redis-benchmark,redis-check-dump, redis-cli,redis-server到bin目录下
c、拷贝/redis/redis-3.0.7下的redis.conf一份到/redis/etc/中并修改
$ vim redis.conf
修改的内容如下:
# 修改redis为守护模式(默认为no)
daemonize yes
# 在上面的模式开启后,.pid文件存放位置
pidfile /redis/redis.pid
# redis监听端口(保持默认即可)
port 6379
# 设定客户端超时时间(默认为0)
timeout 200
# 设定日志级别模式(默认为notice 这里设debug为向日志写入更多信息搜集)
loglevel debug
# 日志的位置
logfile /redis/log-redis.log
# 设置链接时支持数据库数(默认即可)
databases 16
# 设置存储数据库时数据是否压缩 yes:使用lzf算法压缩,但会增加cpu开销
# no:不压缩,但是会增加数据库的存储空间,一般设置为yes即可
rdbcompression yes
# 数据库文件写入的位置
dir /redis/db/
#指定是否在每次更新操作后进行日志记录,Redis在默认情况下是异步的把数据写入磁盘,如果不开启,可能
#会在断电时导致一段时间内的数据丢失。因为 redis本身同步数据文件是按上面save条件来同步的,所以有
#的数据会在一段时间内只存在于内存中
appendonly no
#指定更新日志条件,共有3个可选值:
#no:表示等操作系统进行数据缓存同步到磁盘(快)
#always:表示每次更新操作后手动调用fsync()将数据写到磁盘(慢,安全)
#everysec:表示每秒同步一次(折衷,默认值)
appendfsync everysec
6、启动服务
$ ./bin/redis-serveretc/redis.conf
可以使用下面命令查看日志:
$ tail -flog-redis.log
7、测试使用
$ ./bin/redis-cli
正如上图所示,测试环境已经安装并配置好了。
二、使用
多种数据类型操作:
1、PING
$ ping
PONG
验证redis服务是否正常,返回PONG代表正常。
2、键操作
127.0.0.1:6379> set mykey redis //存入值 键:mykey 值:redis
OK
127.0.0.1:6379> get mykey // 获得值
"redis"
127.0.0.1:6379> exists mykey // 判断键mykey是否存在 1代表存在
(integer) 1
127.0.0.1:6379> type mykey // 获得存入数据的类型
string
127.0.0.1:6379> set mykey2 redis2
OK
127.0.0.1:6379> expire mykey2 10 // 指定键mykey2的值 有效期为10秒
(integer) 1
127.0.0.1:6379> get mykey2
"redis2"
127.0.0.1:6379> get mykey2 // 键mykey2 10秒后为nil
(nil)
127.0.0.1:6379> set mykey3 redis3
OK
127.0.0.1:6379> expire mykey3 100
(integer) 1
127.0.0.1:6379> ttl mykey3 // 实时获得键mykey3的有效剩余时间
(integer) 92
127.0.0.1:6379> ttl mykey3
(integer) 89
127.0.0.1:6379> ttl mykey3
(integer) 86
127.0.0.1:6379> ttl mykey3
(integer) 74
127.0.0.1:6379> del mykey3 // 删除了键mykey3的值
(integer) 1
127.0.0.1:6379> ttl mykey3
(integer) -2
127.0.0.1:6379>
3、字符串操作
127.0.0.1:6379> set mykey "Hello" // 存入数据 键:mykey 值:”Hello”
OK
127.0.0.1:6379> get mykey // 获取数据 键:mykey
"Hello"
127.0.0.1:6379> append mykey " Redis" // 为键mykey追加字符串
(integer) 11
127.0.0.1:6379> get mykey
"Hello Redis"
127.0.0.1:6379> strlen mykey // 计算键mykey存放字符串的长度
(integer) 11
127.0.0.1:6379> getrange mykey 0 5 // 获取键mykey字符位置0到5范围的内容字符
"Hello "
127.0.0.1:6379> mset mykey "Redis" mykey2 " is now" // 设置多对key-value
OK
127.0.0.1:6379> mget mykey mykey2 // 获取多个键值
1) "Redis"
2) " is now"
127.0.0.1:6379> setrange mykey2 5 " command" // 覆盖键mykey 指定位置后的内容,覆盖内容” command”
(integer) 13
127.0.0.1:6379> get mykey2
" is n command"
127.0.0.1:6379> set mykey 99 // 存放整形字符
OK
127.0.0.1:6379> incr mykey // 递增键mykey数字字符
(integer) 100
127.0.0.1:6379> get mykey
"100"
127.0.0.1:6379> decr mykey // 递减键mykey数字字符
(integer) 99
127.0.0.1:6379> get mykey
"99"
127.0.0.1:6379>
4、哈稀操作
Hash是一个字符串类型的field和value的映射表,比较适用于存储对象。
127.0.0.1:6379> hmset user username "cwteam" nickname"Heinie" descri "this is a test for hashs using in rediscache!" // 设置对象名字user,属性username、nickname及descri,并给初始值
OK
127.0.0.1:6379> hgetall user // 获取对象user哈希键所有的field和value
1) "username"
2) "cwteam"
3) "nickname"
4) "Heinie"
5) "descri"
6) "this is a test for hashs using in redis cache!"
127.0.0.1:6379> hlen user // 返回对象user的field个数
(integer) 3
127.0.0.1:6379> hexists user username // 判断对象中是否有username属性,1代表含有
(integer) 1
127.0.0.1:6379> hkeys user // 查看对象所有的属性名
1) "username"
2) "nickname"
3) "descri"
127.0.0.1:6379> hmget user username descri // 获取对象user的属性username和descri的值
1) "cwteam"
2) "this is a test for hashs using in redis cache!"
127.0.0.1:6379> hset user nickname "cwteam" // 修改对象user中属性nickname的值
(integer) 0
127.0.0.1:6379> hgetall user
1) "username"
2) "cwteam"
3) "nickname"
4) "cwteam"
5) "descri"
6) "this is a test for hashs using in redis cache!"
127.0.0.1:6379> hvals user // 查看对象user中所有值
1) "cwteam"
2) "cwteam"
3) "this is a test for hashs using in redis cache!"
127.0.0.1:6379> hdel user descri nickname // 删除对象user中指定的descri和nickname属性
(integer) 2
127.0.0.1:6379> hgetall user
1) "username"
2) "cwteam"
127.0.0.1:6379> hkeys user // 查看对象user所有的fields
1) "username"
127.0.0.1:6379> hsetnx user age 30 // 如果user中不存在age属性,那么就给user新增属性age并存储值
(integer) 1
127.0.0.1:6379> hkeys user
1) "username"
2) "age"
127.0.0.1:6379> hincrby user age 1 // 为属性age递增1
(integer) 31
127.0.0.1:6379> hget user age
"31"
127.0.0.1:6379> hsetnx user money 1000000.999
(integer) 1
127.0.0.1:6379> hkeys user
1) "username"
2) "age"
3) "money"
127.0.0.1:6379> hincrbyfloat user money 1 // 为属性(float) 递增1
"1000001.99900000000002365"
127.0.0.1:6379> hget user money
"1000001.99900000000002365"
127.0.0.1:6379>
5、列表操作
列表(List)是一种简单的字符串列表,可以添加一个元素到表头会表尾。
127.0.0.1:6379> lpush cache redis // 向列表存储入redis(默认表头)
(integer) 1
127.0.0.1:6379> lpush cache memcached
(integer) 2
127.0.0.1:6379> lpush cache xcache
(integer) 3
127.0.0.1:6379> lpush cache db
(integer) 4
127.0.0.1:6379> lrange cache 0 10 // 获取键cache从0到10索引的值
1) "db"
2) "xcache"
3) "memcached"
4) "redis"
127.0.0.1:6379> lindex cache 3 // 修改键列表索引3位置的内容
"redis"
127.0.0.1:6379> llen cache // 返回列表的大小
(integer) 4
127.0.0.1:6379> linsert cache before redis file // 在redis前插入file
(integer) 5
127.0.0.1:6379> linsert cache after redis apc // 在redis后插入apc
(integer) 6
127.0.0.1:6379> lrange cache 0 10
1) "db"
2) "xcache"
3) "memcached"
4) "file"
5) "redis"
6) "apc"
127.0.0.1:6379> lset cache 5 rpc // 修改索引5位置的内容
OK
127.0.0.1:6379> lrange cache 0 10
1) "db"
2) "xcache"
3) "memcached"
4) "file"
5) "redis"
6) "rpc"
127.0.0.1:6379> lset cache 5 "rpc is not caching skill"
OK
127.0.0.1:6379> lrange cache 0 10
1) "db"
2) "xcache"
3) "memcached"
4) "file"
5) "redis"
6) "rpc is not caching skill"
127.0.0.1:6379> rpop cache // 移除列表尾部一条数据并返回该位置值
"rpc is not caching skill"
127.0.0.1:6379> lpop cache // 移除列表头部一条数据并返回该位置值
"db"
127.0.0.1:6379> lrange cache 0 6
1) "xcache"
2) "memcached"
3) "file"
4) "redis"
127.0.0.1:6379>
6、集合操作
集合(Set)是字符串类型无须集合,里面的元素唯一,不能出现重复数据。集合是用哈希技术实现,所以它的速度也是很快的。
127.0.0.1:6379> sadd cache redis // 向集合添加redis
(integer) 1
127.0.0.1:6379> sadd cache memcached xcache db // 向集合添加多个值
(integer) 3
127.0.0.1:6379> smembers cache //返回集合所有成员
1) "memcached"
2) "db"
3) "xcache"
4) "redis"
127.0.0.1:6379> scard cache // 返回集合的大小
(integer) 4
127.0.0.1:6379> sadd cache2 file redis session
(integer) 3
127.0.0.1:6379> sdiff cache cache2 // 返回集合cache与cache2的差集
1) "memcached"
2) "xcache"
3) "db"
127.0.0.1:6379> sinter cache cache2 // 返回集合cache与cache2的交集
1) "redis"
127.0.0.1:6379> sismember cache redis // 判断集合是否含有redis成员
(integer) 1
127.0.0.1:6379> srem cache2 session // 移除集合一个或多个成员
(integer) 1
127.0.0.1:6379> smembers cache2
1) "file"
2) "redis"
127.0.0.1:6379> sunion cache cache2 // 返回多集合的并集
1) "xcache"
2) "db"
3) "redis"
4) "memcached"
5) "file"
127.0.0.1:6379>
7、有序集合操作
有序集合和集合一样同样是字符串类型元素的集合,并且不允许重复的成员。
不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。另外,有序集合的成员是唯一的,但分数(score)却可以重复的。
127.0.0.1:6379> zadd cache 1 redis // 向有序集合添加一个值 并指定分数
(integer) 1
127.0.0.1:6379> zadd cache 2 memcached 3 xcache 4 db // 向有序集合添加多个值 并指定分数
(integer) 3
127.0.0.1:6379> zrange cache 0 10 withscores // 返回指定索引位置区内的成员,并绑定分数
1) "redis"
2) "1"
3) "memcached"
4) "2"
5) "xcache"
6) "3"
7) "db"
8) "4"
127.0.0.1:6379> zcard cache // 返回集合成员数
(integer) 4
127.0.0.1:6379> zadd cache 4 file
(integer) 1
127.0.0.1:6379> zrange cache 0 10 withscores
1) "redis"
2) "1"
3) "memcached"
4) "2"
5) "xcache"
6) "3"
7) "db"
8) "4"
9) "file"
10) "4"
127.0.0.1:6379> zcount cache 4 4 // 返回指定区间的成员数
(integer) 2
127.0.0.1:6379> zrank cache file // 返回指定成员的索引
(integer) 4
注意:
上面的所有数据类型命令并未都列出使用,具体可查看官网,因为命令很多也不需要每一个都记住,使用时去查看使用即可。
三、拓展PHP支持
在php中使用redis前,我需要做几件事儿,分别是安装配置redis服务环境、安装配置phpredis拓展驱动及测试验证,具体如下所示:
1、安装配置redis服务环境
该步骤在本篇文章第一部分,请可参考使用。
2、安装配置phpredis拓展驱动
a、下载
下载地址:https://github.com/nicolasff/phpredis
b、安装
$ cd /redis/php/phpredis
$ sudo phpize
$ sudo ./configure
$ sudo make
$ sudo make install
默认安装路径:
/usr/local/Cellar/php56/5.6.18/lib/php/extensions/no-debug-non-zts-20131226/
c、配置
修改php.ini文件,内容如下:
extension=/redis/redis.so
当然,我的redis.so文件存放在/redis/下了,所以上面的配置。配置完成后,需要重新启动服务环境,例如:apache、nginx等。
我们可以使用phpinfo(),测试php拓展redis加载是否成功:
d、测试
php(ThinkPHPv3.2)代码:
<?php
// 链接本地的Redis服务
$redis= new \Redis();
$redis->connect("127.0.0.1",6379);
echo "Redis Server Connection isOk!</br>";
echo "Server is running:".$redis->ping(); //查看服务是否运行
?>
结果返回:
说明,Redis服务可以使用,一切正常。
e、实例
例子是以存储一个字符串类型数据到redis中,并显示返回,具体如下:
<?php
// 链接本地的Redis服务
$redis= new \Redis();
$redis->connect("127.0.0.1",6379);
echo "Redis Server Connection isOk!</br>";
// 设置redis字符串数据
$redis->set('redis-string-key','this is a string key-value for testingredis use.');
echo 'Stored string in redis:'.$redis->get('redis-string-key');
?>
浏览器结果:
当然,你也可以直接登录redis-cli客户端查看:
技术讨论群:
489451956(新)
- Redis安装、配置及拓展使用总结
- redis安装及配置/给php安装redis拓展
- PHP安装、配置及拓展总结
- Windows下安装Redis及php的redis拓展教程
- Linux(Centos5.5) redis 安装及redisphp拓展安装应用
- Linux(Centos5.5) redis 安装及redisphp拓展安装应用
- Linux(CentOS 5.5) Redis 安装及RedisPHP拓展安装应用
- Linux(CentOS 5.5) Redis 安装及RedisPHP拓展安装应用
- redis安装配置总结
- Redis 安装及配置
- Redis 安装及配置
- Redis 安装及配置
- redis安装及配置
- redis安装及配置
- redis安装及配置
- laravel安装redis拓展
- linux安装redis拓展
- wamp安装redis拓展
- UCenter 同步改密相关操作!
- Unable to start netserver with 'IN(6)ADDR_ANY' port '12865' when run netperf
- 贵州的山贵州的水
- Request获取用户真实IP
- java xml解析工具
- Redis安装、配置及拓展使用总结
- srs之console调试
- ubuntu10.10上移植opencv 2.3.1
- eclipse控制台输出重定向到文件
- 利用Opencv保存图片,并生成JPG格式,调整压缩质量。
- http通信,Android Gzip压缩解压
- 优化hibernate性能的几点建议
- HDU 1227 Fast Food(经典DP)
- CentOS针对磁盘IO[jdb2进程]的优化