Redis官网
https://redis.io/
redis默认端口
6379
安装Redis
[root@tom01 tools]# tar xf redis-3.0.3.tar.gz
[root@tom01 redis-3.0.3]# cd redis-3.0.3
[root@tom01 redis-3.0.3]# make PREFIX=/usr/local/redis install
启动文件
[root@tom01 redis-3.0.3]# cp /tools/redis-3.0.3/utils/redis_init_script /etc/init.d/redis
[root@tom01 redis-3.0.3]# chmod 755 /etc/init.d/redis
修改启动文件
vim /etc/init.d/redis
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
$CLIEXEC -h 192.168.56.5 -p $REDISPORT -a redistest shutdown
#如果Redis设置了密码 这里要 -a 密码
配置文件
mkdir /etc/redis
cp /tools/redis-3.0.3/redis.conf /etc/redis/6379.conf
[root@tom01 redis-3.0.3]# vim /etc/redis/6379.conf
37 daemonize yes #后台运行
41 pidfile /var/run/redis_6379.pid
54 tcp-backlog 32678 #tcp完成三四握手的队列
65 bind 192.168.56.7 #绑定ip 绑定后就不会监控IPV6
104 logfile “/var/log/redis_6379.log”
119 databases 16 #最多允许有多少库
188 dir /var/lib/redis_6379 #持久化数据存放位置
392 requirepass redistest #设置密码
450 maxmemory 1g #设置最大使用内存大小
473 maxmemory-policy allkeys-lru #设置内存淘汰机制
1k => 1000 bytes
1kb => 1024 bytes
1m => 1000000 bytes
1mb => 10241024 bytes
1g => 1000000000 bytes
1gb => 10241024*1024 bytes
更改系统配置 tcp-backlog
TCP连接中已完成队列(完成三次握手之后)的长度
echo “net.core.somaxconn = 32768” >> /etc/sysctl.conf
/sbin/sysctl -p
创建存储路径
[root@tom01 ~]# mkdir /var/lib/redis_6379
启动Redis并查看
[root@tom01 redis-3.0.3]# /etc/init.d/redis start
[root@tom01 redis-3.0.3]# netstat -lnt|grep 6379
tcp 0 0 192.168.56.7:6379 0.0.0.0:* LISTEN
查看日志
less /var/log/redis_6379.log
有两个警告 可以修改
1 #WARNING 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.
这是内存分配策略
可选值:0、1、2。
0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2, 表示内核允许分配超过所有物理内存和交换空间总和的内存
解决方法:
很简单,按提示的操作(将vm.overcommit_memory 设为1)即可:
有三种方式修改内核参数,但要有root权限:
(1)echo “vm.overcommit_memory=1” >>/etc/sysctl.conf 然后sysctl -p 使配置文件生效 永久生效
(2)sysctl vm.overcommit_memory=1 剩下这两种方法 重启失效 可以加入到rc.local中
(3)echo 1 > /proc/sys/vm/overcommit_memory
2 #WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue 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 the setting after a reboot. Redis must be restarted after THP is disabled.
关闭大页内存
echo ‘echo never > /sys/kernel/mm/transparent_hugepage/enabled’ >> /etc/rc.d/rc.local
配置环境变量
echo “export PATH=/usr/local/redis/bin:$PATH” >> /etc/profile
. /etc/profile
连接Redis
[root@tom01 ~]# redis-cli -h 192.168.56.7 -p 6379 -a redistest