1、下载程序包
wget https://download.redis.io/redis-stable.tar.gz
2、解压、编译安装
tar -xf redis-stable.tar.gz
cd redis-stable
make
3、配置
# 内核参考优化
echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
sysctl -p
# 创建数据目录并配置执行文件
mkdir -p /usr/local/redis/{bin,etc,var}
cp src/{redis-benchmark,redis-check-aof,redis-check-rdb,redis-cli,redis-sentinel,redis-server} /usr/local/redis/bin/
ln -s /usr/local/redis/bin/* /usr/local/bin/
# 拷贝配置文件
cp redis.conf /usr/local/redis/etc/
# 创建用户并授权
useradd -M -s /sbin/nologin redis
chown -R redis:redis /usr/local/redis/{var,etc}
# 修改部分的配置文件
# vim /usr/local/redis/etc/redis.conf
...
bind 0.0.0.0
timeout 600
daemonize yes
pidfile /var/run/redis/redis.pid
dir /usr/local/redis/var
requirepass password
maxmemory 4G
maxmemory-policy volatile-lru
appendonly yes
...
4、服务配置
# vim /etc/systemd/system/redis-server.service
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]
Type=forking
PIDFile=/var/run/redis/redis.pid
User=redis
Group=redis
Environment=statedir=/var/run/redis
PermissionsStartOnly=true
ExecStartPre=/bin/mkdir -p ${statedir}
ExecStartPre=/bin/chown -R redis:redis ${statedir}
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
ExecStop=/bin/kill -s TERM $MAINPID
Restart=always
LimitNOFILE=1000000
LimitNPROC=1000000
LimitCORE=1000000
[Install]
WantedBy=multi-user.target
官方提供的脚本将Redis注册为系统服务
# cd /usr/local/src/redis-stable/utils
# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/usr/local/redis/etc/6379.conf]
Selected default - /usr/local/redis/etc/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] /usr/local/redis/var
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
# 如果遇到以下问题
This systems seems to use systemd.
Please take a look at the provided example service unit filesin this directory, and adapt and install them. Sorry!
# 编辑install_server.sh脚本文件,注释以下几行代码后,再次执行./install_server.sh
#bail if this system is managed by systemd
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
# echo "This systems seems to use systemd."
# echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
# exit 1
#fi
5、启动服务
systemctl enable --now redis-server
6、验证
redis-cli -h 127.0.0.1 -p 6379 -a password info
参考:
https://redis.io/docs/getting-started/installation/install-redis-from-source/