Redis安装-单机

avatar
作者
猴君
阅读量:0

Redis基础

Redis,全称(Remote Dictionary Server),遵循BSD协议。

官方网址

https://redis.io/ 

Redis典型应用场景

Session 共享:常见于 web 集群中的 Tomcat 或者 PHP 中多 web 服务器 session 共享 消息队列:ELK 的日志缓存、部分业务的订阅发布系统 计数器:访问排行榜、商品浏览数等和次数相关的数值统计场景 缓存:数据查询、电商网站商品信息、新闻内容 微博/微信社交场合:共同好友、点赞评论等 

Redis安装

yum安装略,需要epel源

编译安装

下载最新源码包

http://download.redis.io/releases/ 

解压,并编译安装

cd /usr/local/src  tar xvf  redis-4.0.14.tar.gz  cd redis-4.0.14   make PREFIX=/usr/local/redis install 

编译出错的解决

缺少gcc库

yum install  vim iotop bc gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl  openssl-devel zip unzip zlib-devel  net-tools lrzsz tree ntpdate telnet lsof tcpdump wget libevent libevent-devel bc  systemd-devel bash-completion traceroute -y  

fatal error: jemalloc/jemalloc.h: No such file or directory

make MALLOC=libc  PREFIX=/usr/local/redis install 

配置文件

[root@node17 redis]# pwd /usr/local/redis  # mkdir /usr/local/redis/etc # mkdir -pv /usr/local/redis/{etc,logs}  # cp /usr/local/src/redis-4.0.14/redis.conf ./etc/ # sed -i 's@logfile ""@logfile "/usr/local/redis/logs/redis.log"@' ./etc/redis.conf   测试前台启动 # ./bin/redis-server ./etc/redis.conf  

解决警告提示

WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

backlog 参数控制的是三次握手的时候 server 端收到 client ack 确认号之后的队列值。 [root@node17 ~]# vim /etc/sysctl.conf  net.core.somaxconn = 512 [root@node17 ~]# sysctl -p net.core.somaxconn = 512 

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.

[root@node17 ~]# vim /etc/sysctl.conf  [root@node17 ~]# sysctl -p net.core.somaxconn = 512 vm.overcommit_memory = 1 

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 never > /sys/kernel/mm/transparent_hugepage/enabled  每次开机重启都执行 echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local   chmod +x /etc/rc.local  

服务脚本

[root@s1 ~]# cat /usr/lib/systemd/system/redis.service [Unit] Description=Redis persistent key-value database After=network.target After=network-online.target Wants=network-online.target [Service] ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf --supervised systemd ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID Type=notify User=redis Group=redis RuntimeDirectory=redis RuntimeDirectoryMode=0755 [Install] WantedBy=multi-user.target 

创建用户

[root@node17 ~]# groupadd -g 2019 redis && useradd -g redis -u 2019 redis [root@node17 ~]# id redis uid=2019(redis) gid=2019(redis) groups=2019(redis) 

授权

[root@node17 ~]# chown redis:redis /usr/local/redis/ -R 

创建软链接

[root@node17 redis]# ln -sv ./bin/* /usr/bin/ ‘/usr/bin/redis-benchmark’ -> ‘./bin/redis-benchmark’ ‘/usr/bin/redis-check-aof’ -> ‘./bin/redis-check-aof’ ‘/usr/bin/redis-check-rdb’ -> ‘./bin/redis-check-rdb’ ‘/usr/bin/redis-cli’ -> ‘./bin/redis-cli’ ‘/usr/bin/redis-sentinel’ -> ‘./bin/redis-sentinel’ ‘/usr/bin/redis-server’ -> ‘./bin/redis-server’ 

启动服务

systemctl start redis 

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!