一、安装1.检查gcc环境执行命令,如果Linux系统没有安装gcc编译器,会提示“Command not found”# gcc -v安装gcc# yum -y install gcc以上是make需要的,不装会报错!2.下载Redis# cd /usr/local# wget http://download.redis.io/releases/redis-4.0.12.tar.gz3.解压# tar xzf redis-4.0.12.tar.gz4.make编译# cd redis-4.0.12# make5.make install初始化# make install6.安装Redis运行make test测试# cd src #进入src目录# make test #执行测试# make install安装成功7.启动# ./src/redis-server该启动方式在控制台退出的时候就关闭了,不建议使用。
建议修改好配置的daemonize为yes后,使用命令redis-server redis.conf应用改配 置文件启动redis
二、配置修改redis.conf配置文件# vi redis.conf1.设置都可访问#bind 127.0.0.1
如果设置为127.0.0.1,则表示仅本机可访问,注销这句表示全部可以访问,这里我们注释掉。想配置内网访问,或者限定IP,一般使用云服务器安全组策略来配置实现。
2.解除外网访问的限制protected-mode no
protected-mode参数是为了禁止外网访问redis,如果启用了,则只能够通过lookback ip(127.0.0.1)访问Redis
3.以后端模式启动daemonize yes4.配置log日志路径
logfile “/usr/local/redis/redis-4.0.12/logs/redis.log” # 建议先创建好redis.log文件,再来配置
5.禁用持久化(如果仅仅需要缓存,禁掉持久化配置)#save 900 1#save 300 10#save 60 10000全部注释6.配置最大内存maxmemory 2147483648 # 后面跟的是Bytes,这里表示2G7.内存淘汰策略maxmemory-policy volatile-lru
redis提供了下面几种淘汰策略供用户选择,其中默认的策略为noeviction策略:
noeviction:当内存使用达到阈值的时候,所有引起申请内存的命令会报错。
allkeys-lru:在主键空间中,优先移除最近未使用的key。
volatile-lru:在设置了过期时间的键空间中,优先移除最近未使用的key。
allkeys-random:在主键空间中,随机移除某个key。
volatile-random:在设置了过期时间的键空间中,随机移除某个key。
volatile-ttl:在设置了过期时间的键空间中,具有更早过期时间的key优先移除。
8.设置密码
requirepass foobared
三、应用配置启动redis1.启动redisredis-server redis.confps -ef|grep redis # 查看redis进程2.关闭rediskill – 9 pid # pid通过查看而来3.远程连接redisredis-cli -h ip地址 -p 端口号 -a 密码四、解决启动后日志提醒的三个警告
第一个警告:The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
第二个警告:overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1’ to/etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1’ for this to take effect.
第三个警告:you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix thisissue run the command ‘echo never > /sys/kernel/mm/transparent_hugepage/enabled’ as root, and add it to your /etc/rc.local in order to retain thesetting after a reboot. Redis must be restarted after THP is disabled.
解决方案:
第一个警告:
将net.core.somaxconn = 1024添加到/etc/sysctl.conf中,然后执行sysctl -p 生效配置。
第二个警告:
将vm.overcommit_memory = 1添加到/etc/sysctl.conf中,然后执行sysctl -p生效配置。
第三个警告:
将echo never > /sys/kernel/mm/transparent_hugepage/enabled添加到/etc/rc.local中,然后执行source /etc/rc.local生效配置。