文章目录


安装环境



  • Linux 平台:CentOS 7
  • Redis 版本:6.2.5


安装步骤


Redis 6.2.6 是目前最新的稳定版本。注意,Redis 官方只提供 Linux 版本的安装包,官方建议使用安装在 Linux 操作系统上,这样能发挥最大的性能。但是也有非官方的 Windows Redis 版本,由 Apache 改造开发,但已经好几年不更新维护 Windows 版本的 Redis 了。

官方安装包下载地址:https://redis.io/download

Linux 平台我们一般使用源代码编译的方式进行安装。即下载源代码安装包,解压,编译,安装。其实在下载的 Redis 压缩包里面有个 ​​README.md​​ 文件就介绍了如何进行编译安装。

注意,如果 wget,tar,make 等命令没有安装的话,使用 ​​yum install -y wget/tar/make​​ 进行安装。

首先下载 Linux 版本的 Redis 安装包,即 redis-6.2.6.tar.gz 。上传到 Linux 服务器上某个目录下,例如 ​​/opt​​​ 目录。或者直接在 Linux 上使用 ​​wget​​ 下载,如下所示:

wget https://download.redis.io/releases/redis-6.2.6.tar.gz

在 Linux 上下载安装最新版本的 gcc 编译器,即安装 C 语言的编译环境。

yum install -y gcc-c++

测试 gcc 编译器是否安装成功。

gcc -v

解压 Redis 压缩包,并进入解压后的目录。

tar -zxvf redis-6.2.6.tar.gz
cd redis-6.2.6

使用 ​​make​​​ 命令进行编译。其实此步骤编译之后已经可以在 src 目录中找到二进制执行文件了,可以直接使用命令 ​​./redis-server ../redis.conf​​ 启动服务。

make

使用 ​​make install​​​ 命令进行安装,默认是安装到 ​​/usr/local/bin​​​ 目录下。我们可以通过 ​​PREFIX​​ 选项指定要安装的目录。

mkdir /usr/local/redis
make install PREFIX=/usr/local/redis

经过上步骤的安装之后,我们可以看到 /usr/local/redis/bin 目录下有如下几个可执行文件:


  • redis-cli:Redis 客户端连接工具
  • redis-server:Redis 服务启动工具
  • redis-sentinel:Redis 集群使用
  • redis-benchmark:Redis 性能测试工具
  • redis-check-aof:可修复 AOF 文件的工具
  • redis-check-rdb:可修复 RDB 文件的工具

现在已经可以使用如下命令启动 Redis 服务。注意,这是前台方式启动,不推荐。

/usr/local/redis/bin/redis-server

后台方式启动,首先拷贝一份配置文件到其他目录,不直接修改原配置文件是如果后续出问题可以再从原文件拷贝。

cp /opt/redis-6.2.6/redis.conf /usr/local/redis/bin

使用 vim 命令打开复制后的 Redis 配置文件,将 daemonize 参数的值改为 yes,即代表后台方式启动。

# set nu 显示行号
# 75行 注释掉下面一行,这样任何IP都可以访问Redis服务
# bind 127.0.0.1 -::1

# 94行 保护模式关闭
protected-mode no

# 98行 Redis端口号,可以修改
port 6379

# 257行 守护进程方式启动
daemonize yes

# 901行 复制另起一行,设置密码
# requirepass foobared
requirepass 123456

最后使用配置文件,以后台方式启动 Redis 服务。

/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf

验证


查看 Redis 服务进程状态。

[root@chenpi bin]# ps -ef | grep redis
root 5981 1 0 19:02 ? 00:00:00 /usr/local/redis/bin/redis-server *:6379
root 5987 1342 0 19:02 pts/0 00:00:00 grep --color=auto redis
[root@chenpi bin]#

使用客户端工具连接,进行验证。

/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379

使用客户端成功连接后,如果没输入密码进行验证,执行 Redis 命令会报错。认证后,可以使用 ​​PING​​​ 命令进行测试,正常返回 ​​PONG​​ 。

127.0.0.1:6379> PING
(error) NOAUTH Authentication required.
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> PING
PONG
127.0.0.1:6379>

当然,也可以在使用客户端连接时指明密码,不过不推荐,毕竟是明文显示。

/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 -a 123456

退出客户端连接,可以使用快捷键 Ctrl+C 或者使用 exit 命令。

127.0.0.1:6379> exit
[root@chenpi bin]#

关闭 Redis 服务,可以使用 ​​kill Redis服务进程号​​的方式;或者使用客户端工具关闭,如下:

/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 -a 123456 shutdown

当然,也可以使用客户端工具连接进入终端后,再进行关闭。

127.0.0.1:6379> SHUTDOWN
(error) NOAUTH Authentication required.
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> SHUTDOWN
not connected>

开机自启动(可选)


在 ​​/etc/init.d​​​ 目录下新建一个脚本文件 ​​redis​​​ ,文件名可随意,一般是 ​​redis_端口​​ 的形式。输入以下内容:

