一、redis简介

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

Redis的所有数据都是保存在内存中然后不定期的通过异步方式保存到磁盘上(这称为“半持久化模式”)也可以把每一次数据变化都写入到一个append only file(aof)里面(这称为“全持久化模式”)


二、安装部署

mkdir/softcd/soft

安装插件

wgethttp://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gztarzxvftcl8.6.1-src.tar.gzcdtcl8.6.1./configuremake&&makeinstall

安装redis

wgethttp://redis.googlecode.com/files/redis-2.6.14.tar.gztarzxvfredis-2.6.14.tar.gzcdredis-2.6.14maketestmake--prefix=/storage/redisdbmakeintsall


三、安装完成后在src目录下生成5个可执行文件redis-server、redis-cli、redis-benchmark、redis-check-aof、redis-check-dump它们的作用如下redis-serverRedis服务器的daemon启动程序redis-cliRedis命令行操作工具。当然你也可以用telnet根据其纯文本协议来操作redis-benchmarkRedis性能测试工具测试Redis在你的系统及你的配置下的读写性能redis-check-aof更新日志检查

redis-check-dump用于本地数据库检查

redis启动

cdsrc./redis-server[5056]04Apr03:33:50.744#WARNINGovercommit_memoryissetto0!Backgroundsavemayfailunderlowmemorycondition.Tofixthisissueadd'vm.overcommit_memory=1'to/etc/sysctl.confandthenrebootorrunthecommand'sysctlvm.overcommit_memory=1'forthistotakeeffect.[5056]04Apr03:33:50.744*Theserverisnowreadytoacceptconnectionsonport6379


四、设置内存分配策略

可选值0、1、2。0 表示内核将检查是否有足够的可用内存供应用进程使用如果有足够的可用内存内存申请允许否则内存申请失败并把错误返回给应用进程。1 表示内核允许分配所有的物理内存而不管当前的内存状态如何。2 表示内核允许分配超过所有物理内存和交换空间总和的内存值得注意的一点是redis在dump数据的时候会fork出一个子进程理论上child进程所占用的内存和parent是一样的比如parent占用的内存为8G这个时候也要同样分配8G的内存给child,如果内存无法负担往往会造成redis服务器的down机或者IO负载过高效率下降。所以这里比较优化的内存分配策略应该设置为 1表示内核允许分配所有的物理内存而不管当前的内存状态如何

echo"vm.overcommit_memory=1">>/etc/sysctl.confsysctl-p

验证

src/redis-cliredis>setfoobarOKredis>getfoobarshutdown

Redis远程连接

telnet 127.0.0.1 6379

安全设置

关闭掉selinux


/usr/sbin/setenforce0立刻关闭SELINUX#/sbin/iptables-IINPUT-ptcp--dport6379-jACCEPT#/etc/rc.d/init.d/iptablessave

五、配置自启动

cp redis.conf /etc

mkdir -p /storage/redis db文件放在这里要修改redis.conf

mkdir -p /var/log/redislog log文件放在这里要修改redis.conf

修改redis.conf db文件位置

cpredis.conf/etc/vim/etc/redis#Theworkingdirectory.#TheDBwillbewritteninsidethisdirectory,withthefilenamespecified#aboveusingthe'dbfilename'configurationdirective.#AlsotheAppendOnlyFilewillbecreatedinsidethisdirectory#daemonizeyes//改为后台启动#Notethatyoumustspecifyadirectoryhere,notafilename.dir/storage/redis//db文件位置logfile=/var/log/redislog//修改redis.conflog文件位置

配置自启动:vi /etc/init.d/redis

startup脚本代码如下所示将其建立为/etc/init.d/redis文件

#!/bin/bash##Initfileforredis##chkconfig:-8012#description:redisdaemon##processname:redis#config:/etc/redis.conf#pidfile:/var/run/redis.pidsource/etc/init.d/functions#BIN="/usr/local/bin"BIN="/usr/local/bin"CONFIG="/etc/redis.conf"PIDFILE="/var/run/redis.pid"###Readconfiguration[-r"$SYSCONFIG"]&&source"$SYSCONFIG"RETVAL=0prog="redis-server"desc="RedisServer"start(){if[-e$PIDFILE];thenecho"$descalreadyrunning...."exit1fiecho-n$"Starting$desc:"

daemon$BIN/$prog$CONFIGRETVAL=$?echo[$RETVAL-eq0]&&touch/var/lock/subsys/$progreturn$RETVAL}stop(){echo-n$"Stop$desc:"killproc$progRETVAL=$?echo[$RETVAL-eq0]&&rm-f/var/lock/subsys/$prog$PIDFILEreturn$RETVAL}restart(){stopstart}case"$1"instart)start;;stop)stop;;restart)restart;;condrestart)[-e/var/lock/subsys/$prog]&&restart

RETVAL=$?;;status)status$progRETVAL=$?;;*)echo$"Usage:$0{start|stop|restart|condrestart|status}"RETVAL=1esacexit$RETVALchmod+x/etc/init.d/redis

重新启动

/etc/init.d/redisrestartStopRedisServer:[OK]StartingRedisServer:[OK]

六、配置php扩展支持redis


1、下载php-redis zip安装包

https://github.com/nicolasff/phpredis


2、找到PHP安装路径

命令whereis phpize和whereis php-config 找到phpize和php-config路径


3、生成configure

#/usr/bin/phpize


4、编译安装

#./configure--with-php-config=/usr/bin/php-config

#make&&makeinstall


5、加入安装的redis.so模块

#vim/etc/php.iniextension=redis.so


6、重启apache或nginx

7、测试

<?php$redis=newRedis();$redis->connect('127.0.0.1',6379);$redis->set('test','helloworld!');echo$redis->get('test');?>