CentOS 7 源码编译安装 Redis 6.x/5.x

本文为单机部署步骤,参考自Redis官网: Redis Download。

  1. 安装gcc。
shell> yum -y install gcc

CentOS 7默认安装的gcc版本为4.8.5,最高支持到Redis 5.x.x版本,Redis 6.x.x版本需要升级gcc。

  1. 升级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.临时生效(重启之类的会恢复到原gcc版本)
shell> scl enable devtoolset-9 bash
  1. 下载并解压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

  1. 编译并安装Redis。
shell> cd /usr/local/src/redis-6.0.6
shell> make
shell> make install
  1. 使用官方提供的脚本将Redis注册为系统服务。
shell> cd /usr/local/src/redis-6.0.6/utils
shell> ./install_server.sh

如果遇到:
shell> This systems seems to use systemd.
shell> 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
// 接下来按照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
// redis-server的路径
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.
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!
  1. 修改配置文件(开发环境推荐配置)。
shell> vi /etc/redis/redis.conf
modify...

默认情况下,如果没有指定"bind"配置指令,Redis将侦听服务器上所有可用网络接口的连接。使用"bind"配置指令,后跟一个或多个IP地址,可以只监听一个或多个选定的接口。如果确定希望实例监听所有接口,只需注释下一行。
# bind 127.0.0.1

保护模式默认是启用的。只有当您确定希望其他主机的客户机连接到Redis,且没有配置身份验证,也没有使用"bind"指令显式列出一组特定的接口时,才应该禁用它。
protected-mode yes

默认情况下,Redis不作为守护进程运行。如果需要,请使用"yes"。
daemonize yes

配置身份验证密码。
requirepass password

  1. 启动Redis服务器。
shell> systemctl start redis_6379
shell> systemctl status redis_6379
shell> systemctl restart redis_6379
shell> systemctl status redis_6379

在Redis 6.x.x中,此处第一次启动redis_6379服务后,服务的Active状态为错误的active (exited),而不是正确的active (running),重启一下服务发现又好了,暂不清楚原因。后续如果知道原因了再补充,毕竟不是专业运维。

  1. 开放firewall防火墙6379端口。
// 开放6379端口
shell> firewall-cmd --add-port=6379/tcp --zone=public --permanent
// 重启防火墙
shell> firewall-cmd --reload

你可能感兴趣的:(redis,centos,linux,服务器,java)