#!/bin/sh

# Redis服务端启动文件
EXEC=/usr/local/redis/bin/redis-server
# Redis客户端连接工具,主要用于关闭Redis服务
CLIEXEC=/usr/local/redis/bin/redis-cli
# pid保存所在文件位置
PIDFILE=/var/run/redis_6379.pid
# 配置文件
CONF="/usr/local/redis/bin/redis.conf"
# 端口号
REDISPORT="6379"
# 密码,如果有
REDISPASSWORD="123456"

###############
# SysV Init Information
# chkconfig: - 58 74
# description: redis_6380 is the redis daemon.
### BEGIN INIT INFO
# Provides: redis_6380
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Should-Start: $syslog $named
# Should-Stop: $syslog $named
# Short-Description: start and stop redis_6380
# Description: Redis daemon
### END INIT INFO


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 $REDISPASSWORD shutdown
# 如果不需要密码,使用 $CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
status)
PID=$(cat $PIDFILE)
if [ ! -x /proc/${PID} ]
then
echo 'Redis is not running'
else
echo "Redis is running ($PID)"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "Please use start, stop, restart or status as first argument"
;;
esac

保存脚本文件后,为文件添加可执行权限。

[root@chenpihost init.d]# chmod 755 /etc/init.d/redis

使用此脚本文件启动 Redis 服务,并增加 Redis 服务,让 ​​chkconfig​​​ 指令得以管理它。如果要删除可以使用命令 ​​chkconfig –del name​​ 。

# 启动Redis服务
[root@chenpihost init.d]# service redis start
Starting Redis server...

# 查看是否启动成功
[root@chenpihost init.d]# ps -ef | grep redis
root 1456 1 0 10:43 ? 00:00:00 /usr/local/redis/bin/redis-server *:6379
root 1476 1359 0 10:44 pts/0 00:00:00 grep --color=auto redis

# 增加redis服务,让chkconfig指令得以管理它,并同时在系统启动的叙述文件内增加相关数据。
[root@chenpihost init.d]# chkconfig --add redis

# 显示所有运行级系统服务的运行状态信息(on或off)
[root@chenpihost init.d]# chkconfig --list

Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.

If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.

netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
redis 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@chenpihost init.d]#

注意,如果你的 Redis 配置文件设置了密码,但是脚本文件没有写上密码,会导致关闭服务失败,如下:

[root@chenpihost init.d]# service redis stop
Stopping ...
(error) NOAUTH Authentication required.
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
只有添加了密码之后,才能关闭成功,如下:

重启 Linux 服务器,验证 Redis 服务是否自启动成功,验证如下则成功。

[root@chenpihost ~]# ps -ef | grep redis
root 1028 1 0 11:03 ? 00:00:00 /usr/local/redis/bin/redis-server *:6379
root 1344 1326 0 11:03 pts/0 00:00:00 grep --color=auto redis
[root@chenpihost ~]#

防火墙设置


将 Redis 服务端口加到防火墙上,不然外部无法访问 Redis 服务。即添加端口,重启防火墙,查看防火墙开放的端口。

[root@chenpi rc.d]# firewall-cmd --zone=public --add-port=6379/tcp --permanent
success
[root@chenpi rc.d]# systemctl restart firewalld
[root@chenpi rc.d]# firewall-cmd --zone=public --list-ports
6379/tcp
[root@chenpi rc.d]#

脚本安装

在解压后的 ​​utils​​​ 目录下有一些脚本工具,其中的 ​​install_server.sh​​ 脚本可以用来安装 Redis,执行脚本后,按照提示输入即可,最终会自动启动 Redis 服务。而且可以使用这个脚本启动多个 Redis 服务。

Linux 平台安装 Redis 6_linux

[root@chenpihost 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] 6380
Please select the redis config file name [/etc/redis/6380.conf]
Selected default - /etc/redis/6380.conf
Please select the redis log file name [/var/log/redis_6380.log]
Selected default - /var/log/redis_6380.log
Please select the data directory for this instance [/var/lib/redis/6380]
Selected default - /var/lib/redis/6380
Please select the redis executable path [] /usr/local/redis/bin/redis-server
Selected config:
Port : 6380
Config file : /etc/redis/6380.conf
Log file : /var/log/redis_6380.log
Data dir : /var/lib/redis/6380
Executable : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6380.conf => /etc/init.d/redis_6380
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@chenpihost utils]#

注意,如果执行脚本时报以下错误。

[root@chenpihost utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

This systems seems to use systemd.
Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!

那么可以打开 ​​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

验证,即可看到端口为 6380 的 Redis 服务已经启动成功。

[root@chenpihost utils]# ps -ef | grep redis
root 1358 1 0 Dec25 ? 00:00:00 /usr/local/redis/bin/redis-server *:6379
root 1478 1 0 00:06 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:6380
root 1485 1334 0 00:08 pts/0 00:00:00 grep --color=auto redis
[root@chenpihost utils]#