一、安装前准备
- 准备编译环境:
~]# yum groupinstall -y "Development Tools"
复制代码
- 升级gcc版本(Redis 6以上版本编译时需要5版本以上gcc)
~]# yum -y install centos-release-scl systemd-devel && yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils && scl enable devtoolset-9 bash
复制代码
二、安装
下载地址:download.redis.io/releases/re…
- 构建
可用构建选项:
~]# make BUILD_TLS=yes # 开启TLS支持
~]# make USE_SYSTEMD=yes
# 开启systemd支持。需systemd开发库支持(Debian/Ubuntu平台上的libsystemd-dev、CentOS上的systemd-devel)
~]# make PROG_SUFFIX="-alt" # 在Redis程序名称后添加后缀
~]# make 32bit # 构建32位程序
~]# make 64bit # 构建64位程序
复制代码
构建后需使用make test
测试,如开启了TLS支持,需在开启TLS的情况下测试(需预先安装tcl-tls
)
~]# ./utils/gen-test-certs.sh
~]# ./runtest --tls
复制代码
- 要把Redis二进制文件安装在/usr/local/bin,使用:
~]# make install
复制代码
如果要安装在别的目录,可以使用make PREFIX=/some/other/directory install
- make install操作只是在系统中安装可执行文件,不会配置初始化脚本和配置文件。 如果只是想要玩一下没问题,如果你要在生产环境中使用,就需要一个初始化脚本。
~]# cd utils
~]# ./install_server.sh
复制代码
- 系统设置提示:
- 建议使用Linux部署Redis
- 添加vm.overcommit_memory = 1到/etc/sysctl.conf,执行
sysctl vm.overcommit_memory=1
使配置生效。 - 关闭内核特性transparent huge pages,它会对内存使用和延迟方面有负面影响。
echo never > /sys/kernel/mm/transparent_hugepage/enabled
- 添加swap内存,建议和内存大小一样大。目的为避免Redis意外使用大量内存耗尽系统内存时,避免Redis被OOM或因内存不足而崩溃。
- 设置明确的
maxmemory
,以限制Redis可使用的内存。目的是当Redis使用的内存达到系统限制时会因内存不足而崩溃,设置限制之后,当达到限制内存大小后,Redis不会直接崩溃,而是发出错误报告 。假设可用内存有10G,可以设置为8或者9G
/etc/sysctl.conf中添加以下内容:
vm.overcommit_memory = 1
net.core.somaxconn = 2048
fs.file-max = 6553560
复制代码
执行以下命令使参数生效
~]# sysctl -p
复制代码
三、报错:
- gcc版本问题
server.c:5342:39: error: ‘struct redisServer’ has no member named ‘maxmemory’
if (server.maxmemory > 0 && server.maxmemory < 1024*1024) {
^
server.c:5343:176: error: ‘struct redisServer’ has no member named ‘maxmemory’
serverLog(LL_WARNING,"WARNING: You specified a maxmemory value that is less than 1MB (current value is %llu bytes). Are you
sure this is what you really want?", server.maxmemory);
^server.c:5346:31: error: ‘struct redisServer’ has no member named ‘server_cpulist’
redisSetCpuAffinity(server.server_cpulist);
^
server.c: In function ‘hasActiveChildProcess’:
server.c:1478:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
server.c: In function ‘allPersistenceDisabled’:
server.c:1484:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
server.c: In function ‘writeCommandsDeniedByDiskError’:
server.c:3934:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
server.c: In function ‘iAmMaster’:
server.c:5134:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
make[1]: *** [server.o] Error 1
make[1]: Leaving directory `/root/redis-6.0.9/src'
make: *** [install] Error 2
复制代码
解决:升级gcc版本
~]# yum -y install centos-release-scl && yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils && scl enable devtoolset-9 bash
复制代码
升级后查看gcc版本:
[root@instance-70o9a3yw redis-6.0.9]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-9/root/usr --mandir
=/opt/rh/devtoolset-9/root/usr/share/man --infodir=/opt/rh/devtoolset-9/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-9.3.1-20200408/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linuxThread model: posix
gcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC)
复制代码
- 执行install_server.sh报错
[root@instance-70o9a3yw redis-6.0.9]# 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!
复制代码
解决1:注释判断部分脚本内容,后重新运行install_server.sh
76 #bail if this system is managed by systemd
77 _pid_1_exe="$(readlink -f /proc/1/exe)"
78 if [ "${_pid_1_exe##*/}" = systemd ]
79 then
80 echo "This systems seems to use systemd."
81 echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!
" 82 exit 1
83 fi
84 unset _pid_1_exe
复制代码
使用这种解决方法,会使用service的方式管理服务,而在CentOS 7 系统中,默认是systemd的方式管理服务。
解决2:在utils目录下有redis服务systemd管理文件,修改后使用systemd管理
-
构建时使用
make USE_SYSTEMD=yes
启用systemd支持 -
创建redis用户及所需目录
~]# useradd -M -s /sbin/nologin redis
~]# mkdir -p /data/redis && chown -R redis.redis /data/redis
~]# mkdir -p /var/log/redis/ && chown -R redis.redis /var/log/redis
复制代码
- 为redis提供systemd管理文件
[Unit]
Description=Redis data structure server
Documentation=https://redis.io/documentation
Wants=network-online.target
After=network-online.target
[Service]
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf --supervised systemd --daemonize no
LimitNOFILE=10032
NoNewPrivileges=yes
OOMScoreAdjust=-900
PrivateTmp=yes
Type=notify
TimeoutStartSec=20
TimeoutStopSec=20
UMask=0077
User=redis
Group=redis
WorkingDirectory=/data/redis
[Install]
WantedBy=multi-user.target
复制代码
复制/utils/systemd-redis_server.service至/usr/lib/systemd/system下
cp /root/redis-6.0.9/utils/systemd-redis_server.service /usr/lib/systemd/system/redis.service
复制代码
utils/systemd-redis_server.service文件说明:
# example systemd service unit file for redis-server
# redis-server的systemd服务文件示例
#
# In order to use this as a template for providing a redis service in your
# environment, _at the very least_ make sure to adapt the redis configuration
# file you intend to use as needed (make sure to set "supervised systemd"), and
# to set sane TimeoutStartSec and TimeoutStopSec property values in the unit's
# "[Service]" section to fit your needs.
# 为使用这个模板管理redis服务,_至少_保证这个文件和redis的配置文件相适应。确保设置"supervised systemd"
# 设置TimeoutStartSec 和 TimeoutStopSec为你需要的值。
#
# Some properties, such as User= and Group=, are highly desirable for virtually
# all deployments of redis, but cannot be provided in a manner that fits all
# expectable environments. Some of these properties have been commented out in
# this example service unit file, but you are highly encouraged to set them to
# fit your needs.
# 有些参数,如User=和Group=非常建议配置,但无法以适合所有环境的方式提供。某些参数已被注释
# 但强烈建议按照需求设置这些参数。
复制代码
NOTIFY_SOCKET not found
r报错
日志中报错如下:
systemd supervision requested, but NOTIFY_SOCKET not found
解决:
配置文件中supervised
的值改为auto