本文参考自Redis README文档: README.md。
- 安装gcc编译器。
shell> yum -y install gcc
CentOS 7 Yum默认安装的gcc编译器版本为4.8.5,最高支持到Redis 5.x.x版本,Redis 6.x.x版本需要升级gcc编译器。
- 升级gcc编译器(Redis 5.x.x及以下版本可跳过此步骤)。
shell> yum -y install centos-release-scl
shell> yum -y install devtoolset-9-gcc*
// 切换gcc编译器版本有2种方式
// a.永久生效(推荐)
shell> echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile
shell> source /etc/profile
// b.临时生效(重启之类的会恢复到旧版本)
shell> scl enable devtoolset-9 bash
- 下载并解压Redis源码包。
shell> wget http://download.redis.io/releases/redis-6.0.6.tar.gz
shell> tar xzf redis-6.0.6.tar.gz -C /usr/local/src
Redis源码包 官方下载地址: https://redis.io/download
Redis源码包 GitHub托管站下载地址: https://github.com/redis/redis/releases
tip:通过EPEL源(版本较旧)或Remi源(版本较新)亦可安装Redis,本博文不提供,请自寻教程
- 编译并安装Redis。
shell> cd /usr/local/src/redis-6.0.6
shell> make
shell> make install
- 使用官方提供的脚本将Redis注册为系统服务。
shell> cd /usr/local/src/redis-6.0.6/utils
shell> ./install_server.sh
如果遇到以下报错:
-bash: This systems seems to use systemd.
-bash: Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!
则需编辑install_server.sh脚本文件:
shell> vi /usr/local/src/redis-6.0.6/utils/install_server.sh
注释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
// 上面步骤中输入./install_server.sh启动脚本后,按照脚本提示信息自定义几项配置(看不懂英文请善用翻译工具),不输入直接按回车则是设为[]内的默认配置
shell> Welcome to the redis service installer
shell> This script will help you easily set up a running redis server
shell> Please select the redis port for this instance: [6379]
shell> Selecting default: 6379
shell> Please select the redis config file name [/etc/redis/6379.conf] /etc/redis/redis.conf
shell> Please select the redis log file name [/var/log/redis_6379.log] /var/log/redis.log
shell> Please select the data directory for this instance [/var/lib/redis/6379] /var/lib/redis
shell> Please select the redis executable path [/usr/local/bin/redis-server]
shell> Selected config:
shell> Port : 6379
shell> Config file : /etc/redis/redis.conf
shell> Log file : /var/log/redis.log
shell> Data dir : /var/lib/redis
shell> Executable : /usr/local/bin/redis-server
shell> Cli Executable : /usr/local/bin/redis-cli
shell> Is this ok? Then press ENTER to go on or Ctrl-C to abort.
// 这里还会让你输入一次指令进行确认,ENTER回车则是确认执行,Ctrl+C则是取消执行脚本。
shell> Copied /tmp/6379.conf => /etc/init.d/redis_6379
shell> Installing service...
shell> Successfully added to chkconfig!
shell> Successfully added to runlevels 345!
shell> Starting Redis server...
shell> Installation successful!
shell> Copied /tmp/6379.conf => /etc/init.d/redis_6379
可以看到生成了一个名为redis_6379的服务。
- 编辑配置文件(自行根据需要配置)。
shell> vi /etc/redis/redis.conf
modify...
默认情况下,如果没有指定"bind"配置指令,Redis将侦听服务器上所有可用网络接口的连接。使用"bind"配置指令,后跟一个或多个IP地址,可以只监听一个或多个选定的接口。如果确定希望实例监听所有接口,只需注释以下一行。
注释掉就是允许所有ip访问此Redis服务,没注释就是配一个允许一个。
# bind 127.0.0.1
保护模式默认是启用的。只有当您确定希望其他主机的客户机连接到Redis,且没有配置身份验证,也没有使用"bind"指令显式列出一组特定的接口时,才应该禁用它。
说人话就是,你没配监听地址bind xxx.xxx.xxx.xxx,且,没配密码requirepass password,才要设为no。
protected-mode yes
默认情况下,Redis不作为守护进程运行。如果需要,请使用"yes"。
daemonize yes
配置身份验证密码。
requirepass password
- 启动Redis服务器。
shell> systemctl start redis_6379
shell> systemctl status redis_6379
shell> systemctl restart redis_6379
shell> systemctl status redis_6379
第一次启动Redis服务后,服务的Active状态为【仅执行一次就正常结束】的active (exited),而不是【正在系统中执行】的active (running)。
重启一下Redis服务就行了,不是很懂什么原因导致Redis第一次启动完就结束,而不是作为守护进程保持运行。
- 开放firewall防火墙6379端口。
// 开放6379端口
shell> firewall-cmd --add-port=6379/tcp --zone=public --permanent
// 重启防火墙
shell> firewall-cmd --reload