本文介绍在 CentOS 7.x 操作系统上安装 Redis 的方法与过程。
版本说明
- CentOS Linux release 7.6
- Redis 5.0.6
安装步骤
下载 Redis,选择
Stable
,本示例使用 Redis 5.0.6 版本,下载文件是redis-5.0.6.tar.gz
。将下载文件拷贝到 CentOS 服务器特定目录下,如
/opt
。执行解压命令
tar -zxvf redis-5.0.6.tar.gz
。进入解压后的文件夹
cd redis-5.0.6/
,执行make
命令进行编译,编译完成后出现以下提示。
Hint: It's a good idea to run 'make test' ;)
- 执行
make test
命令对编译结果进行检查,检查编译通过会出现以下提示。
\o/ All tests passed without errors!
Cleanup: may take some time... OK
- 执行
make install
命令进行安装,安装成功会出现以下提示。
[root@... redis-5.0.6]# make install
cd src && make install
make[1]: Entering directory `/opt/redis-5.0.6/src'
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory `/opt/redis-5.0.6/src'
- 修改
redis.conf
文件,将daemonize no
修改为daemonize yes
(开启服务后台启动)。
################################# GENERAL #####################################
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
- 修改
redis.conf
文件,将bind 127.0.0.1
注释掉(开启远程访问)。
################################## NETWORK #####################################
# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# bind 127.0.0.1
- 修改
redis.conf
文件,将protected-mode yes
修改为protected-mode no
(开启远程访问)。
# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
# "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode no
- 执行
redis-server redis.conf
命令启动。
[root@... redis-5.0.6]# redis-server redis.conf
54786:C 18 Nov 2019 16:58:59.875 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
54786:C 18 Nov 2019 16:58:59.875 # Redis version=5.0.6, bits=64, commit=00000000, modified=0, pid=54786, just started
54786:C 18 Nov 2019 16:58:59.875 # Configuration loaded
- 执行
redis-cli
命令进入控制台进行测试,测试完成后执行SHUTDOWN
命令关闭。
[root@... redis-5.0.6]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> SHUTDOWN
not connected> quit