四. 安装与配置 Redis 并设置开机启动
1.安装 gcc
Redis 由 C语言编写,所以需要系统中有 gcc 编译器,使用 yum install gcc
命令安装
# yum install gcc
安装完毕后,测试 gcc 是否安装成功
# gcc --version
2.安装 Redis
在 Redis 官网 下载 Redis 压缩包,这里版本为 redis-6.2.6.tar.gz
将 redis-6.2.6.tar.gz
上传至 /home/
中,然后对Redis进行安装:
# cd /home/
# tar -zxvf redis-6.2.6.tar.gz
# cd redis-6.2.6
# make
# make install PREFIX=/usr/local/redis
# cp /home/redis-6.2.6/redis.conf /usr/local/redis/bin/redis.conf
此时Redis已安装完成,目录为 /usr/local/redis/bin
,文件功能如下:
redis.conf:redis配置文件
redis-benchmark:性能测试工具,可以在自己本子运行,看看自己本子性能如何
redis-check-aof:修复有问题的AOF文件,rdb和aof后面讲
redis-check-dump:修复有问题的dump.rdb文件
redis-sentinel:Redis集群使用
redis-server:Redis服务器启动命令
redis-cli:客户端,操作入口
3.修改Redis配置文件
/usr/local/redis/bin/redis.conf
文件过长,建议下载修改后上传:
-
设置密码
-
守护线程 daemonize,设置为 yes
-
注释掉 bind
-
将保护模式关闭(否则无法远程访问)
4.配置开机自启动 Redis
- 编辑
vim /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=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
# CONF="/etc/redis/${REDISPORT}.conf"
CONF="/usr/local/redis/bin/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 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
- 设置权限:
# chmod 755 /etc/init.d/redis
- 服务生效
# systemctl daemon-reload
- 开机自启动
# systemctl enable redis.service
- 启动服务
# systemctl start redis.service
- 查看服务状态
# systemctl status redis.service