## 下载redis
```
axel -n 10 http://download.redis.io/releases/redis-6.2.6.tar.gz
tar -zxvf redis-6.2.6.tar.gz
cd redis-6.2.6
```
## 安装redis
```
make PREFIX=/alidata/server/redis/ install
```
## 配置redis
#### 新建数据目录和日志目录
~~~
mkdir /alidata/server/redis/data
mkdir /alidata/server/redis/log
cp ./redis.conf /alidata/server/redis/
vim /alidata/server/redis/redis.conf
~~~
#### 编辑内容
~~~
# IP绑定
bind 0.0.0.0
# 保护模式(开启条件为各redis之间可以互相通信,做集群不可开启)
protected-mode yes
# 访问端口
port 6379
# 连接超时,单位S,0为不启用超时
timeout 0
# 以守护进程运行
daemonize yes
# 数据文件路径(dump.rdb附近)
dir /alidata/server/redis/data
# 进程ID文件的路径
pidfile /var/run/redis.pid
# 日志文件路径
logfile /alidata/server/redis/log/redis.log
# 设置登陆密码
requirepass [redis的密码]
# 禁用部分危险命令
rename-command FLUSHALL ""
rename-command CONFIG ""
rename-command KEYS ""
rename-command EVAL ""
# 开启键过期删除通知
notify-keyspace-events Ex
~~~
## 性能优化
~~~
# 编辑/etc/rc.local
vim /etc/rc.local
echo never > /sys/kernel/mm/transparent_hugepage/enabled
# 添加/etc/rc.local执行权限
chmod +x /etc/rc.d/rc.local
# 编辑/etc/sysctl.conf
vim /etc/sysctl.conf
vm.overcommit_memory = 1
net.core.somaxconn = 5120
# 立即解决
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo 5120 > /proc/sys/net/core/somaxconn
sysctl vm.overcommit_memory=1
sysctl -p
~~~
## 修改目录归属
~~~
useradd -s /sbin/nologin -M redis
chown -R redis:redis /alidata/server/redis
~~~
## 启动redis并设置开机启动
~~~
# 进入单元文件目录
cd /usr/lib/systemd/system
# 创建redis单元文件,格式为: [单元文件名].[单元文件类型]
vim redis.service
[Unit]
Description=Start redis on boot.
After=default.target network.target
[Service]
User=redis
Group=redis
Type=forking
#PIDFile=/var/run/redis.pid
ExecStart=/alidata/server/redis/bin/redis-server /alidata/server/redis/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false
#Restart=always
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
# 修改文件权限为只有root用户可以编辑该文件
chown -R root:root /etc/systemd/system/redis.service
chmod -R 644 /etc/systemd/system/redis.service
# 更新systemd
systemctl daemon-reload
systemctl enable redis
systemctl start redis
~~~
## redis加入系统服务
```
cp /home/redis/redis-6.2.6/utils/redis_init_script /etc/init.d/redis
```
## 编辑/etc/init.d/redis
```
vim /etc/init.d/redis
```
```
#!/bin/sh
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
### BEGIN INIT INFO
# Provides: redis_6379
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Redis data structure server
# Description: Redis data structure server. See https://redis.io
### END INIT INFO
REDISPORT=6379
EXEC=/alidata/server/redis/bin/redis-server
CLIEXEC=/alidata/server/redis/bin/redis-cli
PIDFILE=/var/run/redis.pid
CONF=/alidata/server/redis/redis.conf
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT -a [redis的密码] shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
```
#如果没有设置开机启动 执行chkconfig redis on
## 配置环境变量
```
vim /etc/profile
PATH=/alidata/server/redis/bin:$PATH
# 使配置生效
source /etc/profile
```