Redis教程

redis 简单安装

本文主要是介绍redis 简单安装,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

参考官网
https://redis.io/download/
https://redis.io/docs/getting-started/installation/install-redis-from-source/

下载最新稳定版,当前是 7.0.0
wget https://download.redis.io/redis-stable.tar.gz

tar zxf /usr/local/src/redis-stable.tar.gz -C /usr/local/

安装编译工具
yum install gcc-c++ make

cd /usr/local/redis-stable && make
至此,redis二进制文件都已生成,且在本目录下,具备可迁移性
# 如果要安装二进制文件到系统目录/usr/local/bin,则执行以下安装命令,否则忽略
make install

调整目录结构,使其更直观
mkdir etc bin
备份原始配置,并修改配置
cp redis.conf etc/redis_default.conf
grep -vP '^#|^$' > etc/redis.conf

## 修改配置
# tcp-keepalive 默认300s,可改为 60s
tcp-keepalive 60
# 单机多实例时,需分别指定不同的pid文件
pidfile /var/run/redis_6379.pid
# 日志
logfile /usr/local/redis-stable/logs/redis.log
# 实例目录
dir /usr/local/redis-stable/
## 更多配置修改,参考原始配置和官方网站

# 单机多实例时,建议以不同目录区分实例,如 redis_6379、redis_6380 等。

cp src/mkreleasehdr.sh bin/
cp src/redis-benchmark bin/
cp src/redis-check-aof bin/
cp src/redis-check-rdb bin/
cp src/redis-cli bin/
cp src/redis-sentinel bin/
cp src/redis-server bin/
cp src/redis-trib.rb bin/


## 也可以使用自带工具初始化,指定目录和配置、端口,会将二进制文件和配置文件部署到自定义目录:
sh utils/install_server.sh


## 启动,并检查日志
## """
3733276:M 22 Feb 2022 17:21:35.781 # Server initialized
3733276:M 22 Feb 2022 17:21:35.781 # 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.
3733276:M 22 Feb 2022 17:21:35.781 # 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.
3733276:M 22 Feb 2022 17:21:35.781 * Ready to accept connections
3733276:M 22 Feb 2022 17:21:35.781 * The server is now ready to accept connections at /usr/local/redis-stable/redis.sock
"""

echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf

sysctl vm.overcommit_memory=1 > /dev/null
echo never > /sys/kernel/mm/transparent_hugepage/enabled


### 启停脚本

## 编写启动脚本 /usr/local/redis-stable/bin/startup.sh
#!/bin/bash

# @env
sysctl vm.overcommit_memory=1 > /dev/null
echo never > /sys/kernel/mm/transparent_hugepage/enabled

# startup
REDIS_HOME="/usr/local/redis-stable"
${REDIS_HOME}/bin/redis-server ${REDIS_HOME}/etc/redis.conf


## 编写停服脚本 /usr/local/redis-stable/bin/stop.sh
#!/bin/bash

pid=`ps -ef |grep 'redis-stable/'|grep -v grep|awk '{print $2}'`
kill -15 ${pid} || kill -9 ${pid}

## 常用基础命令,参考 菜鸟教程 https://www.runoob.com/redis/redis-tutorial.html

这篇关于redis 简单安装的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!