Redis 安装及配置 (转整理)

Redis 安装及配置 (转整理)

 

Redis是一种高级key-value数据库。它跟memcached相似,不过数据能够持久化,并且支持的数据类型很丰富。有字符串,链表,集 合和有序集合。支持在服务器端计算集合的并,交和补集(difference)等,还支持多种排序功能。因此Redis也能够被当作是一个数据结构服务 器。

Redis的全部数据都是保存在内存中,而后不按期的经过异步方式保存到磁盘上(这称为“半持久化模式”);也能够把每一次数据变化都写入到一个append only file(aof)里面(这称为“全持久化模式”)。

I.快速运行Redis

1、下载安装
进入redis.io官方网站:
Linux代码javascript

 收藏代码

  1. $ wget http://redis.googlecode.com/files/redis-2.4.15.tar.gz  
  2. $ tar xzf redis-2.4.5.tar.gz //这里假设解压缩到/usr/local/redis  
  3. $ cd redis-2.4.5  
  4. $ make  
  5. $ make install  
  6. $ cd utils  
  7. $./install_server  



就会自动安装到/usr/local/bin目录下。在该目录下生成几个可执行文件,分别是redis-server、redis-cli、redis-benchmark、redis-stat、redis-check-aof,它们的做用以下:
    redis-server:Redis服务器的daemon启动程序
    redis-cli:Redis命令行操做工具。固然,你也能够用telnet根据其纯文本协议来操做
    redis-benchmark:Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能
    redis-stat:Redis状态检测工具,能够检测Redis当前状态参数及延迟情况
    redis-check-aof:

二.启动服务器
安装时的最后一步install_server脚本会生成启动命令文件(试试就知道),下面就是一个执行例子
Linux代码php

 收藏代码

  1. Welcome to the redis service installer  
  2. This script will help you easily set up a running redis server  
  3.   
  4.   
  5. Please select the redis port for this instance: [6379]  
  6. Selecting default: 6379  
  7. Please select the redis config file name [/etc/redis/6379.conf]  
  8. Selected default - /etc/redis/6379.conf  
  9. Please select the redis log file name [/var/log/redis_6379.log]  
  10. Selected default - /var/log/redis_6379.log  
  11. Please select the data directory for this instance [/var/lib/redis/6379]  
  12. Selected default - /var/lib/redis/6379  
  13. Please select the redis executable path [/usr/local/bin/redis-server]  
  14. Copied /tmp/6379.conf => /etc/init.d/redis_6379  
  15. Installing service...  
  16. Successfully added to chkconfig!  
  17. Successfully added to runlevels 345!  
  18. Starting Redis server...  
  19. Installation successful!  


/etc/init.d/redis_6379 start
将启动服务到默认端口6379

三.客户端访问
Linux代码java

 收藏代码

  1. $ redis-cli  
  2. redis> set foo bar  
  3. OK  
  4. redis> get foo  
  5. "bar"  



四.关闭服务器
Linux代码git

 收藏代码

  1. $ /etc/init.d/redis_6379 stop  


以上摘官方文档http://redis.io/download。若是想使用Windows版的Redis请去http://code.google.com/p/servicestack/wiki/RedisWindowsDownload下载(其版本滞后于官方版本,不建议在生产环境使用Win32.)。

II. 定制服务器启动参数
在咱们成功安装Redis后,咱们直接执行redis-server便可运行Redis,此时它是按照默认配置来运行的(默认配置甚至不是后台运行)。咱们但愿Redis按咱们的要求运行,则咱们须要修改配置文件(在redis解压缩目录下有一个redis.con能够做为范本),下面是redis.conf的主要配置参数的意义:github

引用redis

    daemonize:是否之后台daemon方式运行
pidfile:pid文件位置
port:监听的端口号
timeout:请求超时时间
loglevel:log信息级别
logfile:log文件位置
databases:开启数据库的数量
save * *:保存快照的频率,第一个*表示多长时间,第三个*表示执行多少次写操做。在必定时间内执行必定数量的写操做时,自动保存快照。可设置多个条件。
rdbcompression:是否使用压缩
dbfilename:数据快照文件名(只是文件名,不包括目录)
dir:数据快照的保存目录(这个是目录)
appendonly:是否开启appendonlylog,开启的话每次写操做会记一条log,这会提升数据抗风险能力,但影响效率。
appendfsync:appendonlylog如何同步到磁盘(三个选项,分别是每次写都强制调用fsync、每秒启用一次fsync、不调用fsync等待系统本身同步)数据库



下面是一个略作修改后的配置文件内容:
Redis.conf代码windows

 收藏代码

  1. daemonize yes  
  2. pidfile /usr/local/redis/var/redis.pid  
  3. port 6000  
  4. timeout 300  
  5. loglevel debug  
  6. logfile /usr/local/redis/var/redis.log  
  7. databases 16  
  8. save 900 1  
  9. save 300 10  
  10. save 60 10000  
  11. rdbcompression yes  
  12. dbfilename dump.rdb  
  13. dir /usr/local/redis/var/  
  14. appendonly no  
  15. appendfsync always  
  16. glueoutputbuf yes  
  17. shareobjects no  
  18. shareobjectspoolsize 1024  


重启服务器
Linux代码服务器

 收藏代码

  1. redis-server /usr/local/redis/redis.conf  


试试看读写是否有问题.若是服务器启动到了指定非默认端口,那么客户端链接则须要-p参数
如:
Linux代码数据结构

 收藏代码

  1. $redis-cli -p 6380  



* 开放服务器端口供其余主机链接
vi /etc/sysconfig/iptables #须要具有其修改权限
可能须要增长一行:
# redis
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
保存后重启iptables:
service iptables restart (PATH中加入了/sbin/)或者 /etc/init.d/iptables restart

III. PHP客户端
Redis的php客户端库很是之多,code.google.com上就有:
* phpredis
http://code.google.com/p/phpredis/ 是C客户端做为php模块

* php-predis
http://code.google.com/p/php-redis/ 纯php客户端
以上两个都不在Redis官方推荐推荐之列。

Redis推荐客户端连接是:http://redis.io/clients,以下图:


纯php库Predis(便于hack),可是性能不高。https://github.com/nrk/predis
下载该库文件后运行bin/createSingleFile.php能够生成一个类库文件Predis.php,很是方便使用。下面是最简单的一个Hello World应用:
Php代码

 收藏代码

  1. <?php  
  2. require('Predis.php');  
  3. $single_server = array(  
  4.     'host'     => '192.168.1.101',  
  5.     'port'     => 6379,  
  6.     'database' => 15  
  7. );  
  8.   
  9. $client = new Predis\Client($single_server);  
  10.   
  11. $client->set('library', 'predis');  
  12. $retval = $client->get('allen ');  
  13.   
  14. var_dump($retval);  



我推荐使用的客户端是:phpredis  https://github.com/nicolasff/phpredis
windows版的dll从这里下载https://github.com/char101/phpredis/downloads

IV . Redis管理工具
1. phpRedisAdmin(推荐)
https://github.com/ErikDubbelboer/phpRedisAdmin/
基于nicolasff/phpredis扩展


2. redis-admin
http://code.google.com/p/redis-admin/

相关文章
相关标签/搜索